Bad Ass 2020-02-11 23:03 采纳率: 100%

# 最近在学数据结构，为什么在C++中，在相乘函数中Z1能够被D1赋值，而Z2不能被D2赋值而一直报错？（经检查，D1, D2都是有值的）

/*多项式相乘测试*/
#include<iostream>
using namespace std;

/*建立节点结构体*/
struct PolyNode {
int coef;
int pow;
struct PolyNode* next;
};
typedef struct PolyNode* Polynomial;

/*建立输入函数*/
Polynomial InputPoly() {
Polynomial P, temp;
P = (Polynomial)malloc(sizeof(struct PolyNode));
temp = P;
int N;
cout << "Input N" << endl;
cin >> N;
for (N; N >= 1; N--) {
cout << "Input coefficent and power:" << endl;
cin >> P->coef;
cin >> P->pow;
P->next = (Polynomial)malloc(sizeof(struct PolyNode));
P = P->next;
}
P = NULL;
P = temp;
return P;
}

/*声明Attach函数*/
Polynomial Attach(int c, int p, Polynomial m) {
Polynomial single, n, first;
single = (Polynomial)malloc(sizeof(struct PolyNode));
single->coef = c;
single->pow = p;
n = m;
first = m;
if (single->pow > n->pow) {
single->next = n;
n = single;
return n;
}
if (single->pow == n->pow) {
n->coef = single->coef + n->coef;
free(single);
if (n->coef == 0) {
Polynomial t = n;
n = n->next;
free(t);
return n;
}
return n;
}
for (; 1; n = n->next) {
if (single->pow < n->pow && single->pow > n->next->pow&& n->next != NULL) {
single->next = n->next;
n->next = single;
n = first;
return n;
}
if (single->pow == n->next->pow && n->next != NULL) {
Polynomial t = n->next;
t->coef = single->coef + t->coef;
free(single);
if (t->coef == 0) {
n->next = t->next;
free(t);
n = first;
return n;
}
n = first;
return n;
}
if (n->next == NULL) {
single->next = n->next;
n->next = single;
n = first;
return n;
}
}
}

/*相乘函数*/
Polynomial Mult(Polynomial D1, Polynomial D2) {
Polynomial Z1;
Polynomial Z2;
Polynomial temp, r, s;
Z1 = D1;
Z2 = D2;
int flag;
temp = (Polynomial)malloc(sizeof(struct PolyNode));
if (!(Z1 && Z2)) {
cout << "Zero";
return NULL;
}
r = temp;
while (Z2!=NULL) {
temp->coef = Z1->coef * Z2->coef;
temp->pow = Z1->pow + Z2->pow;
Z2 = Z2->next;
temp->next= (Polynomial)malloc(sizeof(struct PolyNode));
temp = temp->next;
}
temp = NULL;
temp = r;
Z1 = Z1->next;
for (; Z1 != NULL; Z1 = Z1->next){
Z2 = D2;
for (; Z2 != NULL; Z2 = Z2->next) {
s = (Polynomial)malloc(sizeof(struct PolyNode));
s->coef = Z1->coef * Z2->coef;
s->pow = Z1->pow + Z2->pow;
temp = Attach(s->coef, s->pow, temp);
free(s);
}
}
temp = r;
return temp;
}

/*输出函数*/
void Output(Polynomial q) {
Polynomial temp = q;
cout << "The ressult is: "<<endl;
int flag = 1;
while (q) {
if (!flag) {
cout << " ";
}
cout << temp->coef << " " << temp->pow;
temp = temp->next;
flag = 0;
}
}

/*清除链表函数*/
void Clean(Polynomial q) {
Polynomial a, b;
a = q;
b = q;
while (a) {
a = a->next;
free(b);
b = a;
}
free(b);
}

/*主函数*/
int main() {
Polynomial p1, p2, res;
p1 = InputPoly();
p2 = InputPoly();
res = Mult(p1, p2);
Output(res);
Clean(res);
return 0;
}
• 写回答

#### 2条回答默认 最新

• Charles Cedric 2020-02-12 16:29
关注

不同的编译器应该会有不同的处理方法，你换一个编译器试试

本回答被题主选为最佳回答 , 对您是否有帮助呢?
评论

#### 悬赏问题

• ¥30 android百度地图SDK海量点显示标题
• ¥15 windows导入environment.yml运行conda env create -f environment_win.yml命令报错
• ¥15 这段代码可以正常运行，打包后无法执行，在执行for内容之前一直不断弹窗，请修改调整
• ¥15 C语言判断有向图是否存在环路
• ¥15 请问4.11到4.18以及4.27和4.29公式的具体推导过程是怎样的呢
• ¥20 将resnet50中的卷积替换微ODConv动态卷积
• ¥15 通过文本框输入商品信息点击按钮将商品信息列举出来点击加入购物车商品信息添加到表单中
• ¥100 这是什么压缩算法？如何解压？