Showing posts with label Arrays In Data Structure. Show all posts
Showing posts with label Arrays In Data Structure. Show all posts

Tuesday, March 22, 2011

Program for Merging of Two Sorted Lists with Example & Explanation


Program

#include<stdio.h>
#include<conio.h>
void main()
{
   void read(int *,int);
   void dis(int *,int);
   void sort(int *,int);
   void merge(int *,int *,int *,int);
   int a[5],b[5],c[10];
   clrscr();
   printf("Enter the elements of first list \n");
   read(a,5);      /*read the list*/
   printf("The elements of first list are \n");
   dis(a,5);  /*Display the first list*/
   printf("Enter the elements of second list \n");
   read(b,5);   /*read the list*/
   printf("The elements of second list are \n");
   dis(b,5);  /*Display the second list*/
   sort(a,5);
   printf("The sorted list a is:\n");
   dis(a,5);
   sort(b,5);
   printf("The sorted list b is:\n");
   dis(b,5);

   merge(a,b,c,5);
   printf("The elements of merged list are \n");
   dis(c,10);  /*Display the merged list*/
   getch();
}
void read(int c[],int i)
{
   int j;
   for(j=0;j<i;j++)
      scanf("%d",&c[j]);
   fflush(stdin);
}

void dis(int d[],int i)
{
   int j;
   for(j=0;j<i;j++)
      printf("%d ",d[j]);
   printf("\n");
}
void sort(int arr[] ,int k)
{
   int temp;
   int i,j;
   for(i=0;i<k;i++)
   {
      for(j=0;j<k-i-1;j++)
      {
         if(arr[j]<arr[j+1])
         {
            temp=arr[j];
            arr[j]=arr[j+1];
            arr[j+1]=temp;
         }
      }
   }
}
void merge(int a[],int b[],int c[],int k)
{
   int ptra=0,ptrb=0,ptrc=0;
   while(ptra<k && ptrb<k)
   {
      if(a[ptra] < b[ptrb])
      {
            c[ptrc]=a[ptra];
         ptra++;
      }
      else
      {
            c[ptrc]=b[ptrb];
         ptrb++;
      }
      ptrc++;
   }
   while(ptra<k)
   {
      c[ptrc]=a[ptra];
      ptra++;ptrc++;
   }
   while(ptrb<k)
   {
      c[ptrc]=b[ptrb];
      ptrb++;  ptrc++;
   }
}

Example

Input
Enter the elements of the first list
10 20 25 50 63
Output
The elements of first list are
20 25 50 63
Input
Enter the elements of the second list
16 62 68 80
Output
The elements of second list are
12 16 62 68 80
The sorted list a is
63 50 25 20 10
The sorted list b is
80 68 62 16 12
The elements of the merged list are
80 68 63 62 50 25 20 16 12 10

Explanation

  1. ptra=0, ptrb=0, ptrc=0;
  2. If the element in the first list pointed to by ptra is greater than the element in the second list pointed to by ptrb, place the element of the first list in the resultant list at the index equal to ptrc. Increment ptra or ptrc by one, or else place the element of the second list in the resultant list at the index equal to ptrc. Increment ptrb and ptrc by 1. Repeat this step until ptra is greater than the number of terms in the first list and ptrb is greater than the number of terms in the second list.
  3. If the first list has any elements, place one in the resultant list pointed to by ptrc, and increment ptra and ptrc. Repeat this step until ptra is greater than the number of terms in the first list.
  4. If the second list has any elements, place one in the resultant list pointed to by ptrc, and increment ptrb and ptrc. Repeat this step until ptrb is greater than the number of terms in the first list.

Program for Inverse of the list with Example-2


This is another version of an inverse program, in which another list is used to hold the reversed list.
#include<stdio.h>
#include<conio.h>
void main()
{
   void read(int *,int);
   void dis(int *,int);
   void inverse(int *,int *,int);
   int a[5],b[5];
   clrscr();
   read(a,5);
   dis(a,5);
   inverse(a,b,5);
   dis(b,5);
   getch();
}

void read(int c[],int i)
{
   int j;
   printf("Enter the list \n");
   for(j=0;j<i;j++)
      scanf("%d",&c[j]);
   fflush(stdin);
}
void dis(int d[],int i)
{
   int j;
   printf("The list is \n");
   for(j=0;j<i;j++)
      printf("%d ",d[j]);
   printf("\n");
}
void inverse(int a[],int inverse_b[],int j)
{
   int i,k;
   k=j-1;
   for(i=0;i<j;i++)
   {
      inverse_b[i]=a[k];
      k-;
   }
}

Example

Input
Enter the list
10
20
30
40
50
Output
The list is
10 20 30 40 50
The inverse of the list is
50 40 30 20 10

Program for Inverse of the list with Example


The following program makes a reverse version of the list.
#include<stdio.h>
#include<conio.h>
void main()
{
   void read(int *,int);
   void   dis(int *,int);
   void  inverse(int *,int);

   int a[5],i;
   clrscr();
   read(a,5);
   dis(a,5);
   inverse(a,5);
   dis(a,5);
   getch();
}

void read(int c[],int i)
{
   int j;
   printf("Enter the list \n");
   for(j=0;j<i;j++)
      scanf("%d",&c[j]);
   fflush(stdin);
}
void dis(int d[],int i)
{
    int j;
   printf("The list is \n");
   for(j=0;j<i;j++)
   printf("%d ",d[j]);
   printf("\n");
}
void inverse(int inver_a[],int j)
{
   int i,temp;
   j-;
   for(i=0;i<(j/2);i++)
   {
      temp=inver_a[i];
      inver_a[i]=inver_a[j];
      inver_a[j]=temp;
      j-;
   }
}

Example

Input
Enter the list
10
20
30
40
50
Output
The list is
10 20 30 40 50
The inverse of the list is
50 40 30 20 10

Program for Addition of the two lists in with Example


#include<stdio.h>
   #include<conio.h>
   void main()
   {
    void read(int *,int);
    void dis(int *,int);
    void add(int *,int *,int * ,int);
    int a[5],b[5],c[5],i;

    clrscr();
    printf("Enter the elements of first list \n");
    read(a,5);       /*read the first list*/
    printf("The elements of first list are \n");
    dis(a,5);  /*Display the first list*/
    printf("Enter the elements of second list \n");
    read(b,5);      /*read the second list*/
    printf("The elements of second list are \n");
    dis(b,5);  /*Display the second list*/
    add(a,b,c,i);
    printf("The resultant list is \n");
    dis(c,5);
    getch();
   }

   void add(int a[],int b[],int c[],int i)
   {
    for(i=0;i<5;i++)
      {
       c[i]=a[i]+b[i];
      }
   }
   void read(int c[],int i)
   {
    int j;
    for(j=0;j<i;j++)
    scanf("%d",&c[j]);
    fflush(stdin);
   }

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

Explanation

  1. Repeat step (2) for i=0,1,2,… (n−1), where n is the maximum number of elements in a list.
  2. c[i] = a[i]+b[i], where a is the first list, b is the second list, and c is the resultant list; a[i] denotes the ith element of list a.

Example

Input
Enter the elements of the first list
1
2
3
4
5
Output
The elements of the first list are
2 3 4 5
Input
Enter the elements of the second list
6
7
8
9
10
Output
The elements of the second list are
6 7 8 9 10
The resultant list is
7 9 11 13 15

Program For Manipulations on the list implemented using an array in Data Structure with example

Program
ADDITION OF THE ELEMENTS OF THE LIST
#include<stdio.h>
#include<conio.h>
void main()
{
   void read(int *,int);
   void dis(int *,int);
   int a[5],i,sum=0;

   clrscr();
   printf("Enter the elements of list \n");
   read(a,5);       /*read the list*/
   printf("The list elements are \n");
   dis(a,5);
   for(i=0;i<5;i++)
   {
      sum+=a[i];
   }
   printf("The sum of the elements of the list is %d\n",sum);
   getch();
}

void read(int c[],int i)
{
   int j;
   for(j=0;j<i;j++)
   scanf("%d",&c[j]);
   fflush(stdin);
}

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

Example

Input
Enter the elements of the first array
15
30
45
60
75
Output
The elements of the first array are
15 30 45 60 75
The sum of the elements of an array is 225.

Program for Application of Arrays in Data Structure with Example

//Implementation of a Static Contiguous List

Program

#include<stdio.h>

#include<conio.h>
void main()
{
   void read(int *,int);
   void dis(int *,int);
   int a[5],i,sum=0;

   clrscr();
   printf("Enter the elements of array \n");
   read(a,5);      /*read the array*/
   printf("The array elements are \n");
   dis(a,5);
}

void read(int c[],int i)
{
   int j;
   for(j=0;j<i;j++)
   scanf("%d",&c[j]);
   fflush(stdin);
}

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

Example

Input
Enter the elements of the first array
15
30
45
60
75
Output
The elements of the first array are
15 30 45 60 75