Basic linked list errors

I am working on basic linked list and I have just writen a program on Adding and multiplying polynomials. Unfortunately, I there's some problem with the memory...or may be my functions. Can you figure out my mistake and give me an advise or how to fix it?. Thanks
( heso = coefficent, somu = exponent)
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
  #include<iostream>
#include<stdio.h>
#include<cstdio>
#include<atlalloc.h>
using namespace std;
struct term {
	int heso;
	int somu;
	term *next;
};
struct polynomial {
	term *first;
};

void initial(polynomial &L)
{
	L.first = NULL;
}
void polyInit(polynomial &tmp)
{
	tmp.first = 0;
}
void printpolynomial(polynomial a)
{
	cout << "P(x) = ";
	for (term *temp = a.first; temp != NULL; temp = temp->next)
	{
		if (temp->next != NULL)
		{
			cout << temp->heso << "x^" << temp->somu << " +";
		}
		else
		{
			cout << temp->heso;
		}
	}
}
void polynomialinput(polynomial a)
{
	term x;
	do
	{
		cout << endl << "He so: "; cin >> x.heso;
		cout << "So mu: "; cin >> x.somu;
	}
	while (x.somu > 0);
}
polynomial Add(polynomial a, polynomial b)
{
	term *atemp = a.first;
	term *btemp = b.first;
	polynomial result;
	polyInit(result);
	term *tempterm = new term;
	if (atemp->somu >= btemp->somu)
	{
		tempterm->somu = btemp->somu;
		tempterm->heso = btemp->heso;
		btemp = btemp->next;
	}
	else
	{
		tempterm->somu = atemp->somu;
		tempterm->heso = atemp->heso;
		atemp = atemp->next;
	}
	result.first = tempterm;
	while (atemp != NULL && btemp != NULL)
	{
		term *newterm = new term;
		if (atemp->somu == btemp->somu)
		{
			newterm->heso = atemp->heso + btemp->heso;
			newterm->somu = atemp->somu + btemp->somu;
			atemp = atemp->next;
			btemp = btemp->next;
		}
		else if (atemp->somu > btemp->somu){
			newterm->heso = atemp->heso;
			newterm->somu = atemp->somu;
			atemp = atemp->next;
		}
		else {
			newterm->heso = btemp->heso;
			newterm->somu = btemp->somu;
			btemp = btemp->next;
		}
		tempterm->next = newterm;
		tempterm = newterm;
	}
	if (atemp == NULL)
	{
		tempterm->next = btemp;
	}
	else
	{
		tempterm->next = atemp;
	}
	return result;
}
int main()
{
	polynomial X, Y, Z;
	polyInit(X);
	polyInit(Y);
	polyInit(Z);
	cout << "Nhap a: ";
	polynomialinput(X);
	cout << "Nhap b: ";
	polynomialinput(Y);
	Z = Add(X, Y);
	printpolynomial(Z);
	cin.get();
	cin.get();
	return 0;
}
/*This is my function on multiplying polynomial, I have some troubles with it too.
polynomial multiply(polynomial a, polynomial b)
{
	polynomial result;
	term *prod = new term[a.first->somu + b.first->somu](0);
	for (term *atemp = a.first; atemp != NULL; atemp = atemp->next){
		for (term *btemp = b.first; btemp != NULL; btemp = btemp->next){
			prod[atemp->somu + btemp->somu] += atemp->heso * btemp->heso;
		}
	}
	term *tempterm = new term;
	tempterm->heso = prod[0];
	tempterm->somu = 0;
	result.first = tempterm;
	for (int i = 0; i < a.first->somu + b.first->somu; i++){
		term *newterm = new term;
		newterm->somu = i;
		newterm->heso = prod[i];
		tempterm->next = newterm;
		tempterm = newterm;
	}
}*/
There are a number of problems with your design. A linked list is not a polynomial, nor is a polynomial a linked list. The logic for the two should be separate. There is not a single delete in your code, although there are plenty of news.

But, your most immediate problem is that polynomialinput doesn't do anything but let the user enter some numbers that are promptly discarded.
Last edited on
so what can I do? I was taught to use this kind. Can you show me how to fix it?
As to the design: redesign. (This is not strictly necessary. You can make your poor design work, but it will still be poorly designed.)

As to not deleting what you new: do so. (Also not strictly necessary. You can pretend your resources are unlimited, but it's not a very good idea to do so.)

As to polynomialinput not doing what it should, make it do what it should. (Entirely necessary.)
Topic archived. No new replies allowed.