Showing posts with label List ADT. Show all posts
Showing posts with label List ADT. Show all posts

Friday, 16 May 2014

Merging Two List Using Array

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,m,n,a[20],b[20],c[20];
clrscr();
printf("$$$ MERGING TWO LIST USING ARRAY $$$");
printf("\nEnter the size of the first array:");
scanf("%d",&n);
printf("Enter first sorted array elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter the size of the second array:");
scanf("%d",&m);
printf("Enter second sorted array elements:");
for(j=0;j<m;j++)
{
scanf("%d",&b[j]);
}
i=j=k=0;
while(i<n && j<m)
{
if(a[i]<b[j])
{
c[k]=a[i];
k++;
i++;
}
else
{
c[k]=b[j];
k++;
j++;
}
}
while(i<n)
{
c[k]=a[i];
k++;
i++;
}
while(j<m)
{
c[k]=b[j];
k++;
j++;
}
printf("Merging of two sorted array is:");
for(k=0;k<n+m;k++)
{
printf("%d\t",c[k]);
}
getch();
}

OUTPUT:

$$$ MERGING TWO LIST USING ARRAY $$$

Enter the size of the first array:4
Enter first sorted array elements:    12    23    35    47

Enter the size of the second array:3
Enter second sorted array elements:    27    68    77

Merging of two sorted array is:    12       23      27      35      47      68     77

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