Monday, 12 May 2014

List ADT Using Array

PROGRAM:

#include<stdio.h>
#include<conio.h>
int list[5],i,n=0;
void insert(int x,int p);
void del(int p);
void display();
void main()
{
 int x,p,c;
 clrscr();
 printf("$$$ LIST ADT USING ARRAY $$$");
 while(1)
 {
  printf("\nThe operations are:");
  printf("\n1.INSERT\n2.DELETE\n3.DISPLAY\n4.EXIT\n");
  printf("Enter your choice:");
  scanf("%d",&c);
  switch(c)
  {
   case 1:printf("Enter the element and position:");
    scanf("%d%d",&x,&p);
    insert(x,p);
    break;
   case 2:printf("Enter the position of element to be deleted:");
    scanf("%d",&p);
    del(p);
    break;
   case 3:display();break;
   case 4:exit(0);
  }
 }
}
void insert(int x,int p)
{
 if(n==5)
  printf("List is full");
 else if((p<0)||(p>n))
  printf("Position is invalid");
 else
 {
  for(i=n-1;i>=p;i--)
  {
  list[i+1]=list[i];
  }
  list[p]=x;
  n++;
 }
}
void del(int p)
{
 if(n==0)
  printf("List is empty");
 else if((p<0)||(p>n-1))
  printf("position is invalid");
 else
 {
  for(i=p;i<n;i++)
  list[i]=list[i+1];
  n--;
 }
}
void display()
{
 int p;
 if(n==0)
  printf("list is empty");
 else
 {
  printf("The elements present in the list are:\n");
  printf("\nElement\tPosition\n\n");
  for(p=0;p<n;p++)
  {
   printf("%d\t%d\n\n",list[p],p);
  }
 }
}


OUTPUT:

$$$ LIST ADT USING ARRAY $$$

The operations are:
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice: 1
Enter the element and position: 11   0

The operations are:
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice: 1
Enter the element and position: 22   1

The operations are:
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice: 1
Enter the element and position: 33   2

The operations are:
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice: 1
Enter the element and position: 44   3

The operations are:
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice: 3
The elements present in the list are:
 Element     Position
     11                0
     22                1
     33                2
     44                3

The operations are:
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice: 2
Enter the position of element to be deleted: 1

The operations are:
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice: 3
The elements present in the list are:
 Element     Position
     11                0
     33                1
     44                2

The operations are:
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice: 1
Enter the element and position: 20   1

The operations are:
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice: 3
The elements present in the list are:
 Element     Position
     11                0
     20                1
     33                2
     44                3

The operations are:
1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter your choice: 4

No comments:

Post a Comment