-
Notifications
You must be signed in to change notification settings - Fork 0
/
ass2_7.c
99 lines (99 loc) · 3.08 KB
/
ass2_7.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
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <stdio.h>
#include <stdlib.h>
struct N
{
int data, pow;
struct N *next;
};
struct N *p1 = NULL, *p2 = NULL, *p = NULL; // these are all the heads of the three pollynomial or the linked list.
struct N *create_node(int, int, struct N *); // adds the newly crested node to the end of the linked list.
struct N *create(int, int); // creates a new node and returns it's address.
void display(struct N *); // traverse through the linked list and displays the values of every node.
struct N *padd(struct N *, struct N *, struct N *); // adds two pollynomials.
void main()
{
// Create first list of 5x^2 + 4x^1 + 2x^0
p1=create_node(5, 2, p1);
p1=create_node(4, 1, p1);
p1=create_node(2, 0, p1);
// Create second list of 5x^1 + 5x^0
p2=create_node(5, 1, p2);
p2=create_node(5, 0, p2);
printf("1st Pollynomial: ");
display(p1);
printf("\n2nd Pollynoimal: ");
display(p2);
p=padd(p1, p2, p); // Function add two pnomial numbers
printf("\nAdded pnomial: "); // Display resultant List
display(p);
}
struct N* padd(struct N *p1, struct N *p2, struct N *p)
{
struct N *temp,*t1,*t2;
t1=p1;
t2=p2;
while (t1 && t2) // both the nodes are having some values.
{
if (t1->pow > t2->pow) // the node present into p1 will be transfered into p as it is.
{
p=create_node(t1->data, t1->pow, p);
t1 = t1->next;
}
else if (t1->pow < t2->pow) // the node present into p2 will be transfered into p as it is.
{
p=create_node(t2->data, t2->pow, p);
t2 = t2->next; // basic traversing.
}
else // if both the coeff are same.
{
p=create_node(t1->data + t2->data, t1->pow, p);
t1 = t1->next;
t2 = t2->next;
}
} // someone in between p1 and p2 is end.
// temp is simply asking for the remaining.
for (temp = (t1) ? t1 : t2; temp; temp = temp->next)
p=create_node(temp->data, temp->pow, p); //basic traversal.
return p;
}
struct N* create_node(int data, int pow, struct N *head) // always adding at the end node.
{
struct N *temp;
temp = head;
if (!head) //insertion at head
head = create(data, pow);
else
{
while (temp->next) // traversing to the end.
temp = temp->next;
temp->next = create(data, pow);
}
return head;
}
struct N *create(int data, int pow)
{
struct N *temp;
int x;
temp = (struct N *)malloc(sizeof(struct N));
temp->data = data;
temp->pow = pow;
temp->next = NULL;
return temp;
}
void display(struct N *head)
{
struct N *temp;
if (!head)
printf("\nThe Linked list is empty. Nothing is to show.\n");
else
{
printf("\nThe linked list are: \n");
temp = head;
while (temp->next)
{
printf("Coefficient is: %d\t Power: %d\n", temp->data, temp->pow);
temp = temp->next;
}
printf("Coefficient is: %d\t Power: %d\n", temp->data, temp->pow);
}
}