Skip to content

Instantly share code, notes, and snippets.

@luk0y
Created March 9, 2019 10:04
Show Gist options
  • Save luk0y/4cceecc130b93743be7bb29221a90130 to your computer and use it in GitHub Desktop.
Save luk0y/4cceecc130b93743be7bb29221a90130 to your computer and use it in GitHub Desktop.
Merging of two Linked lists
#include<stdio.h>
#include<stdlib.h>
void create();
void start1();
void start2();
int a=0;
struct node
{
int data;
struct node *adr;
};struct node *start=NULL,*star1,*star2,*ptr;int n=0,y,z;
void main()
{
start1();
start=NULL;
start2();
ptr=star1;
while(ptr->adr!=NULL)
{ptr=ptr->adr;}
ptr->adr=star2;
printf("\n\nMerged Linked list : \n\n");
while(star1!=NULL)
{printf("%d\n",star1->data);
star1=star1->adr;}
}
void create()
{struct node *nn;
hi:
printf("\nEnter number of nodes you want : ");
char ch;
int x,y;
scanf("%d",&y);
if(y>0){
printf("\nEnter the data : \n");
while(y!=0)
{
scanf("%d",&x);
nn=(struct node*) malloc(sizeof(struct node));
if(nn==NULL)
{
printf("no space");
}
else
{
if(start==NULL)
{
start=nn;
nn->data=x;
nn->adr=NULL;
}
else
{
ptr=start;
while(ptr->adr!=NULL)
{
ptr=ptr->adr;
}
ptr->adr=nn;
nn->data=x;
nn->adr=NULL;
}
}
y--;
}
ptr=start;
a=a+1;
if(a==1){
printf("\n%d'st Linked list : \n",a);}
else{printf("\n%d'nd Linked list : \n",a);}
while(ptr!=NULL)
{ printf("%d\n",ptr->data);
ptr=ptr->adr;
}
printf("\n");
}
else
{
printf("\nNo node created...Enter y to create list again or else enter any other for exit : \n");
scanf(" %c",&ch);
if(ch=='y')
{
goto hi;
}
else
{
printf("\nNo merging will be performed without two linked lists \n");
exit(0);
}
}
}
void start1()
{ create();
star1=start;
}
void start2()
{
create();
star2=start;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment