#include <stdio.h>
#include <conio.h>
//Merging two List
typedef
struct node
{
int
n;
struct
node *next;
}List;
void
Insert(List **first)
{
List
*temp,*cur;
temp=(List
*)malloc(sizeof(List));
printf("\n\tEnter
the Value : ");
scanf("%d",&temp->n);
if(*first==NULL)
{
*first=cur=temp;
temp->next=NULL;
}
else
{
for(cur=*first;cur->next!=NULL;cur=cur->next);
cur->next=temp;
temp->next=NULL;
}
}
void
disp(List *first)
{
List
*cur;
for(cur=first;cur!=NULL;cur=cur->next)
{
printf("%d ",cur->n);
}
getch();
printf("\n");
}
void merge(List *first,List **final)
{
List *cur,*temp,*pr;
for(cur=first;cur!=NULL;cur=cur->next)
{
temp=(List *)malloc(sizeof(List));
temp->n=cur->n;
temp->next=NULL;
if(*final==NULL)
{
*final=temp;
}
else
{
for(pr=*final;pr->next!=NULL;pr=pr->next);
pr->next=temp;
}
}
}
void
main()
{
int
c=0,ch=0;
List
*head,*head1,*head2;
clrscr();
head=head1=head2=NULL;
while(c!=4)
{
clrscr();
fflush(stdin);
printf("\n\n\t1. Add");
printf("\n\t2. Display");
printf("\n\t3. Merge");
printf("\n\t4. Exit");
printf("\n\tEnter your choice :
");
scanf("%d",&c);
fflush(stdin);
switch(c)
{
case
1:
while(ch!=3)
{
printf("\n\t1. List-1");
printf("\n\t2.
List-2");
printf("\n\t3.
Exit");
printf("\n\tEnter
Choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: Insert(&head);break;
case 2: Insert(&head1);break;
}
fflush(stdin);
}
break;
case
2:
ch=0;
while(ch!=3)
{
printf("\n\t1.
List-1");
printf("\n\t2.
List-2");
printf("\n\t3.
List-3");
printf("\n\tEnter
Choice : ");
scanf("%d",&ch);
fflush(stdin);
switch(ch)
{
case
1: printf("\n\tFirst List : ");
disp(head);break;
case
2: printf("\n\tSecond List :
");
disp(head1);break;
case
3: printf("\n\tMerged List :
");
disp(head2);break;
}
}
break;
case
3: merge(head,&head2);
merge(head1,&head2);
break;
case
4: printf("\n\tQuit....");
getch();
exit(0);
default:printf("\nWrong
no");
}
}
getch();
}
No comments:
Post a Comment