Write a C Program to Create Two Singly Linked List and concatenate one list at the end of another list.


#include

#include

#include

struct node

{

int data;

struct node *link;

}*head,*head1;

void create1();

void display1();

void create2();

void display2();

void concat();

void main()

{

clrscr();

head=NULL;

head1=NULL;

printf("\n Create List 1 \n");

create1();

display1();

printf("\n Create List 2 \n");

create2();

display2();

printf("\n Concated List: ");

concat();

getch();

}

void create1()

{

struct node *newnode,*prev;

int n,i;

head=NULL;

printf("\n Enter How Many Nodes?: ");

scanf("%d",&n);

for(i=1;i<=n;i++)

{

newnode=(struct node*)malloc(sizeof(struct node));

if(newnode==NULL)

{

printf("\n Insufficient Memory.");

exit(0);

}

printf("\n Enter Element %d : ",i);

scanf("%d",&newnode->data);

newnode->link=NULL;

if(head==NULL)

{

head=newnode;

prev=newnode;

}

else

{

prev->link=newnode;

prev=newnode;

}

}

}

void display1()

{

struct node *temp;

temp=head;

printf("\n Element in List 1 are: ");

while(temp!=NULL)

{

printf("\t%d",temp->data);

temp=temp->link;

}

}

void create2()

{

struct node *newnode,*prev;

int n,i;

head1=NULL;

printf("\n Enter How Many Nodes?: ");

scanf("%d",&n);

for(i=1;i<=n;i++)

{

newnode=(struct node*)malloc(sizeof(struct node));

if(newnode==NULL)

{

printf("\n Insufficient Memory.");

exit(0);

}

printf("\n Enter Element %d : ",i);

scanf("%d",&newnode->data);

newnode->link=NULL;

if(head1==NULL)

{

head1=newnode;

prev=newnode;

}

else

{

prev->link=newnode;

prev=newnode;

}

}

}

void display2()

{

struct node *temp;

temp=head1;

printf("\n Element in List 1 are: ");

while(temp!=NULL)

{

printf("\t%d",temp->data);

temp=temp->link;

}

}

void concat()

{

struct node *temp;

temp=head;

while(temp->link!=NULL)

{

temp=temp->link;

}

temp->link=head1;

temp=head;

while(temp!=NULL)

{

printf("\t%d",temp->data);

temp=temp->link;

}

}


/*OUTPUT

Create List 1


Enter How Many Nodes?: 3


Enter Element 1 : 2


Enter Element 2 : 1


Enter Element 3 : 3


Element in List 1 are:         2       1       3

Create List 2


Enter How Many Nodes?: 4


Enter Element 1 : 8


Enter Element 2 : 4


Enter Element 3 : 2


Enter Element 4 : 1


Element in List 1 are:         8       4       2       1

Concated List:         2       1       3       8       4       2       1 */
Write a C Program to Create Two Singly Linked List and concatenate one list at the end of another list. Write a C Program to Create Two Singly Linked List and concatenate one list at the end of another list. Reviewed by on November 17, 2013 Rating: 5
Powered by Blogger.