Skip to content

Instantly share code, notes, and snippets.

@luk0y
Created March 9, 2019 10:06
Show Gist options
  • Save luk0y/997dea0b4378c49efa2587e37f20d051 to your computer and use it in GitHub Desktop.
Save luk0y/997dea0b4378c49efa2587e37f20d051 to your computer and use it in GitHub Desktop.
Merging of two double linked lists
#include<stdio.h>
#include<stdlib.h>
int create();
int start();
int start1();
int a=0;
int x,k;
struct node
{
int data;
struct node *prev,*post;
};struct node *start2=NULL,*star1,*star2,*ptr,*new_node;
void main()
{
start();
start2=NULL;
ptr=NULL;
start1();
ptr=star1;
while(ptr->post!=NULL)
{ptr=ptr->post;}
ptr->post=star2;
printf("\n\nMerged Double Linked list : \n\n");
while(star1!=NULL)
{printf("%d\n",star1->data);
star1=star1->post;}
}
int create()
{
int y;
hi:
printf("\nEnter number of nodes you want to enter : ");
scanf("%d",&x);
char ch;
if(x>0){
printf("\nEnter the data :\n\n");
while(x!=0)
{
scanf("%d",&y);
new_node=(struct node*)malloc(sizeof(struct node));
if(new_node==NULL)
{
printf("No Space");
}
else
{
if(start2==NULL)
{
start2=new_node;
new_node->data=y;
new_node->prev=NULL;
new_node->post=NULL;
}
else
{
ptr=start2;
while(ptr->post!=NULL)
{
ptr=ptr->post;
}
ptr->post=new_node;
new_node->data=y;
new_node->prev=new_node;
new_node->post=NULL;
}
}
x--;
}
a=a+1;
if(a==2){printf("\n%d'nd Linked list : \n\n",a);}else{
printf("\n%d'st Linked list : \n\n",a);}
new_node=start2;
while(new_node!=NULL)
{
printf("%d\n",new_node->data);
new_node=new_node->post;
}
}
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);
}
}
}
int start()
{ create();
star1=start2;}
int start1()
{
create();
star2=start2;}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment