一个星期了还是不懂。创建一个一元多项式链表并且实现相加
这是结构体
struct Linklist {
int val;//存值
int coe;
struct Linklist* next;//存下一个元素地址
};struct Linklist* p;//表头
在main函数中初始化表头
struct Linklist* heada= (struct Linklist*)malloc(sizeof(struct Linklist));
heada->next=null;
然后用这个函数尾插法插入元素
void Init(struct Linklist* L) {
Linklist* p = L;
int n ;
cout << “输入多少组数据”;
cin >> n;
for(int i=0;i<n;i++) {
struct Linklist* ptr = (struct Linklist*)malloc(sizeof(struct Linklist));
cin >> ptr->val;
cin >> ptr ->coe;
ptr->next = p->next;
p->next = ptr;
p = ptr;
};
}
用同样方法创建b表后初始化c表并用函数相加最后爆出c6011错误
Linklist sum(struct Linklist* L1, struct Linklist* L2, struct Linklist* L3) {
Linklist* p1 = L1->next,* p2 = L2->next,* p3 = L3;
Linklist* p = L3;
while (p1&&p2) {
if(p1->coe == p2->coe){
struct Linklist* s= (struct Linklist*)malloc(sizeof(struct Linklist));
s->val = p1->val + p2->val;
s->coe = p1->coe;
p1 = p1->next;
p2 = p2->next;
}
else if ((p1->coe) > (p2->coe)) {
struct Linklist* s = (struct Linklist*)malloc(sizeof(struct Linklist));
s->val = p1->val;
s->coe = p1->coe;
p1 = p1->next;
}
else {
struct Linklist* s = (struct Linklist*)malloc(sizeof(struct Linklist));
s->val = p2->val;
s->coe = p2->coe;
p2 = p2->next;
}
}
while (p2) {
struct Linklist* s = (struct Linklist*)malloc(sizeof(struct Linklist));
s->val = p2->val;
s->coe = p2->coe;
p2 = p2->next;
}
while(p1){
struct Linklist* s = (struct Linklist*)malloc(sizeof(struct Linklist));
s->val = p1->val;
s->coe = p1->coe;
p1 = p1->next;
}return* L3;
}
在调用了这个函数后内存突然升至2g怎么回事,如何解决,诚心求教