Algorithm
INPUT: HEADER1 AND HEADER2 ARE POINTER TO THE HEADER NODE OF THE LINKEDLISTS
OUTPUT: MERGED LINKEDLIST POLYNOMIAL RESULT
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->EXP<PTR2->EXP)
STEP 5: NEW->EXP=PTR1->EXP
STEP 6: NEW->COEFF=PTR1->COEFF+PTR2->COEFF
STEP 7: NEW->LINK=NULL
STEP 8: PTR3->LINK=NEW
STEP 9: PTR3=NEW
STEP 10: PTR1=PTR1->LINK
STEP 11: ELSE IF(PTR1->EXP==PTR2->EXP)
STEP 12: NEW->EXP=PTR2->EXP
STEP 13: NEW->COEFF=PTR2->COEFF
STEP 14: NEW->LINK=NULL;
STEP 15: PTR3->LINK=NEW
STEP 16: PTR3-NEW
STEP 17: PTR1=PTR1->LINK
STEP 18: PTR2=PTR2->LINK
STEP 19: ELSE
STEP 20: NEW->EXP=PTR2->EXP
STEP 21: NEW->COEFF=PTR2->COEFF
STEP 22: NEW->LINK=NULL
STEP 23: PTR3->LINK=NEW
STEP 24: PTR3=NEW
STEP 25: PTR2=PTR2->LINK
STEP 26: ENDWHILE
STEP 27: WHILE(PTR1!=NULL)
STEP 28: NEW->DATA=PTR1->DATA
STEP 29: NEW->LINK=NULL
STEP 30: PTR3->LINK=NEW
STEP 31: PTR3=NEW
STEP 32: PTR1=PTR1->LINK
STEP 33: ENDWHILE
STEP 34: WHILE(PTR2!=NULL)
STEP 35: NEW->DATA=PTR2->DATA
STEP 36: NEW->LINK=NULL
STEP 37: PTR3->LINK=NEW
STEP 38: PTR3=NEW
STEP 39: PTR2=PTR2->LINK
STEP 40: ENDWHILE
STEP 41: END
Code
#include <stdio.h>
#include <stdlib.h>
int getnode(int);void display();void merge();void newnode();
int x, pos, n;
struct node
{
int coeff;
int exp;
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 POLYNOMIAL 1: ");
scanf("%d", &n);
getnode(n);
header1=header;
printf("ENTER THE LIMIT OF POLYNOMIAL 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->coeff);
printf("%d",ptr->exp);
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->exp<ptr2->exp)
{
newnode();
news->exp=ptr1->exp;
news->coeff=ptr1->coeff+ptr2->coeff;
news->link=NULL;
ptr3->link=news;
ptr3=news;
ptr1=ptr1->link;
}
else if(ptr1->exp==ptr2->exp)
{
newnode();
news->exp=ptr2->exp;
news->coeff=ptr2->coeff;
news->link=NULL;
ptr3->link=news;
ptr3=news;
ptr1=ptr1->link;
ptr2=ptr2->link;
}
else{
newnode();
news->exp=ptr2->exp;
news->coeff=ptr2->coeff;
news->link=NULL;
ptr3->link=news;
ptr3=news;
ptr2=ptr2->link;
}
}
while(ptr1!=NULL)
{
newnode();
news->exp=ptr1->exp;
news->coeff=ptr1->coeff;
news->link=NULL;
ptr3->link=news;
ptr3=news;
ptr1=ptr1->link;
}
while(ptr2!=NULL)
{
newnode();
news->exp=ptr2->exp;
news->coeff=ptr2->coeff;
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;
printf("enter the coefficient");
scanf("%d", &p->coeff);
printf("enter the degree");
scanf("%d",&p->exp);
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 POLYNOMIAL 1: 3
enter the coefficient1
enter the degree2
enter the coefficient3
enter the degree3
enter the coefficient4
enter the degree4
1 23 34 4ENTER THE LIMIT OF POLYNOMIAL 2 3
enter the coefficient5
enter the degree5
enter the coefficient6
enter the degree6
enter the coefficient7
enter the degree8
5 56 67 8THE MERGE LIST IS:6 28 39 45 56 67 8