0% found this document useful (0 votes)
4 views5 pages

Polynomial.cpp

Uploaded by

zufallsreise
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)
4 views5 pages

Polynomial.cpp

Uploaded by

zufallsreise
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/ 5

12/3/24, 8:46 AM Polynomial.

cpp

Linked_List\Polynomial.cpp

1 #include<iostream>
2 using namespace std;
3 struct Node
4 {
5 int coff,exp;
6 Node *next;
7 };
8 Node *start=NULL,*head=NULL,*newnode,*temp,*temp1,*temp2,
9 *start1=NULL,*start2=NULL,*head1=NULL,*head2=NULL;
10 void create()
11 {
12 int c,p;
13 cout<<"Enter Cofficient ";
14 cin>>c;
15 cout<<"Enter Exponent ";
16 cin>>p;
17 newnode=(Node*)malloc(sizeof(Node));
18 newnode->coff=c;
19 newnode->exp=p;
20 newnode->next=NULL;
21 }
22 void insertion()
23 {
24 if(start==NULL)
25 start=head=newnode;
26 else if(start->exp==newnode->exp)
27 {
28 start->coff+=newnode->coff;
29 if(start->coff==0)
30 {
31 temp=start;
32 start=start->next;
33 free(temp);
34 }
35 }
36 else if(start->exp<newnode->exp)
37 {
38 newnode->next=start;
39 start=newnode;
40 }
41 else if(head->exp==newnode->exp)
42 {
43 head->coff+=newnode->coff;
44 if(head->coff==0)
45 {
46 temp=start;
47 while(temp->next!=head)
48 {
49 temp=temp->next;
50 }
51 head=temp;

localhost:55142/2fc7c393-c82a-4f3f-b0c1-c389a97db11b/ 1/5
12/3/24, 8:46 AM Polynomial.cpp

52 temp=temp->next;
53 head->next=NULL;
54 free(temp);
55 }
56 }
57 else if(head->exp>newnode->exp)
58 {
59 head->next=newnode;
60 head=newnode;
61 }
62 else
63 {
64 temp1=temp2=start;
65 while(temp2->exp>newnode->exp)
66 {
67 temp1=temp2;
68 temp2=temp2->next;
69 }
70 if(temp2->exp==newnode->exp)
71 {
72 temp2->coff+=newnode->coff;
73 }
74 else
75 {
76 temp1->next=newnode;
77 newnode->next=temp2;
78 }
79 if(temp2->coff==0)
80 {
81 temp1->next=temp2->next;
82 free(temp2);
83 }
84
85 }
86 }
87 void addition()
88 {
89 Node *temp1=start1,*temp2=start2;
90 while(temp1!=NULL && temp2!=NULL)
91 {
92 newnode=(Node *)malloc(sizeof(Node));
93 newnode->next=NULL;
94 if(temp1->exp>=temp2->exp)
95 {
96 newnode->coff=temp1->coff;
97 newnode->exp=temp1->exp;
98 temp1=temp1->next;
99 }
100 else
101 {
102 newnode->coff=temp2->coff;
103 newnode->exp=temp2->exp;
104 temp2=temp2->next;
105 }
localhost:55142/2fc7c393-c82a-4f3f-b0c1-c389a97db11b/ 2/5
12/3/24, 8:46 AM Polynomial.cpp

106 insertion();
107
108 }
109 if(temp2!=NULL)
110 {
111 if(head->exp==temp2->exp)
112 {
113 newnode=(Node *)malloc(sizeof(Node));
114 newnode->coff=temp2->coff;
115 newnode->exp=temp2->exp;
116 temp2=temp2->next;
117 insertion();
118 }
119
120 head->next=temp2;
121 head=head2;
122 }
123 else if(temp1!=NULL)
124 {
125 if(head->exp==temp1->exp)
126 {
127 newnode=(Node *)malloc(sizeof(Node));
128 newnode->coff=temp1->coff;
129 newnode->exp=temp1->exp;
130 temp1=temp1->next;
131 insertion();
132 }
133 head->next=temp1;
134 head=head1;
135 }
136 }
137 void display(Node *start)
138 {
139 temp=start;
140 while(temp)
141 {
142 if(temp!=start && temp->coff>=0)
143 {
144 cout<<"+";
145 }
146 cout<<temp->coff;
147 if(temp->exp>0)
148 cout<<"x^"<<temp->exp;
149 temp=temp->next;
150 }
151 }
152 int main()
153 {
154 int a,ch;
155 do{
156 cout<<"\nEnter 1 For Operation in 1st Linked List "
157 <<"\nEnter 2 For Operation in 2nd Linked List "
158 <<"\nNote :You Can Perform Operation Only Once On List "
159 <<"\nEnter your Choice ";
localhost:55142/2fc7c393-c82a-4f3f-b0c1-c389a97db11b/ 3/5
12/3/24, 8:46 AM Polynomial.cpp

160 cin>>ch;
161 switch(ch)
162 {
163 case 1:
164 do
165 {
166 cout<<"\nEnter 1 For Insertion "
167 <<"\nEnter 2 For Display"
168 <<"\nEnter Your Choice ";
169 cin>>ch;
170 switch (ch)
171 {
172 case 1:
173 create();
174 insertion();
175 start1=start;
176 head1=head;
177
178 break;
179 case 2:
180 display(start1);
181 break;
182 default:
183 cout<<"\nWrong Choice ";
184 }
185 cout<<"\nDo you Want to Continue ?(1 for yes )";
186 cin>>a;
187 } while (a==1);
188 start=head=NULL;
189 break;
190 case 2:
191 do
192 {
193 cout<<"\nEnter 1 For Insertion "
194 <<"\nEnter 2 For Subtract "
195 <<"\nEnter 3 For Display"
196 <<"\nEnter Your Choice ";
197 cin>>ch;
198 switch (ch)
199 {
200 case 1:
201 create();
202 insertion();
203 start2=start;
204 head2=head;
205 break;
206 case 2:
207 display(start2);
208 break;
209 default:
210 cout<<"\nWrong Choice ";
211 }
212 cout<<"\nDo you Want to Continue ?(1 for yes )";
213 cin>>a;
localhost:55142/2fc7c393-c82a-4f3f-b0c1-c389a97db11b/ 4/5
12/3/24, 8:46 AM Polynomial.cpp

214 } while (a==1);


215 start=head=NULL;
216 break;
217 default :
218 cout<<"\nWrong Choice ";
219 }
220 cout<<"\nDo you Want to Continue ?(1 for yes )";
221 cin>>a;
222 }while(a==1);
223 addition();
224 cout<<"\nAfter Addition : ";
225 display(start);
226 return 0;
227 }

localhost:55142/2fc7c393-c82a-4f3f-b0c1-c389a97db11b/ 5/5

You might also like