-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolynomial_List.c
More file actions
153 lines (153 loc) · 3.86 KB
/
Polynomial_List.c
File metadata and controls
153 lines (153 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int co;
int pow;
struct node *next;
}node;
node *new_node,*trav;//global node* pointers
void Display(node *head)//display node in the list
{
if(head == NULL)
printf("LIST IS EMPTY!!!\n");
else
{
trav = head;
while(trav != NULL)
{
if(trav->next == NULL)
printf("%dx^%d",trav->co,trav->pow);
else
printf("%dx^%d + ",trav->co,trav->pow);
trav = trav->next;
}
}
}
node *Create()//allocate memory for new node
{
node *new_node1;
new_node1 = (node *)malloc(sizeof(node));
printf("Enter the cofficient: ");
scanf("%d",&new_node1->co);
printf("Enter the power: ");
scanf("%d",&new_node1->pow);
new_node1->next = NULL;
return new_node1;
}
node *Insert(node *head)//insert node in the list
{
new_node = Create();
if(head == NULL)
head = new_node;
else
{
trav = head;
while(trav->next != NULL)
trav = trav->next;
trav->next = new_node;
}
return head;
}
node *Create2(int co,int pow)//function for allocating memory to poly3 expression
{
node *new_node3;
new_node3 = (node *)malloc(sizeof(node));
new_node3->pow = pow;
new_node3->co = co;
new_node3->next = NULL;
return new_node3;
}
node *Insert3(node *head3,int co,int pow)//function for traversing poly3 after head3 node
{
node *new_node3,*trav3;
new_node3 = Create2(co,pow);
if(head3 == NULL)
head3 = new_node3;
else
{
trav3 = head3;
while(trav3->next != NULL)
trav3 = trav3->next;
trav3->next = new_node3;
}
return head3;
}
node *Add(node *head1,node *head2,node *head3)//polynomial 1 and polynomial 2 addition function
{
while(head1 != NULL && head2 != NULL)
{
if(head1->pow > head2->pow)
{
head3 = Insert3(head3, head1->co, head1->pow);
head1 = head1->next;
}
else if(head2->pow > head1->pow)
{
head3 = Insert3(head3, head2->co, head2->pow);
head2 = head2->next;
}
else
{
head3 = Insert3(head3, head1->co + head2->co, head1->pow);
head1 = head1->next;
head2 = head2->next;
}
}
while(head1 != NULL || head2 != NULL)//loop for evaluating remaining poly1 or poly2 terms
{
if(head1 != NULL)
{
head3 = Insert3(head3, head1->co, head1->pow);
head1 = head1->next;
}
if(head2 != NULL)
{
head3 = Insert3(head3, head2->co, head2->pow);
head2 = head2->next;
}
}
return head3;
}
int main()
{
int a;
char ch;
node *head1 = NULL, *head2 = NULL, *head3 = NULL;//head1->poly1 head2->poly2 head3->poly3
do
{
printf("1)Create Polynomial 1:\n");
printf("2)Create Polynomial 2:\n");
printf("3)Display Polynomial 1:\n");
printf("4)Display Polynomial 2:\n");
printf("5)Add Polynomial 1 and Polynomial 2:\nEnter your choice : ");
scanf("%d",&a);
switch(a)
{
case 1:
head1 = Insert(head1);
Display(head1);
break;
case 2:
head2 = Insert(head2);
Display(head2);
break;
case 3:
Display(head1);
break;
case 4:
Display(head2);
break;
case 5:
head3 = NULL;
head3 = Add(head1,head2,head3);
Display(head3);
break;
default:
printf("INVALID CHOICE!!!\n");
}
printf("\nDo you want to Continue(Y/N): ");
scanf(" %c",&ch);
} while (ch == 'y' || ch == 'Y');
return 0;
}