Program
: Concatenation of two singly linked list
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct
link
{
int no;
struct link *next;
};
struct
link *list1,*list2,*list3;
int i;
void
create_link(struct link *node);
void
display(struct link *node);
void
concat_link(struct link *list1,struct link *list2, struct link *list3);
void
main()
{
clrscr();
list1=(struct link
*)malloc(sizeof(struct link));
list2=(struct link
*)malloc(sizeof(struct link));
list3=(struct link
*)malloc(sizeof(struct link));
printf("\n Create list -1
");
create_link(list1);
printf("\n Create list -2 ");
create_link(list2);
printf("\n Output List -1\n");
display(list1);
printf("\n Output List -2\n");
display(list2);
concat_link(list1,list2,list3);
printf("\n List after concatenation \n");
display(list3);
getch();
}
void
create_link(struct link *node)
{
char ans;
i=0;
fflush(stdin);
printf("\n Enter 'n' for
break:-");
ans=getchar();
while(ans!='n')
{
printf("\n Enter data
for node:-");
scanf("%d",&node->no);
node->next=(struct link
*)malloc(sizeof(struct link));
node=node->next;
node->next=NULL;
fflush(stdin);
printf("\n Enter 'n' for
break:-");
ans=getchar();
i++;
}
printf("\n Total number of nodes : %d ", i);
}
void
display(struct link *node)
{
while(node->next)
{
printf("\n %d",node->no);
node=node->next;
}
}
void
concat_link(struct link *list1,struct link *list2, struct link *list3)
{
// copy first list
while(list1->next!=NULL)
{
list3->no=list1->no;
list1=list1->next;
list3->next=(struct link
*)malloc(sizeof(struct link));
list3=list3->next;
}
// copy
second list
while(list2->next!=NULL)
{
list3->no=list2->no;
list2=list2->next;
list3->next=(struct link
*)malloc(sizeof(struct link));
list3=list3->next;
}
list3->next=NULL;
}
No comments:
Post a Comment