Operations
Insertion Operation
Inserting one or more element into an array is refers as Insertion Operation. According to the specification, a new element can be insert in following ways.
- Insertion at the beginning of an array
- Insertion at the given index of an array
- Insertion after the given index of an array
- Insertion before the given index of an array
Algorithm: (Inserting into a Linear Array) INSERT (LA, N, K, ITEM) Here LA is a linear array with N elements and K is a positive integer such that K≤N. This algorithm inserts an elements ITEM into the Kth position in LA.
[Initialize counter.] Set J: = N.
Repeat Steps 3 and 4 while J≥K.
[Move Jth element downwards]. Set LA[J + 1]:= LA[J]
[Decrease counter.] Set J:= J-1.
[End of Step 2 loop]
[Insert element.] Set LA[K] :=ITEM.
[Reset N.] Set N:= N+1.
Exit.
Deletion Operation
Removing an element from the array and rearranging the array‘s all elements known as Deletion in the array. Following algorithm deletes the Kth element from a linear array LA and assigns it to a variable ITEM.
Algorithm: (Deleting from a Linear Array) DELETE(LA, N, K, ITEM)
Here LA is a linear array with N elements and K is a positive integer such that K≤N. This algorithm deletes the Kth element from LA.
Set ITEM:= LA[K]
Repeat for J =K to N-1:
[Move J + 1st element upward] Set LA[J]:=LA[J+1].
[End of loop]
[Reset the number of N elements in LA] Set N:=N-1.
Exit.
Search Operation
Searching an element in the array can be based on its value or its index. Consider LA is a linear array with N elements and K is a positive integer such that K≤N. Following is the algorithm to find an element with a value of ITEM using sequential search.
Algorithm: (Searching into a Linear Array) SEARCH (LA, N, K, ITEM)
[Initialize counter.] Set J: = 0.
Repeat Steps 4 and 5 while J < N.
If LA[J]:= ITEM then GOTO STEP 6
Set J = J +1
PRINT J, ITEM
Exit.
Update Operation
Update operation can be performed according to a specified index of an existing element from the array. Consider LA is a linear array with N elements and K is a positive integer such that K≤N. Following is the algorithm to update an element available at the Kth position of LA.
Algorithm: (Updating into a Linear Array) UPDATE (LA, N, K, ITEM)
Initialize Counter.
Set LA[K-1] = ITEM
Exit.
Listing: Following program is showing the implementation of array and performing all the operations on array.
/* Program to perform array operations */
#include<stdio.h>
#define MAX 5
void insert(int*, int pos, int num);
void del(int*, int pos);
void reverse(int*);
void display(int* );
void search(int*, int num);
void main( )
{
int myarray[5];
insert(myarray, 1, 11);
insert(myarray, 2, 12);
insert(myarray, 3, 13);
insert(myarray, 4, 14);
insert(myarray, 5, 15);
printf(“\n Elements of Array: ”);
display(myarray);
del(myarray, 5);
del(myarray, 2);
printf(“\n\n After deletion: ”);
display(myarray);
insert(myarray, 2, 222) ;
insert(myarray, 5, 555) ;
printf(“\n\n After insertion of the elements:”);
display(myarray);
reverse(myarray);
printf(“\n\n After reversing the element: ”);
display(myarray);
search(myarray, 222);
search(myarray, 666);
getch();
}
/* Insert an element num at given position pos */
void insert(int *myarray, int pos, int num)
{
// shift elements to the right
int i;
for(I = MAX-1 ; I >=pos ; i--)
myarray[i] = myarray[i-1];
myarray[i] = num;
}
// Delete an element from the given position pos
void del(int*myarray, int pos)
{
//skip to the desired position
int i;
For(i = pos; i < MAX ; i++)
myarray[i-1] = myarray[i];
myarray[i-1] = 0;
}
// Reverse the entire array
void reverse(int* myarray)
{
int i;
for (i =0; i< MAX/2; i++)
{
int temp = myarray[i];
myarray[i] = myarray[MAX-1-i];
myarray[MAX-1-i] = temp;
}
}
// searches array for a given element num
void search(int*myarray, int num)
{
// traverse the array
int i;
for (i = 0; i< MAX; i++)
{
if(myarray[i] == num)
{
printf(“\n\n The element %d is present at %dth position., num, i + 1”);
return 0;
}
}
if(i== MAX)
printf(“\n\n The element %d is not present in the array.”, num );
}
//Display the contents of array
void display(int*myarray)
{
//traverse the entire array
int i;
printf(“\n”);
for(i=0; i<MAX ; i++)
printf(“%d\t”, myarray[i]);
}