0% found this document useful (0 votes)
7 views3 pages

Polynomial Addition in C Code

The document contains a C program that implements a polynomial data structure using linked lists. It provides functions to create polynomials, insert terms, display the polynomial, and add two polynomials together. The program prompts the user to input coefficients and exponents, then outputs the resulting polynomial after addition.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Polynomial Addition in C Code

The document contains a C program that implements a polynomial data structure using linked lists. It provides functions to create polynomials, insert terms, display the polynomial, and add two polynomials together. The program prompts the user to input coefficients and exponents, then outputs the resulting polynomial after addition.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

09_poly.

1 #include <stdio.h>
2 #include <stdlib.h>
3
4 struct node{
5 float coeff;
6 int expo;
7 struct node* link;
8 };
9
10 struct node* insert(struct node* head, float coe, int exp)
11 {
12 struct node* temp;
13 struct node* newP = malloc(sizeof(struct node));
14 newP->coeff = coe;
15 newP->expo = exp;
16 newP->link = NULL;
17
18 if(head == NULL || exp > head->expo)
19 {
20 newP->link = head;
21 head = newP;
22 }
23 else
24 {
25 temp = head;
26 while(temp->link != NULL && temp->link->expo >= exp)
27 temp = temp->link;
28
29 newP->link = temp->link;
30 temp->link = newP;
31 }
32 return head;
33 }
34
35 struct node* create(struct node* head)
36 {
37 int n, i;
38 float coeff;
39 int expo;
40
41 printf("Enter the number of terms: ");
42 scanf("%d", &n);
43
44 for(i = 0; i < n; i++)
45 {
46 printf("Enter coefficient for term %d: ", i+1);
47 scanf("%f", &coeff);
48
49 printf("Enter exponent for term %d: ", i+1);
50 scanf("%d", &expo);
51
52 head = insert(head, coeff, expo);
53 }
54 return head;
55 }
56
57 void display(struct node* head)
58 {
59 if(head == NULL)
60 printf("No Polynomial");
61 else
62 {
63 while(head != NULL)
64 {
65 printf("%.1fx^%d", head->coeff, head->expo);
66 head = head->link;
67 if(head != NULL)
68 printf(" + ");
69 }
70 printf("\n");
71 }
72 }
73
74 void polyAdd(struct node* head1, struct node* head2)
75 {
76 struct node* ptr1 = head1;
77 struct node* ptr2 = head2;
78 struct node* head3 = NULL;
79
80 while(ptr1 != NULL && ptr2 != NULL)
81 {
82 if(ptr1->expo == ptr2->expo)
83 {
84 head3 = insert(head3, ptr1->coeff + ptr2->coeff, ptr1->expo);
85 ptr1 = ptr1->link;
86 ptr2 = ptr2->link;
87 }
88 else if(ptr1->expo > ptr2->expo)
89 {
90 head3 = insert(head3, ptr1->coeff, ptr1->expo);
91 ptr1 = ptr1->link;
92 }
93 else
94 {
95 head3 = insert(head3, ptr2->coeff, ptr2->expo);
96 ptr2 = ptr2->link;
97 }
98 }
99
100 while(ptr1 != NULL)
101 {
102 head3 = insert(head3, ptr1->coeff, ptr1->expo);
103 ptr1 = ptr1->link;
104 }
105
106 while(ptr2 != NULL)
107 {
108 head3 = insert(head3, ptr2->coeff, ptr2->expo);
109 ptr2 = ptr2->link;
110 }
111
112 printf("Added polynomial is: ");
113 display(head3);
114 }
115
116 int main()
117 {
118 struct node* head1 = NULL;
119 struct node* head2 = NULL;
120
121 printf("Enter the first polynomial\n");
122 head1 = create(head1);
123
124 printf("Enter the second polynomial\n");
125 head2 = create(head2);
126
127 polyAdd(head1, head2);
128 return 0;
129 }
130
131 /*
132 Ouput:
133 Enter the first polynomial
134 Enter the number of terms: 3
135 Enter coefficient for term 1: 5
136 Enter exponent for term 1: 3
137 Enter coefficient for term 2: 4
138 Enter exponent for term 2: 2
139 Enter coefficient for term 3: 2
140 Enter exponent for term 3: 0
141 Enter the second polynomial
142 Enter the number of terms: 3
143 Enter coefficient for term 1: 3
144 Enter exponent for term 1: 3
145 Enter coefficient for term 2: 1
146 Enter exponent for term 2: 2
147 Enter coefficient for term 3: 6
148 Enter exponent for term 3: 1
149 Added polynomial is: 8.0x^3 + 5.0x^2 + 6.0x^1 + 2.0x^0
150
151 Explanation:
152 (5x³ + 4x² + 2)
153 + (3x³ + 1x² + 6x)
154 -------------------
155 8x³ + 5x² + 6x + 2
156 */

You might also like