C语言 链表 没有头指针,C语言链表头结点指针域不赋值出现错误?

用单链表实现多项式的加法和乘法

很基础的一道数据结构题目,用C语言实现了

主要的函数和main函数如下

#include

#include

//多项式结构体

typedef struct PolyNode *Polynomial;

struct PolyNode{

int coef;//系数

int expon;//指数

Polynomial link;//指向后一个节点的指针域

};

Polynomial ReadPoly();//读多项式

void Attach(int c, int e, Polynomial *pRear);//构造多项式

Polynomial Mult(Polynomial P1, Polynomial P2);//多项式乘法

Polynomial Add(Polynomial P1, Polynomial P2);//多项式加法

void PrintPoly(Polynomial P);//输出多项式

int main(int argc, const char * argv[]) {

// insert code here...

Polynomial P1, P2, PP, PS;

P1 = ReadPoly();//读取两个多项式

P2 = ReadPoly();

PP = Mult(P1, P2);

PrintPoly(PP);

PS = Add(P1, P2);

PrintPoly(PS);

return 0;

}

Polynomial ReadPoly(){

Polynomial P, Rear, t;

int c, e, N;

scanf("%d", &N);

P = (Polynomial)malloc(sizeof(struct PolyNode));

P->link = NULL;

Rear = P;

while (N--) {

scanf("%d %d", &c, &e);

Attach(c, e, &Rear);

}

t = P;

P = P->link;

free(t);

return P;

}

//Attach函数要构建链表表示多项式

void Attach(int c, int e, Polynomial *pRear){

Polynomial P;

P = (Polynomial)malloc(sizeof(struct PolyNode));

P->coef = c;

P->expon = e;

P->link = (*pRear)->link;

(*pRear)->link = P;

*pRear = P;

}

//加法

Polynomial Add(Polynomial P1, Polynomial P2){

Polynomial front, rear, t1, t2;

int sum;

t1 = P1;

t2 = P2;

front = (Polynomial)malloc(sizeof(struct PolyNode));

front->link = NULL;

rear = front;

while (t1 && t2) {

if (t1->expon == t2->expon) {

sum = t1->coef + t2->coef;

if(sum)

Attach(sum, t1->expon, &rear);

t1 = t1->link;

t2 = t2->link;

}

else if (t1->expon > t2->expon){

Attach(t1->coef, t1->expon, &rear);

t1 = t1->link;

}

else if (t1->expon < t2->expon){

Attach(t2->coef, t2->expon, &rear);

t2 = t2->link;

}

}

while (t1){

Attach(t1->coef, t1->expon, &rear);

t1 = t1->link;

}

while (t2){

Attach(t2->coef, t2->expon, &rear);

t2 = t2->link;

}

rear = front;

front = front->link;

free(rear);

return front;

}

//乘法

Polynomial Mult(Polynomial P1, Polynomial P2){

Polynomial P, Rear, t1, t2, t;

int c, e;

if (!P1 || !P2)

return NULL;

t1 = P1;

t2 = P2;

P = (Polynomial)malloc(sizeof(struct PolyNode));

P->link = NULL;//**为什么要让p指向空?**

Rear = P;

while (t2) {

Attach(t1->coef*t2->coef, t1->expon+t2->expon, &Rear);

t2 = t2->link;

}

t1 = t1->link;

while (t1) {

t2 = P2;

Rear = P;

while (t2) {

c = t1->coef * t2->coef;

e = t1->expon + t2->expon;

while (Rear->link && Rear->link->expon > e)

Rear = Rear->link;

if (Rear->link && Rear->link->expon == e) {

if (Rear->link->coef + c)

Rear->link->coef += c;

else {

t = Rear->link;

Rear->link = t->link;

free(t);

}

}

else {

t = (Polynomial)malloc(sizeof(struct PolyNode));

t->coef = c;

t->expon = e;

t->link = Rear->link;

Rear->link = t;

Rear = Rear->link;

}

t2 = t2->link;

}

t1 = t1->link;

}

Rear = P;

P = P->link;

free(Rear);

return P;

}

问题在于我把乘法中的P->link = NULL删掉之后,整个算法的乘法和加法都出错了,请问这是为什么呢?

Polynomial P, Rear, t1, t2, t;

int c, e;

if (!P1 || !P2)

return NULL;

t1 = P1;

t2 = P2;

P = (Polynomial)malloc(sizeof(struct PolyNode));

P->link = NULL;//**为什么要让p指向空?**

Rear = P;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值