25)Write a C program to create a two single linked list and concatenate one list at the end of another list.


#include<stdio.h>
#include<conio.h>
struct list
{
int data;
struct list *link;
};
typedef struct list NODE;
NODE *create(NODE *);
void disp(NODE *);
NODE *concat(NODE *,NODE *);
void main()
{
NODE *l1=NULL,*l2=NULL;
clrscr();
l1=create(l1);
printf(“\n\nFIrst Created List Is :\t”);
disp(l1);
l2=create(l2);
printf(“\n\nSecond Created List Is :\t”);
disp(l2);
l1=concat(l1,l2);
printf(“\nconcatenated :”);
disp(l1);
getch();
}
NODE *create(NODE *l1)
{
NODE *tmp,*q;
int n,m,i;
printf(“\n\nHOW MANY NODES U WANT”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\nEnter the Data”);
scanf(“%d”,&m);
tmp=(NODE *)malloc(sizeof(NODE));
tmp->data=m;
tmp->link=NULL;
if(l1==NULL)
{
l1=tmp;
}
else
{
q=l1;
while(q->link!=NULL)
{
q=q->link;
}
q->link=tmp;
}
}
return l1;
}
void disp(NODE *l)
{
NODE *q;
if(l==NULL)
{
printf(“\nLIST IS EMPTY”);
}
else
{
q=l;
while(q!=NULL)
{
printf(“%d->”,q->data);
q=q->link;
}
printf(“NULL”);
}
}
NODE *concat(NODE *l1,NODE *l2)
{
NODE *q,*p,*tmp;
q=l1;
p=l2;
while(q->link!=NULL)
{
q=q->link;
}
while(p!=NULL)
{
tmp=(NODE *)malloc(sizeof(NODE));
tmp->data=p->data;
tmp->link=NULL;
q->link=tmp;
q=tmp;
p=p->link;
}
return(l1);
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s