Write c program to concatenate 2 Linked List

#include<conio.h>
#include<stdlib.h>
#include<stdio.h>

//To create structure sll
struct sll
{
int data;
struct sll *next;
};
typedef struct sll node;

//To define functions
node * create(node *);
void concat(node * ,node *);
void display(node *);
void main()
{
int ch;
node*head1=NULL,*head2=NULL;
clrscr();
head1=create(head1);
       printf("\nFirst linked list => ");
       display(head1);
head2=create(head2);
       printf("\nSecond linked list => ");
       display(head2);
       concat(head1,head2);
       printf("\nConcatenated linked list => ");
display(head1);
getch();
}

//To create Linked List
node * create(node *head)
{
node *temp=NULL,*last=NULL;
int n,i;
printf("\nHow many nodes you want: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
temp=(node*)malloc(sizeof(node));
printf("\nEnter data: ");
scanf("%d",&temp->data);
temp->next=NULL;

if(head==NULL)
{
head=temp;
}
else
{
last=head;
while(last->next!=NULL)
{
last=last->next;
}
last->next=temp;
}
}
return head;
}

//To concatenate 2 Linked List
void concat(node*head1,node*head2)
{
       node*last=NULL;
       last=head1;
       while(last->next!=NULL)
last=last->next;
       last->next=head2;


}

//To display elements in Linked List
void display(node*head)
{
node*last=NULL;
last=head;
while(last!=NULL)
{
printf("%d ",last->data);
last=last->next;
}
}

0 Comments