VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
01 list using array
1. 1
Ex. No.: 1
Date:
Array implementation of List ADT
AIM
To write a C program for array implementation of List ADT
ALGORITHM
Step 1: Include all the header files which are used in the program.
Step 2: Declare all the user defined functions.
Step 3: Define maximum size for an array.
Step 4: Create a list initially.
Step 5: For insertion, obtain position and element from user. If the position is valid, insert
an element at the mentioned position and shift other elements accordingly. Else, display an
error message.
Step 6: For deletion, obtain position from user. If the position is valid, delete an element
from the mentioned position and shift other elements accordingly. Else, display an error
message.
Step 7: For search, obtain element to be searched from user. Compare it with other
elements of list. If match is found, display the position of an element in the list.
Step 8: Implement the main method by displaying operations menu and make suitable
function calls in the main method to perform user selected operation.
PROGRAM
#include<stdio.h>
#include<conio.h>
#define MAX 10
void create();
void insert();
void deletion();
void search();
void display();
int a,b[20], n, p, e, f, i, pos;
void main()
{
int ch;
clrscr();
while(1)
{
printf("n Main Menu");
printf("n 1.Create n 2.Delete n 3.Search n 4.Insert n 5.Displayn 6.Exit
n");
printf("n Enter your Choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
create();
break;
case 2:
deletion();
break;
case 3:
2. 2
search();
break;
case 4:
insert();
break;
case 5:
display();
break;
case 6:
exit();
break;
default:
printf("n Enter the correct choice:");
}
getch();
}
}
void create()
{
printf("n Enter the number of nodes: ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("n Enter the Element at position %d:",i);
scanf("%d", &b[i]);
}
}
void deletion()
{
printf("n Enter the position u want to delete:");
scanf("%d", &pos);
if(pos>=n)
{
printf("n Invalid Location");
goto outer;
}
else
{
for(i=pos+1;i<n;i++)
{
b[i-1]=b[i];
}
n--;
}
printf("n The list of elements after deletion aren");
for(i=0;i<n;i++)
{
printf("t%d", b[i]);
}
outer:
}
void search()
3. 3
{
int flag=0;
printf("n Enter the Element to be searched:");
scanf("%d", &e);
for(i=0;i<n;i++)
{
if(b[i]==e)
{
flag=1;
break;
}
}
if(flag==1)
printf("Value is in the %d Positionn", i);
else
printf("Value %d is not in the listn", e);
}
void insert()
{
printf("n Enter the position u need to insert:");
scanf("%d", &pos);
if(pos>=n)
{
printf("n invalid Location:");
}
else
{
for(i=MAX-1;i>=pos-1;i--)
{
b[i+1]=b[i];
}
printf("n Enter the element to insert:n");
scanf("%d",&p);
b[pos]=p;
n++;
}
printf("n The list after insertion:n");
display();
}
void display()
{
printf("n The Elements of The list ADT are:");
for(i=0;i<n;i++)
{
printf("nn%d", b[i]);
}
}
RESULT
Thus the C program for array implementation of List ADT has been executed successfully
and the output has been verified.