0% found this document useful (0 votes)
12 views4 pages

Week 8 Assignment DSA.20240330165459

The document outlines an assignment for implementing a priority queue and polynomial addition using singly linked lists. It details the algorithm for creating polynomials, displaying them, and adding two polynomials together, including the necessary C code. The conclusion reflects a better understanding of polynomial addition through this implementation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

Week 8 Assignment DSA.20240330165459

The document outlines an assignment for implementing a priority queue and polynomial addition using singly linked lists. It details the algorithm for creating polynomials, displaying them, and adding two polynomials together, including the necessary C code. The conclusion reflects a better understanding of polynomial addition through this implementation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Week 8 assignment DSA.

Objective:
Implementation of a priority queue using singly linked list.
polynomial addition using linked list. Implementation of a
priority queue using singly linked list.
Algorithm :
Start
Step1 :Define a structure Term to represent a single term in
a polynomial, containing coeff (coefficient) and exp
(exponent).
Step2 :Define a structure Poly to represent a polynomial,
containing n (number of terms) and an array of Term
structures.
Step3 : Inside create(p) Initialize a polynomial
Step4 :Read the number of terms n for the polynomial from
the user.
Step5 :Allocate memory for n terms in the polynomial.
Step6 :Read the coefficients and exponents for each term
from the user.
Step7 :Inside display(p) Iterate over each term in polynomial
Step8 :Display the coefficient and exponent of each term.
Step9 :Inside add(p1, p2) Initialize a new polynomial sum.
Step10 :Compare terms of p1 and p2 based on their
exponents and add them to sum.
If exponents are equal, add their coefficients.
Continue until all terms from both polynomials are added.
Return the sum polynomial.
Step11 :Inside Main Function Declare variables p1, p2 as
struct Poly and p3 as struct Poly*.
Step13 :Call add(&p1, &p2) to add the two polynomials and
store the result in p3.
Step14 :Display the original polynomials (p1 and p2) and
their sum (p3) using the display function
Step15 : Stop.
Code:
#include <stdio.h>
#include<stdlib.h>
struct Term
{
int coeff;
int exp;
};
struct Poly
{
int n;
struct Term *terms;
};
void create(struct Poly *p)
{
int i;
printf("Number of terms:");
scanf("%d",&p->n);
p->terms=(struct Term*)malloc(p->n*sizeof(struct
Term));
printf("Enter terms\n");
for(i=0;i<p->n;i++)
scanf("%d%d",&p->terms[i].coeff,&p->terms[i].exp);
}
void display(struct Poly p)
{
int i;
for(i=0;i<p.n;i++)
printf("%dx^%d+",p.terms[i].coeff,p.terms[i].exp);
printf("\n");
}
struct Poly *add(struct Poly *p1,struct Poly *p2)
{
int i,j,k;
struct Poly *sum;
sum=(struct Poly*)malloc(sizeof(struct Poly));
sum->terms=(struct Term
*)malloc((p1->n+p2->n)*sizeof(struct Term));
i=j=k=0;
while(i<p1->n && j<p2->n)
{
if(p1->terms[i].exp>p2->terms[j].exp)
sum->terms[k++]=p1->terms[i++];
else if(p1->terms[i].exp < p2->terms[j].exp)
sum->terms[k++]=p2->terms[j++];
else
{

for(;i<p1->n;i++)sum->terms[k++]=p1->terms[i];
for(;j<p2->n;j++)sum->terms[k++]=p2->terms[j];
sum->n=k;
return sum;
}
int main()
{
struct Poly p1,p2,*p3;
create(&p1);
create(&p2);
p3=add(&p1,&p2);
printf("\n");
printf("First polynomial is:");
display(p1);
printf("\n");
printf("Second polynomial is:");
display(p2);
printf("\n");
printf("Sum of two polynomials is:");
display(*p3);
return 0;}
Output:
Enter terms
23
32
50
Number of terms:3
Enter terms
43
-2 1
30

Conclusion: I got better idea in adding of two polynomial

You might also like