-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPolynomialSLL.c
86 lines (77 loc) · 1.81 KB
/
PolynomialSLL.c
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
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node{
int a,b,c;
struct node *next;
};
struct node *head;
struct node *ptr;
/*
This function creates the linked list and stores the value of the expression in the list.
*/
void insert(){
struct node *ptr1;
ptr1 = (struct node *)malloc(sizeof(struct node));
ptr1->next = NULL;
printf("\nEnter the coefficient of a:\t");
scanf("%d",&ptr1->a);
printf("\nEnter the coefficient of b:\t");
scanf("%d",&ptr1->b);
printf("\nEnter the coefficient of c:\t");
scanf("%d",&ptr1->c);
if(head == NULL){
head = ptr1;
return;
}
else{
ptr = head;
while(ptr->next != NULL){
ptr = ptr->next;
}
ptr->next = ptr1;
}
}
/*
This function sums all the values present in the linked list and gives the sum of the polynomial.
*/
void sum(){
int a=0;
int b=0;
int c=0;
if(head == NULL){
printf("\nNo Expression Added!");
}
else{
ptr = head;
while(ptr != NULL){
a += ptr->a;
b += ptr->b;
c += ptr->c;
ptr = ptr->next;
}
printf("\nThe sum of the expressions entered is: %dx^2 + %dx + %d ",a,b,c);
}
}
void main(){
while(1){
printf("\n1. Enter the expression");
printf("\n2. Display the sum of the expressions");
printf("\n3. Exit!");
int ch;
printf("\nEnter your choice:\t");
scanf("%d",&ch);
switch(ch){
case 1:
insert();
break;
case 2:
sum();
break;
case 3:
exit(0);
default:
printf("\nINVALID CHOICE!");
}
}
}