Professional Documents
Culture Documents
Anjana S (20104014) Experiment No. 2 DS Laboratory
Anjana S (20104014) Experiment No. 2 DS Laboratory
Anjana S (20104014) Experiment No. 2 DS Laboratory
Algorithm:
Insert Element to nth Position of the LIST
1. Check whether the List is full or not
7. Free the nth Position of the list by moving all Elements to one position forward except n-
1,n-2, ... 1 Position i.e move only from n to current size position Elements. i.e New
Position=Current Position + 1.
10. Move all the Elements to one position backward except n,n-1,n-2,... 1 Position i.e
move only from n+1 to current size position Elements. i.e New Position=Current
Position - 1.
11. After the previous step, nth element will be deleted automatically.
4. Find the Position where the last element available in the List by
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define LIST_SIZE 30
void main()
int *element=NULL;
int ch,i,j,n;
int insdata,deldata,moddata,found;
int top=-1;
element=(int*)malloc(sizeof(int)* LIST_SIZE);
while(1)
fflush(stdin);
scanf("%d",&ch);
switch(ch)
case 1:
top=-1;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&element[++top]);
break;
case 2:
if(top==-1)
break;
}
scanf("%d",&moddata);
found=0;
for(i=0;i<=top;i++)
if(element[i]==moddata)
found=1;
scanf("%d",&element[i]);
break;
if(found==0)
break;
case 3:
if(top==-1)
for(i=0;i<=top;i++)
printf("\n Element[%d]is-->%d",(i+1),element[i]);
break;
case 4:
if(top==LIST_SIZE-1)
top++;
for(i=top;i>0;i--)
element[i]=element[i-1];
scanf("%d",&element[0]);
break;
case 5:
if(top==LIST_SIZE-1)
break;
scanf("%d",&element[++top]);
break;
case 6:
if(top==LIST_SIZE-1)
else if(top==-1)
else
found=0;
scanf("%d",&insdata);
for(i=0;i<=top;i++)
if(element[i]==insdata)
{
found=1;
top++;
for(j=top;j>i;j--)
element[j]=element[j-1];
scanf("%d",&element[i+1]);
break;
if(found==0)
break;
case 7:
if(top==-1)
break;
top--;
for(i=0;i<=top;i++)
element[i]=element[i+1];
break;
case 8:
if(top==-1)
else
case 9:
if(top==-1)
break;
scanf("%d",&deldata);
found=0;
for(i=0;i<=top;i++)
if(element[i]==deldata)
found=1;
top--;
for(j=i;j<=top;j++)
element[j]=element[j+1];
break;
if(found==0)
break;
default:
free(element);
exit(0);
}
}
Output:
Result:
Thus, the ‘C’ program to implement the list ADT using arrays has been executed and the output is
verified successfully.