ASSIGNMENT 13 CS 392 : TCPTUTOR

14
190

PROBLEM STATEMENT

Write a C program to add two polynomials using Single Linked List (SLL).

ALGORITHMS

Algorithm to add two polynomials using linked list:

Let p and q be the two polynomials represented by linked lists

Step 1: While p and q are not null, repeat step 2.
Step 2: If powers of the two terms are equal
.               .then if the terms do not cancel
.               .then insert the sum of the terms into the sum Polynomial
Step 3: goto next node of p
Step 4: goto next node of q
Step 5: Else if the power of the first polynomial> power of second
.               .Then insert the term from first polynomial into sum polynomial
Step 6: goto next node of p

.               .Else insert the term from second polynomial into sum polynomial
Step 7: goto next node of q
Step 8: copy the remaining terms from the non empty polynomial into the sum polynomial.
Step 9: Repeat step 8 of the algorithm till the end of the polynomials has not been reached.
Step 10: EXIT

SOURCE CODE

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int exp;
int num;
struct node *link;
};
struct node *start1=NULL,*start2=NULL,*start3=NULL;
struct node* createnode()
{
struct node *n;
n=(struct node*)malloc(sizeof(struct node));
return(n);
}
int set(struct node *temp)
{
struct node *ptr;
ptr=createnode();
float store;
if(start1==NULL)
start1=temp;
else
{
while(ptr->link!=NULL)
{
if(ptr->exp<=temp->exp)
{
ptr=ptr->link;
continue;
}
else if(ptr->exp==temp->exp)
{
ptr->num=ptr->num+temp->num;
ptr=ptr->link;
continue;
}
else
{
ptr->num=store;
ptr->num=temp->num;
temp->num=store;
ptr->exp=store;
ptr->exp=temp->exp;
temp->exp=store;
}
ptr=ptr->link;
}
ptr->link=temp;
}
return 0;
}
int set2(struct node *temp)
{
struct node *ptr;
ptr=createnode();
float store;
if(start2==NULL)
start2=temp;
else
{
while(ptr->link!=NULL)
{
if(ptr->exp<temp->exp)
{
ptr=ptr->link;
continue;
}
else if(ptr->exp==temp->exp)
{
ptr->num=ptr->num+temp->num;
ptr=ptr->link;
continue;
}
else
{
ptr->num=store;
ptr->num=temp->num;
temp->num=store;
ptr->exp=store;
ptr->exp=temp->exp;
temp->exp=store;
}
ptr=ptr->link;
}
ptr->link=temp;
}
return 0;
}
int final(struct node *ptr)
{
struct node *temp,*t;
temp=createnode();
temp->exp=ptr->exp;
temp->num=ptr->num;
temp->link=NULL;
if(start3==NULL)
start3=temp;
else
{
while(t->link!=NULL)
{
t=t->link;
}
t->link=temp;
}
return 0;
}
int add()
{
struct node *ptr1,*ptr2,*pptr1,*pptr2,*temp,*t;
ptr1=start1;
ptr2=start2;
while(ptr1->link!=NULL)
{
while(ptr2->link!=NULL)
{
if(ptr1->exp<ptr2->exp)
{
final(ptr2);
}
else if(ptr1->exp>ptr2->exp)
{
final(ptr1);
}
else if(ptr1->exp==ptr2->exp);
{
temp=createnode();
temp->exp=ptr1->exp;
temp->num=ptr1->num+ptr2->num;
temp->link=NULL;
if(start3==NULL)
start3=temp;
else
{
while(t->link!=NULL)
{
t=t->link;
}
t->link=temp;
}
}
}
}
}
int main()
{
int n1,n2,i;
struct node *temp,*temp2,*t;
printf(“Enter the number of elements in first expression:”);
scanf(“%d”,&n1);
for(i=0;i<n1;i++)
{
temp=createnode();
printf(“Enter the number:”);
scanf(“%d”,&temp->num);
printf(“Enter the Exponent:”);
scanf(“%d”,&temp->exp);
temp->link=NULL;
set(temp);
}
printf(“Enter the number of elements in second expression:”);
scanf(“%d”,&n2);
for(i=0;i<n2;i++)
{
temp2=createnode();
printf(“Enter the number:”);
scanf(“%d”,&temp2->num);
printf(“Enter the exponent:”);
scanf(“%d”,&temp2->exp);
temp2->link=NULL;
set2(temp2);
}
add();
start3=t;
printf(“Sum=”);
while(t->link!=NULL)
{
printf(“%d X^%d+”,t->num,t->exp);
}
return 0;
}

OUTPUT

Enter the number of elements in first expression:3
Enter the number:5
Enter the Exponent:2
Enter the number:4
Enter the Exponent:1
Enter the number:2
Enter the Exponent:0
Enter the number of elements in first expression:2
Enter the number:5
Enter the Exponent:1
Enter the number:5
Enter the Exponent:0
Sum=5X^2+9X^1+7X^0

14 COMMENTS

  1. I just want to tell you that I am beginner to blogs and really loved you’re blog site. Probably I’m planning to bookmark your blog post . You absolutely come with terrific well written articles. Regards for sharing with us your webpage.

  2. Excellent website you have here but I was curious if you knew of any message boards that cover the same topics talked about in this article? I’d really love to be a part of group where I can get advice from other knowledgeable people that share the same interest. If you have any recommendations, please let me know. Cheers!

  3. Restylane乃一系列用於美顏的長效透明質酸產品。 Restylane療程能有效撫平皺紋、美化臉型輪廓及唇部,並可改善膚質及肌膚的彈性。Restylane透明質酸作用原理 Restylane透明質酸均以非動物來源的透明質酸作為原料,獨有的NASHA™專利技術研製而成。微量Restylane透明質酸凝膠到皮膚中,可以讓您的面部肌膚恢復豐盈飽滿。 Restylane透明質酸中的透明質酸與人體內的天然透明質酸十分相似,使用前無需進行皮膚測試,引起過敏反應的機率亦極低。

  4. I’ve learned quite a few important things as a result of your post. I’d also like to express that there may be a situation that you will have a loan and never need a cosigner such as a Federal Student Support Loan. However, if you are getting a borrowing arrangement through a common loan company then you need to be made ready to have a cosigner ready to assist you to. The lenders are going to base that decision over a few variables but the largest will be your credit rating. There are some creditors that will in addition look at your work history and decide based on that but in many cases it will hinge on your score.

LEAVE A REPLY

Please enter your comment!
Please enter your name here