Operation
Operations
on Data Structures
The
basic operations that are performed on data structures are as follows:
Insertion:Insertion means addition of a new data element in a data structure.
Deletion: Deletion means removal of a data element from a data structure if it is found.
Searching:Searching involves searching for the specified data element in a data structure.
Traversal:Traversal of a data structure means processing all the data elements present in it.
Sorting:Arranging data elements of a data structure in a specified order is called sorting.
Merging:Combining elements of two similar data structures to form a new data structure of the same type, is called merging.
Review
of Array
·
Array is a collection similar data element
·
Arrays data element (i.e. int,
float, char) whose memory is allocated in a contiguous block of memory.
Arrays
:: Declaration and Syntax
Syntax : datatype
array_name [ array_size ] ;
For example, take an integer array 'a’.
int a[6];
a[ ] is used to denote an array 'n'.
So, a6] means that 'a' is an array of 6 integers.
Here, 6 is the size of the array i.e. there are 6 elements of the array 'n'.
Giving array size i.e. 6 is necessary because
compiler needs to allocate space to that many integers. Compiler determines the
size of array by calculating the number of elements of an array.
This would effectively make
an area in memory (if availble) for a, which is 6 *
sizeof(int).
Basically sizeof() returns
the size of what is being passed based on machine.
Ie 6* 2= 12 byte it allocate
memory .
Here 'int n[6]' will allocate space to 6 integers of
size 2byte each.
initialize an entire array element
int a[ ] = { 2,3,15,8,48,13 };
In this case, we are declaring and assigning values
to the array at the same time. Here, no need to specify the array size because
compiler gets it from { 2,3,15,8,48,13 }.
Memory allocation calculation
example int a[4]={10,20,30,40};
10
|
20
|
30
|
40
|
a[0]
a[1] a[2] a[3]
staring
Address 200 202 204
206 207 last
Here staring address of array is 200 & last
address of array is 207.
allocation
of bytes in memory can be calculated as
total
memory = last adr- staring adr + 1;
= 207-200+1 => 8 byte
example :
initalization of array element individually
#include<stdio.h>
int main()
{
int arr[5];
arr[0] = 5;
arr[2] =
-10;
arr[1]
= 2;
arr[3] =
arr[0];
printf("%d
%d %d %d", arr[0], arr[1], arr[2], arr[3]);
return 0;
}
output 5 2 -10 5
output
enter the size of array 10 20 30 40
Array element are 10 20 30 40
C program for one dimensional array common
example used as reference in below Example 1:
#include<stdio.h>
Void main ( )
{
int a[3],i,n;
printf(“enter the size of array \n”);
scanf(“%d”,&n);
printf(“Enter
the array element \n”);
for(i=0;i<n;i++)
{
Scanf(“%d”,&a[i]);
}
printf(“Array
element are \n”);
for(i=0;i<n;i++)
{
printf(“%d”,&a[i]);
}
}//end of mainoutput
enter the size of array 10 20 30 40
Array element are 10 20 30 40
Multi-dimensional array
Array having more than one subscript variable is called
Multi Dimensional Array & is also called as Matrix.
These consist of rows and columns.
Syntax : datatype array_name [ row-size ] [Col-size];
Example
float x[3][4];
Here, x is a two-dimensional (2d) array. The array can
hold 12 elements. You can think the array as table with 3 row and each row has
4 column.
Initialization of a two
dimensional array
// Different ways to
initialize two dimensional array
int c[2][3] = {{1, 3, 0}, {-1, 5, 9}};
int c[][3] = {{1, 3, 0}, {-1, 5, 9}};
int c[2][3] = {1, 3, 0, -1, 5, 9};
int a[3][3] = { 1,2,3 5,6,7
8,9,0 };
int a[2][3] = { 1, 2, 3, 4, 5, 6 };
Here, value of a[0][0] is 1, a[0][1] is 2, a[0][2] is 3,
a[1][0] is 4, a[1][1] is 5 and a[1][2] is 6.
int a[2][2] = { 1, 2, 3, 4
}; /* valid */
int a[ ][2] = { 1, 2, 3, 4
}; /* valid */
int
a[2][ ] = { 1, 2, 3, 4 }; /* invalid */
int a[ ][ ] = { 1, 2, 3, 4
}; /* invalid */
#include<stdio.h>
{
int
i,j; // declaring and Initializing array
int arr[2][2] = {10,20,30,40};
/*
Above array can be initialized as below also
arr[0][0] = 10; // Initializing array
arr[0][1] = 20; arr[1][0] =
30; arr[1][1] = 40; */
for
(i=0;i<2;i++)
{
for
(j=0;j<2;j++)
{
// Accessing variables
printf("value of arr[%d] [%d] :
%d\n",i,j,arr[i][j]);
}
}
}// end of
main
output
output