Friday, 22 June 2012

write a c program to merge two sorted singly linked list of integer into third list such that the third list is also in sorted order


/*write a c program to merge two sorted singly linked list of integer
into third list such that the third list is also in sorted order*/
order*/
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
}*start1,*q1,*temp1,*start2,*q2,*temp2;
int n1,n2,*p1,*p2,i,j;
void create();
void display();
void sort_two();
void merge();
void main()
{
clrscr();
create();
display();
sort_two();
merge();
getch();
}
void create()
{
char ch='y';
while(ch=='y'||ch=='Y')
{
temp1=malloc(sizeof(struct node));
printf("\nenter the number for first L.L:-");
scanf("%d",&temp1->data);
temp1->next=NULL;
n1++;
if(start1==NULL)
start1=temp1;
else
{
q1=start1;
while(q1->next!=NULL)
{
q1=q1->next;
}
q1->next=temp1;
}
printf("\ndo you want to continue(Y|N):-");
scanf("\n%s",&ch);
}
ch='y';
while(ch=='y'||ch=='Y')
{
temp2=malloc(sizeof(struct node));
printf("\nenter the number for second L.L:-");
scanf("%d",&temp2->data);
temp2->next=NULL;
n2++;
if(start2==NULL)
start2=temp2;
else
{
q2=start2;
while(q2->next!=NULL)
{
q2=q2->next;
}
q2->next=temp2;
}
printf("\ndo you want to continue(Y|N):-");
scanf("\n%s",&ch);
}
}
void display()
{
p1=(int *)malloc(n1*sizeof(int));
p2=(int *)malloc(n2*sizeof(int));
if(start1==NULL)
printf("\nlinked list is empty");
else
{
printf("\nelement in first linked list are:-\n");
q1=start1;
for(i=0;i<n1;i++)
{
if(q1!=NULL)
{
*(p1+i)=q1->data;
printf("%d\n",*(p1+i));
q1=q1->next;
}
}
}
if(start2==NULL)
printf("\nlinked list is empty");
else
{
printf("\nelement in second linked list are:-\n");
q2=start2;
for(i=0;i<n2;i++)
{
if(q2!=NULL)
{
*(p2+i)=q2->data;
printf("%d\n",*(p2+i));
q2=q2->next;
}
}
}
}
void sort_two()
{
int i,j,t;
for(i=0;i<n1;i++)
{
for(j=i+1;j<n1;j++)
{
if(*(p1+i)>*(p1+j))
{
t=*(p1+i);
*(p1+i)=*(p1+j);
*(p1+j)=t;
}
}
}
for(i=0;i<n2;i++)
{
for(j=i+1;j<n2;j++)
{
if(*(p2+i)>*(p2+j))
{
t=*(p2+i);
*(p2+i)=*(p2+j);
*(p2+j)=t;
}
}
}
}

void merge()
{
int i,j,n,k,*p;
n=n1+n2;
p=(int *)malloc(n*sizeof(int));
for(i=j=k=0;i<n1+n2;)
{
if(*(p1+i)<*(p2+j))
{
*(p+k)=*(p1+i);
i++;
k++;
}
else
{
*(p+k)=*(p2+j);
k++;
j++;
}
if(i==n1||j==n2)
break;
}
while(i<n1)
{
*(p+k)=*(p1+i);
i++;
k++;
}
while(j<n2)
{
*(p+k)=*(p2+j);
k++;
j++;
}
printf("element of two linked list is sort by merge sort\n");
for(i=0;i<n;i++)
printf("%d\n",*(p+i));
}
/*
enter the number for first L.L:-7                                              
                                                                               
do you want to continue(Y|N):-y                                                
                                                                               
enter the number for first L.L:-1                                              
                                                                               
do you want to continue(Y|N):-y                                                
                                                                               
enter the number for first L.L:-8

do you want to continue(Y|N):-y

enter the number for first L.L:-3

do you want to continue(Y|N):-n

enter the number for second L.L:-5

do you want to continue(Y|N):-y

enter the number for second L.L:-2

do you want to continue(Y|N):-y

enter the number for second L.L:-8

do you want to continue(Y|N):-n

element in first linked list are:-
7
1
8
3

element in second linked list are:-
5
2
8
element of two linked list is sort by merge sort
1
2
3
5
7
8
8
*/

No comments: