Adding polynomial equation using linked lists

I am trying to add 2 polynomial equation using linked list .But the program is breaking.PLease help!

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
#include <iostream>
using namespace std;
struct Poly
{
	int coff;
	int expo;
	Poly *next;
};
Poly *head = NULL;
Poly *head2 = NULL;
Poly *temp = head;
Poly *temp2 = head2;
void showdata()
{
	Poly *temp = head;
	while (temp != NULL)
	{
		cout << temp->coff << "x^" << temp->expo<< " + ";
		temp = temp->next;
	}

}
void showdatatwo()
{
	Poly *temp2 = head2;
	while (temp2 != NULL)
	{
		cout << temp2->coff << "x^" << temp2->expo << " + ";
		temp2 = temp2->next;
	}

}
void makenodes()
{
	Poly * node = new Poly;
	cout << "Enter coffiecient of node " << endl;
	cin >> node->coff;
	cout << "Enter exponent of node " << endl;
	cin >> node->expo;
	node->next = NULL;
	if (head == NULL)
	{
		head = node;
	}
	else
	{
	     // temp pointer ko head ka address day dia shuro mein dono 
		while (temp->next != NULL) // temp ko chalao last tak..agay aur koi node nahi
		{
			temp = temp->next;
		}
		temp->next = node; // ab temp next yani...last node k address walay slot par node ka address copy..LINKED!
						   // nabeel did temp->next = &node; cuz object ka address pointer ko!
	}

}
void makenodesTwo()
{
	Poly * node = new Poly;
	cout << "Enter coffiecient of node " << endl;
	cin >> node->coff;
	cout << "Enter exponent of node " << endl;
	cin >> node->expo;
	node->next = NULL;
	if (head2 == NULL)
	{
		head2 = node;
	}
	else
	{
 
		while (temp2->next != NULL) 
		{
			temp2 = temp2->next;
		}
		temp2->next = node;
	}

}
void addpoly()
{
	
	while (temp->next != NULL && temp2->next != NULL)
	{
		if (temp->expo == temp2->expo)
		{
			temp->coff= temp->coff + temp2->coff;
			temp = temp->next;
			temp2 = temp2->next;
		}
		cout << temp->coff << "x^" << temp->expo << " + ";


	}
}
int main()
{
	int x;
	cout << "How many nodes you want to make:" << endl;
	cin >> x;
	for (int i = 0; i < x; i++)
	{
		makenodes();
	}
	showdata();
	cout << "How many nodes you want to make:" << endl;
	cin >> x;
	for (int i = 0; i < x; i++)
	{
		makenodesTwo();
	}
	showdatatwo();
	addpoly();

	system("pause");
}
For sure, you need to learn about passing parameters, returning results and NOT relying on global variables for very short term convenience.

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
void showdata(Poly * head)
{
	Poly *temp = head;
	while (temp != NULL)
	{
		cout << temp->coff << "x^" << temp->expo<< " + ";
		temp = temp->next;
	}

}

Poly *appendNode(Poly *head)
{
	Poly * node = new Poly;
        Poly * result = node;  // the head of the list
	cout << "Enter coffiecient of node " << endl;
	cin >> node->coff;
	cout << "Enter exponent of node " << endl;
	cin >> node->expo;
	node->next = NULL;
	if (head == NULL)
	{
		head = node;
	}
	else
	{
	     // temp pointer ko head ka address day dia shuro mein dono 
		while (temp->next != NULL) // temp ko chalao last tak..agay aur koi node nahi
		{
			temp = temp->next;
		}
		temp->next = node; // ab temp next yani...last node k address walay slot par node ka address copy..LINKED!
						   // nabeel did temp->next = &node; cuz object ka address pointer ko!
	}
    return result;
}

int main()
{
        Poly *p1 = nullptr, *p2 = nullptr;
	int x;
	cout << "How many nodes you want to make:" << endl;
	cin >> x;
	for (int i = 0; i < x; i++)
	{
		p1 = appendNode(p1);
	}
	showdata(p1);
	cout << "How many nodes you want to make:" << endl;
	cin >> x;
	for (int i = 0; i < x; i++)
	{
		p2 = appendNode(p2);
	}
	showdata(p2);
	addpoly(p1,p2);

	system("pause");
}


Write each function ONCE, with suitable parameters.

Don't make N copies of it, and hope you manage to make all the right edits in every single copy, every time you make a change to one of them.


A linked list is a bad way to store a polynomial:
- adding to the tail is expensive unless you keep a tail pointer.
- There's nothing to prevent the user from entering the terms out of order.
- You have to store the exponent of each term.

I would store it as a vector where vec[n] is the coefficient of xn
- Impossible to get items out of order.
- Random access to coefficients
- Guarantee that if xn exists, then all the previous terms exist too.

If you must use a linked list, then take Salem C's advice. In addition, I'd suggest:
- appendNode() needs to initialize temp
- Addpoly() should not print the result. That's showpoly's job. Just call showpoly() in main after calling addpoly()
- Modify addpoly() to handle the case where the polynomials are not of the same order: i.e., when p1's max exponent is 3 and p2's max exponent is 5.
- Modify addpoly() to handle the case where polynomials have missing terms (which presumably means the coefficient is zero).

Ideally, you'd also change appendNode() to insert the new node in the right position, so the user can enter the nodes in any order.

Also, consider using for loops to go through the list. For example:
1
2
3
4
5
6
void showdata(Poly *head)
{
    for (Poly *temp = head; temp; temp = temp->next) {
        cout << temp->coff << "x^" << temp->expo<< " + ";
    }
}

Topic archived. No new replies allowed.