0% found this document useful (0 votes)
40 views6 pages

#Include #Include #Include: "Memory Not Allocated/n"

This document defines functions for representing and manipulating polynomials using a circular linked list data structure. It includes functions to create nodes, attach terms to a polynomial, read a polynomial from user input, add two polynomials together by comparing exponents, and display a polynomial by traversing the linked list. These functions are used in a main function to read in two polynomials from the user, add them together, and display the original and resulting polynomials.
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)
40 views6 pages

#Include #Include #Include: "Memory Not Allocated/n"

This document defines functions for representing and manipulating polynomials using a circular linked list data structure. It includes functions to create nodes, attach terms to a polynomial, read a polynomial from user input, add two polynomials together by comparing exponents, and display a polynomial by traversing the linked list. These functions are used in a main function to read in two polynomials from the user, add them together, and display the original and resulting polynomials.
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
You are on page 1/ 6

1: #include<stdio.

h>
2: #include<conio.h>
3: #include<stdlib.h>
4:
5: //Structure Definition
6: struct node
7: {
8: int coeff;
9: int exp;
10: struct node *link;
11: };
12:
13: typedef struct node *NODE;
14:
15: //Getnode function
16: NODE getnode()
17: {
18: NODE x;
19: x=(NODE)malloc(sizeof(struct node));
20: if(x==NULL)
21: {
22: printf("Memory not allocated\n");
23: return 0;
24: }
25: return x;
26: }
27:
28: //Function for Attaching terms to polynomial
29: //Similar to insert rear
30: //Takes coeff,exp and head as the argument
31:
32: NODE attach(int coeff,int exp,NODE head)
33: {
34: NODE temp,cur;
35:
36: //temp getnode
37: //put coeff in temp->coeff
38: //expin temp ->exp
39: temp=getnode();
40: temp->coeff=coeff;
41: temp->exp=exp;
42:
43: //Make cur point to head->link
44: cur=head->link;
45:
46: //While cur of link is not equal to head node
47: //keep on updating cur=cur->link
48: //Once it comes out of loop
49: //Make cur->link equals(point) to temp cur>>temp
50: //and temp->link equals(point) to head temp>>head
51: //Since it is singly circular linked list
52:
53: while(cur->link!=head)
54: {
55: cur=cur->link;
56: }
57: cur->link=temp;
58: temp->link=head;
59:
60: //return the head
61: return head;
62: }
63:
64: //Function to read polynomial takes head as the argument
65: NODE read_poly(NODE head)
66: {
67: int coeff,exp,i;
68:
69: /*In an infinite unconditional for loop i=1(first term...)
70: scan coeffecient
71: check if coeff is -999 then break
72: scan exponent
73: attach coeff,exp and head to head itself
74: */
75:
76: printf("Enter the coefficient -999 to end the polynomial\n");
77: for(i=1;;i++)
78: {
79: printf("Enter the %d term\n",i);
80: printf("Enter the coefficient: ");
81: scanf("%d",&coeff);
82: if(coeff==-999) break;
83: printf("Enter the power of x: ");
84: scanf("%d",&exp);
85: head=attach(coeff,exp,head);
86: }
87:
88: //return the head
89: return head;
90: }
91:
92: //Function add two polynomials
93: //takes 3 header nodes
94: //h1,h2,h3-first,second,third polynomial
95:
96: NODE add_poly(NODE h1, NODE h2, NODE h3 )
97: {
98: NODE a,b;
99: int coeff,val;
100:
101: /*Make 'a' point to head1->link
102: 'b' point to head2->link
103: */
104:
105: a=h1->link;
106: b=h2->link;
107:
108: /*while a and b are not equal to their head nodes h1 and h2
109: Compare the exponents of a and b,
110: Assign values to val
111: */
112: while(a!=h1 && b!=h2)
113: {
114: if(a->exp==b->exp)
115: {
116: val=0;
117: }
118: else if(a->exp>b->exp)
119: {
120: val=1;
121: }
122: else
123: {
124: val=2;
125: }
126:
127: //Compare the value of val using switch case
128: //And attach to h3 accordingly
129: switch(val)
130: {
131: /*Case 0 i.e when exponents of both a and b are equal:
132: >We add the coeff of a and b store it in coeff
133: >If coeff does not becomes 0 the we will attach
134: coeff exp of a and h3 to h3
135: >Update links of a and b
136: */
137: case 0: coeff=a->coeff+b->coeff;
138: if(coeff!=0){
139: h3=attach(coeff,a->exp,h3);
140: }
141: a=a->link;
142: b=b->link;
143: break;
144:
145: /*Case 1 i.e when a's exp is greater than b's exp:
146: >Just attach a's coeff and exp to h3
147: .Update only a's link
148: */
149: case 1: h3=attach(a->coeff,a->exp,h3);
150: a=a->link;
151: break;
152:
153: /*
154: By default attach b's coeff and exp to h3
155: and update only b's link
156: */
157: default: h3=attach(b->coeff,b->exp,h3);
158: b=b->link;
159: break;
160: }
161: //End of switch
162: }
163: //End of while
164:
165: //Finally while a is not equal to h1
166: //Attach a's coeff and exp to h3
167: //update a's link
168: while(a!=h1)
169: {
170: h3=attach(a->coeff,a->exp,h3);
171: a=a->link;
172: }
173:
174: //Similarly do for b
175: while(b!=h2)
176: {
177: h3=attach(b->coeff,b->exp,h3);
178: b=b->link;
179: }
180:
181: //return h3(result)
182: return h3;
183: }
184:
185: //Function to display polynomial
186: void display(NODE head)
187: {
188: NODE temp;
189:
190: //First check if polynomial is empty
191: if(head->link==head)
192: {
193: printf("Polynomial empty\n");
194: return ;
195: }
196:
197: //Then make temp point to head and
198: //While temp does not become head
199: //Check if temp's coeff is < or > 0 (print +) and print
accordingly
200: temp=head->link;
201: while(temp!=head)
202: {
203: if(temp->coeff<0)
204: {
205: printf("%dx^%d",temp->coeff,temp->exp);
206: }
207: else
208: {
209: printf("+%dx^%d",temp->coeff,temp->exp);
210: }
211: temp=temp->link;
212: }
213: }
214:
215: //MAIN Function
216: int main()
217: {
218: NODE h1,h2,h3;
219: //clrscr();
220:
221: //get 3 nodes h1,h2,h3
222: h1=getnode();
223: h2=getnode();
224: h3=getnode();
225:
226: //Initially h1,h2,h3 links points to themselves
227: h1->link=h1;
228: h2->link=h2;
229: h3->link=h3;
230:
231: //Scan first and second polynomial
232: printf("Enter the first polynomial\n");
233: h1=read_poly(h1);
234: printf("Enter the second polynomial\n");
235: h2=read_poly(h2);
236:
237: //Call add_poly
238: h3=add_poly(h1,h2,h3);
239:
240: //Print the three polynomials
241: printf("\nThe first polynomial is :\n");
242: display(h1);
243: printf("\nThe second polynomial is: \n");
244: display(h2);
245: printf("\nThe third polynomial is: \n");
246: display(h3);
247:
248: //getch();
249: return 0;
250: }

You might also like