DATA STRUCTURES



Admission Enquiry Form

What is deletion in Array.

Deletion in an array here means delete an element from a given location in an array.

deletion from an array by compuhelp

Algorithm of deletion in an Array

Delete(A,N,LOC) [LOC is the location of the element to be deleted]

Step1. START

Step2. Repeat for I = LOC to N – 1, I<-I+1

             A[I] <- A[I+1] [Move element with index I+1 left by 1]

             [End of loop]

Step3.  N <- N-1   [Reset the size of A to N-1]

Step4.  END

 


Program of deletion in an Array.

#include<stdio.h>
#include<conio.h>
void delete_array(int array[],int *n,int loc)
{
int i;
if (loc >= *n+1 )
{
printf("Deletion not possible.\n");
}
else
{
for ( i = loc - 1 ; i < *n - 1 ; i++ )
{
array[i] = array[i+1];
}
*n=*n-1;
}
}//end of method delete_array
void main()
{
int array[100], loc, i, n;

printf("Enter number of elements in array\n");
scanf("%d", &n);

printf("Enter %d elements\n", n);

for ( i = 0 ; i < n ; i++ )
{
scanf("%d", &array[i]);
}
printf("Enter the location from where you want to delete an element\n");
scanf("%d", &loc);
loc=loc;
delete_array(array,&n,loc);
printf("Resultant array is\n");

for( i = 0 ; i < n ; i++ )
{
printf("%d\n", array[i]);
}

getch();
}//end of main function