Algorithm
INPUT: HEADER1 AND HEADER2 ARE POINTER TO THE HEADER NODE OF THE LINKEDLISTS
OUTPUT: MERGED LINKEDLIST
DATA STRUCTURE: SINGLY LINKEDLIST
STEP 1: START
STEP 2: PTR1=HEADER1->LINK ,PTR2=HEADER2->LINK ,PTR3=HEADER3->LINK
STEP 3: WHILE(PTR1!=NULL&&PTR2!=NULL)
STEP 4: NEW=NEWNODE(NODE)
STEP 4: IF(PTR1->DATA<PTR2->DATA)
STEP 5: NEW->DATA=PTR1->DATA
STEP 6: NEW->LINK=NULL
STEP 7: PTR3->LINK=NEW
STEP 8: PTR3=NEW
STEP 9: PTR1=PTR1->LINK
STEP 10: ELSE IF(PTR1->DATA==PTR2->DATA)
STEP 11: NEW->DATA=PTR2->DATA
STEP 12: NEW->LINK=NULL;
STEP 13: PTR3->LINK=NEW
STEP 14: PTR3-NEW
STEP 15: PTR1=PTR1->LINK
STEP 16: PTR2=PTR2->LINK
STEP 17: ELSE
STEP 18: NEW->DATA=PTR2->DATA
STEP 19: NEW->LINK=NULL
STEP 20: PTR3->LINK=NEW
STEP 21: PTR3=NEW
STEP 22: PTR2=PTR2->LINK
STEP 23: ENDWHILE
STEP 24: WHILE(PTR1!=NULL)
STEP 25: NEW->DATA=PTR1->DATA
STEP 26: NEW->LINK=NULL
STEP 27: PTR3->LINK=NEW
STEP 28: PTR3=NEW
STEP 29: PTR1=PTR1->LINK
STEP 30: ENDWHILE
STEP 31: WHILE(PTR2!=NULL)
STEP 32: NEW->DATA=PTR2->DATA
STEP 33: NEW->LINK=NULL
STEP 34: PTR3->LINK=NEW
STEP 35: PTR3=NEW
STEP 36: PTR2=PTR2->LINK
STEP 37: ENDWHILE
STEP 38: END
Code
#include <stdio.h>
#include <stdlib.h>
int getnode(int);
void display();
void merge();
void newnode();
int x, pos, n;
struct node
{
int data;
struct node *link;
};
struct node *prev, *head, *news, *p, *ptr,*ptr1,*ptr2,*ptr3;
struct node *q1;
struct node *header, *header1,*header3,*header2;
struct node *q, *r, *s;
int main()
{
printf("ENTER THE LIMIT OF LINKEDLIST 1: ");
scanf("%d", &n);
getnode(n);
header1=header;
printf("ENTER THE LIMIT OF LINKEDLIST 2 ");
scanf("%d", &n);
getnode(n);
header2=header;
merge();
printf("THE MERGE LIST IS:");
display(header3);
return 0;
}
void display(struct node *head1)
{
struct node *ptr;
ptr = head1->link;
// ptr=header->link;
while (ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->link;
}
}
void newnode()
{
news=malloc(sizeof(struct node));
}
void merge()
{
header3=malloc(sizeof(struct node));
header3->link=news;
ptr1=header1->link;
ptr2=header2->link;
ptr3=header3;
while(ptr1!=NULL&&ptr2!=NULL)
{
if(ptr1->data<ptr2->data)
{
newnode();
news->data=ptr1->data;
news->link=NULL;
ptr3->link=news;
ptr3=news;
ptr1=ptr1->link;
}
else if(ptr1->data==ptr2->data)
{
newnode();
news->data=ptr2->data;
news->link=NULL;
ptr3->link=news;
ptr3=news;
ptr1=ptr1->link;
ptr2=ptr2->link;
}
else{
newnode();
news->data=ptr2->data;
news->link=NULL;
ptr3->link=news;
ptr3=news;
ptr2=ptr2->link;
}
}
while(ptr1!=NULL)
{
newnode();
news->data=ptr1->data;
news->link=NULL;
ptr3->link=news;
ptr3=news;
ptr1=ptr1->link;
}
while(ptr2!=NULL)
{
newnode();
news->data=ptr2->data;
news->link=NULL;
ptr3->link=news;
ptr3=news;
ptr2=ptr2->link;
}
}
int getnode(int n)
{
header = malloc(sizeof(struct node));
head = NULL;
for (int i = 0; i < n; i++)
{
p = malloc(sizeof(struct node));
q1 = malloc(sizeof(struct node));
q = NULL;
scanf("%d", &p->data);
p->link = NULL;
if (head == NULL)
head = p;
else
prev->link = p;
prev = p;
}
header->link = head;
display(header);
return 0;
}
Output
user@computer$ : ENTER THE LIMIT OF LINKEDLIST 1: 5
1
3
5
7
9
1 3 5 7 9 ENTER THE LIMIT OF LINKEDLIST 2 5
2
4
6
8
10
2 4 6 8 10 THE MERGE LIST IS:1 2 3 4 5 6 7 8 9 10