user-0 提问: 两个多项式相加的程序，数据结构，看看有没有人能解决

回复:
user: user-0
content: 引用 4楼kugeniha 的回复:#include<stdio.h> #include<malloc.h>  typedef struct node   /*定义节点*/ { 	float coef;		/*多项式的系数*/ 	int exp;		/*多项式的指数*/ 	struct node * next; }listnode;  listnode * head; listnode * p; listnode * q; listnode * r; listnode * head1; listnode * head2; int head_num = 0; listnode* point[2];  void input()    /*输入的多项式系数和指数*/ { 	int j = 0; 	float coef;	/*节点中存放的多项式的系数*/ 	int exp;	/*节点中存放的多项式的指数*/ 	head = (listnode *)malloc(sizeof(listnode));		/* malloc 分配内存单元空间给头指针*/ 	head->next = NULL;	/*使头指针指向空，为空链表状态*/ start: printf("请输入系数和指数："); 	scanf_s("%f%d", &coef, &exp); 	if (coef == 0)//////////////系数为0退出 		goto next; 	p = (listnode *)malloc(sizeof(listnode));/*malloc 分配内存单元给指针p，通过p去预存输入的系数和指数*/ 	j++; 	p->coef = coef;	/*用p节点存储系数*/ 	p->exp = exp;		/*存储指数*/ 	p->next = head->next;	/*将下一个头指针分配给p，连接*/ 	head->next = p; 	goto start; next: head->exp = j; 	point[head_num] = head;	/*为了创建两个单链表*/ 	head_num++; }  int bubble_sort(listnode * myhead)/*用冒泡排序去将多项式的指数从高到低排序*/ { 	int i, j,num; 	listnode * swap;	/*用来交换节点*/ 	listnode * real_myhead;/*将每个多项式排序传递*/ 	real_myhead = myhead; 	num = myhead->exp; 	if (num == 1 || num == 0) 		goto next; 	for (i = 0; i<num - 1; i++) 	{ 		myhead = real_myhead; 		for (j = 0; j<num - 1; j++) /*将所有的多项式进行扫描*/ 		{ 			if ((myhead->next->exp)<(myhead->next->next->exp)) 			{ 				swap = (listnode *)malloc(sizeof(listnode)); 				swap->exp = myhead->next->exp;	/*将指数小的项传递给swap指针，存储*/ 				swap->coef = myhead->next->coef; 				myhead->next->exp = myhead->next->next->exp;/*将指数大的项排前*/ 				myhead->next->coef = myhead->next->next->coef; 				myhead->next->next->exp = swap->exp; 				myhead->next->next->coef = swap->coef; 				free(swap); 			} 			myhead = myhead->next; 		} 	}  next:return 0; }  void myprintf(listnode * node)//函数的表现形式 { 	listnode * mynode; 	mynode = (listnode *)malloc(sizeof(listnode)); 	mynode->coef = node->coef; 	mynode->exp = node->exp; 	if ((mynode->coef == 1) && (mynode->exp == 1))     //当系数为1，指数为1，输出X 		printf("x"); 	else if ((mynode->coef == 1) && (mynode->exp != 1)  		&& (mynode->exp != 0))                        //当系数为1，指数不为1和不为0，就输出X^exp 		printf("x^%d", mynode->exp); 	else if ((mynode->coef != 1) && (mynode->exp == 1))//系数不为1，指数为1时，输出 coef X 		printf("%.2fx", mynode->coef); 	else if ((mynode->coef != 0) && (mynode->exp == 0))//系数不为0，指数为0，那么X^exp为0，直接输出 coef 		printf("%.f", mynode->coef); 	else                                                //其他情况下，输出    coef X^exp 		printf("%.2fx^%d", mynode->coef, mynode->exp); 	free(mynode); }  int output(listnode * output)    /*输出函数,将多项式输出，还有两个多项式相加的结果输出*/ { 	if (output->exp == 0) 	{ 		printf("0\n"); 		return 0; 	} 	p = output->next; 	if (p->coef>0) 	{ 		myprintf(p); 		p = p->next; 	} 	for (;output->exp>1&&p; p = p->next) 	{ 		if (p->coef>=0) 			printf("+"); 		myprintf(p); 	} 	printf("\n"); 	return 0; }  void plus() { 	int i=0,m=point[0]->exp+point[1]->exp; 	listnode *p=point[0]; 	listnode *tmp; 	point[0]->exp=m; 	//连接两个链表 	for(;p->next;p=p->next); 	p->next=point[1]->next; 	//连接后整体排序 	bubble_sort(point[0]); 	//合并,多余的清0 	for(p=point[0]->next;p->next;p=p->next) 	{ 		if(p->exp==p->next->exp) 		{ 			p->next->coef=p->coef+p->next->coef; 			p->coef=0; 		} 	} 	//消除0系数结点 	for(p=point[0];p->next;) 		if(p->next->coef==0) 		{ 			tmp=p->next; 			p->next=p->next->next; 			free(tmp); 		} 		else p=p->next; }  int main() { 	printf("请输入第一个多项式的各个项的系数和指数(当系数为0时结束输入）\n"); 	input(); 	printf("请输入第二个多项式的各个项的系数和指数(当系数为0时结束输入）\n"); 	input(); 	bubble_sort(point[0]); 	bubble_sort(point[1]); 	printf("第一个多项式为："); 	output(point[0]); 	printf("第二个多项式为："); 	output(point[1]); 	printf("多项式1+多项式2=："); 	plus(); 	output(point[0]); 	printf("Press any key to exit"); 	getchar(); 	getchar(); 	return 0; }挺好的，完美运行哈哈

user: user-0
content: 引用 5楼kugeniha 的回复: #include<stdio.h> #include<malloc.h>  typedef struct node   /*定义节点*/ { 	float coef;		/*多项式的系数*/ 	int exp;		/*多项式的指数*/ 	struct node * next; }listnode;  listnode * head; listnode * p; listnode * q; listnode * r; listnode * head1; listnode * head2; int head_num = 0; listnode* point[2];  void input()    /*输入的多项式系数和指数*/ { 	int j = 0; 	float coef;	/*节点中存放的多项式的系数*/ 	int exp;	/*节点中存放的多项式的指数*/ 	head = (listnode *)malloc(sizeof(listnode));		/* malloc 分配内存单元空间给头指针*/ 	head->next = NULL;	/*使头指针指向空，为空链表状态*/ start: printf("请输入系数和指数："); 	scanf_s("%f%d", &coef, &exp); 	if (coef == 0)//////////////系数为0退出 		goto next; 	p = (listnode *)malloc(sizeof(listnode));/*malloc 分配内存单元给指针p，通过p去预存输入的系数和指数*/ 	j++; 	p->coef = coef;	/*用p节点存储系数*/ 	p->exp = exp;		/*存储指数*/ 	p->next = head->next;	/*将下一个头指针分配给p，连接*/ 	head->next = p; 	goto start; next: head->exp = j; 	point[head_num] = head;	/*为了创建两个单链表*/ 	head_num++; }  int bubble_sort(listnode * myhead)/*用冒泡排序去将多项式的指数从高到低排序*/ { 	int i, j,num; 	listnode * swap;	/*用来交换节点*/ 	listnode * real_myhead;/*将每个多项式排序传递*/ 	real_myhead = myhead; 	num = myhead->exp; 	if (num == 1 || num == 0) 		goto next; 	for (i = 0; i<num - 1; i++) 	{ 		myhead = real_myhead; 		for (j = 0; j<num - 1; j++) /*将所有的多项式进行扫描*/ 		{ 			if ((myhead->next->exp)<(myhead->next->next->exp)) 			{ 				swap = (listnode *)malloc(sizeof(listnode)); 				swap->exp = myhead->next->exp;	/*将指数小的项传递给swap指针，存储*/ 				swap->coef = myhead->next->coef; 				myhead->next->exp = myhead->next->next->exp;/*将指数大的项排前*/ 				myhead->next->coef = myhead->next->next->coef; 				myhead->next->next->exp = swap->exp; 				myhead->next->next->coef = swap->coef; 				free(swap); 			} 			myhead = myhead->next; 		} 	}  next:return 0; }  void myprintf(listnode * node)//函数的表现形式 { 	listnode * mynode; 	mynode = (listnode *)malloc(sizeof(listnode)); 	mynode->coef = node->coef; 	mynode->exp = node->exp; 	if ((mynode->coef == 1) && (mynode->exp == 1))     //当系数为1，指数为1，输出X 		printf("x"); 	else if ((mynode->coef == 1) && (mynode->exp != 1)  		&& (mynode->exp != 0))                        //当系数为1，指数不为1和不为0，就输出X^exp 		printf("x^%d", mynode->exp); 	else if ((mynode->coef != 1) && (mynode->exp == 1))//系数不为1，指数为1时，输出 coef X 		printf("%.2fx", mynode->coef); 	else if ((mynode->coef != 0) && (mynode->exp == 0))//系数不为0，指数为0，那么X^exp为0，直接输出 coef 		printf("%.f", mynode->coef); 	else                                                //其他情况下，输出    coef X^exp 		printf("%.2fx^%d", mynode->coef, mynode->exp); 	free(mynode); }  int output(listnode * output)    /*输出函数,将多项式输出，还有两个多项式相加的结果输出*/ { 	if (output->exp == 0) 	{ 		printf("0\n"); 		return 0; 	} 	p = output->next; 	if (p->coef>0) 	{ 		myprintf(p); 		p = p->next; 	} 	for (;output->exp>1&&p; p = p->next) 	{ 		if (p->coef>=0) 			printf("+"); 		myprintf(p); 	} 	printf("\n"); 	return 0; }  void plus() { 	int i=0,m=point[0]->exp+point[1]->exp; 	listnode *p=point[0]; 	listnode *tmp; 	point[0]->exp=m; 	//连接两个链表 	for(;p->next;p=p->next); 	p->next=point[1]->next; 	//连接后整体排序 	bubble_sort(point[0]); 	//合并,多余的清0 	for(p=point[0]->next;p->next;p=p->next) 	{ 		if(p->exp==p->next->exp) 		{ 			p->next->coef=p->coef+p->next->coef; 			p->coef=0; 		} 	} 	//消除0系数结点 	for(p=point[0];p->next;) 		if(p->next->coef==0) 		{ 			tmp=p->next; 			p->next=p->next->next; 			free(tmp); 		} 		else p=p->next; }  int main() { 	printf("请输入第一个多项式的各个项的系数和指数(当系数为0时结束输入）\n"); 	input(); 	printf("请输入第二个多项式的各个项的系数和指数(当系数为0时结束输入）\n"); 	input(); 	bubble_sort(point[0]); 	bubble_sort(point[1]); 	printf("第一个多项式为："); 	output(point[0]); 	printf("第二个多项式为："); 	output(point[1]); 	printf("多项式1+多项式2=："); 	plus(); 	output(point[0]); 	printf("Press any key to exit"); 	getchar(); 	getchar(); 	return 0; } 挺好的

user: user-1
content: #include<stdio.h> #include<malloc.h>  typedef struct node   /*定义节点*/ { 	float coef;		/*多项式的系数*/ 	int exp;		/*多项式的指数*/ 	struct node * next; }listnode;  listnode * head; listnode * p; listnode * q; listnode * r; listnode * head1; listnode * head2; int head_num = 0; listnode* point[2];  void input()    /*输入的多项式系数和指数*/ { 	int j = 0; 	float coef;	/*节点中存放的多项式的系数*/ 	int exp;	/*节点中存放的多项式的指数*/ 	head = (listnode *)malloc(sizeof(listnode));		/* malloc 分配内存单元空间给头指针*/ 	head->next = NULL;	/*使头指针指向空，为空链表状态*/ start: printf("请输入系数和指数："); 	scanf_s("%f%d", &coef, &exp); 	if (coef == 0)//////////////系数为0退出 		goto next; 	p = (listnode *)malloc(sizeof(listnode));/*malloc 分配内存单元给指针p，通过p去预存输入的系数和指数*/ 	j++; 	p->coef = coef;	/*用p节点存储系数*/ 	p->exp = exp;		/*存储指数*/ 	p->next = head->next;	/*将下一个头指针分配给p，连接*/ 	head->next = p; 	goto start; next: head->exp = j; 	point[head_num] = head;	/*为了创建两个单链表*/ 	head_num++; }  int bubble_sort(listnode * myhead)/*用冒泡排序去将多项式的指数从高到低排序*/ { 	int i, j,num; 	listnode * swap;	/*用来交换节点*/ 	listnode * real_myhead;/*将每个多项式排序传递*/ 	real_myhead = myhead; 	num = myhead->exp; 	if (num == 1 || num == 0) 		goto next; 	for (i = 0; i<num - 1; i++) 	{ 		myhead = real_myhead; 		for (j = 0; j<num - 1; j++) /*将所有的多项式进行扫描*/ 		{ 			if ((myhead->next->exp)<(myhead->next->next->exp)) 			{ 				swap = (listnode *)malloc(sizeof(listnode)); 				swap->exp = myhead->next->exp;	/*将指数小的项传递给swap指针，存储*/ 				swap->coef = myhead->next->coef; 				myhead->next->exp = myhead->next->next->exp;/*将指数大的项排前*/ 				myhead->next->coef = myhead->next->next->coef; 				myhead->next->next->exp = swap->exp; 				myhead->next->next->coef = swap->coef; 				free(swap); 			} 			myhead = myhead->next; 		} 	}  next:return 0; }  void myprintf(listnode * node)//函数的表现形式 { 	listnode * mynode; 	mynode = (listnode *)malloc(sizeof(listnode)); 	mynode->coef = node->coef; 	mynode->exp = node->exp; 	if ((mynode->coef == 1) && (mynode->exp == 1))     //当系数为1，指数为1，输出X 		printf("x"); 	else if ((mynode->coef == 1) && (mynode->exp != 1)  		&& (mynode->exp != 0))                        //当系数为1，指数不为1和不为0，就输出X^exp 		printf("x^%d", mynode->exp); 	else if ((mynode->coef != 1) && (mynode->exp == 1))//系数不为1，指数为1时，输出 coef X 		printf("%.2fx", mynode->coef); 	else if ((mynode->coef != 0) && (mynode->exp == 0))//系数不为0，指数为0，那么X^exp为0，直接输出 coef 		printf("%.f", mynode->coef); 	else                                                //其他情况下，输出    coef X^exp 		printf("%.2fx^%d", mynode->coef, mynode->exp); 	free(mynode); }  int output(listnode * output)    /*输出函数,将多项式输出，还有两个多项式相加的结果输出*/ { 	if (output->exp == 0) 	{ 		printf("0\n"); 		return 0; 	} 	p = output->next; 	if (p->coef>0) 	{ 		myprintf(p); 		p = p->next; 	} 	for (;output->exp>1&&p; p = p->next) 	{ 		if (p->coef>=0) 			printf("+"); 		myprintf(p); 	} 	printf("\n"); 	return 0; }  void plus() { 	int i=0,m=point[0]->exp+point[1]->exp; 	listnode *p=point[0]; 	listnode *tmp; 	point[0]->exp=m; 	//连接两个链表 	for(;p->next;p=p->next); 	p->next=point[1]->next; 	//连接后整体排序 	bubble_sort(point[0]); 	//合并,多余的清0 	for(p=point[0]->next;p->next;p=p->next) 	{ 		if(p->exp==p->next->exp) 		{ 			p->next->coef=p->coef+p->next->coef; 			p->coef=0; 		} 	} 	//消除0系数结点 	for(p=point[0];p->next;) 		if(p->next->coef==0) 		{ 			tmp=p->next; 			p->next=p->next->next; 			free(tmp); 		} 		else p=p->next; }  int main() { 	printf("请输入第一个多项式的各个项的系数和指数(当系数为0时结束输入）\n"); 	input(); 	printf("请输入第二个多项式的各个项的系数和指数(当系数为0时结束输入）\n"); 	input(); 	bubble_sort(point[0]); 	bubble_sort(point[1]); 	printf("第一个多项式为："); 	output(point[0]); 	printf("第二个多项式为："); 	output(point[1]); 	printf("多项式1+多项式2=："); 	plus(); 	output(point[0]); 	printf("Press any key to exit"); 	getchar(); 	getchar(); 	return 0; }

user: user-2
content: 单步跟踪程序运行，看每一步执行结果与你期望的是否一致，这样容易找到BUG原因

user: user-3
content: 排序问题，建议先判断排序是否正确了，再进行多项式的加法运算。

user: user-1
content: #include<stdio.h> #include<malloc.h>  typedef struct node   /*定义节点*/ { 	float coef;		/*多项式的系数*/ 	int exp;		/*多项式的指数*/ 	struct node * next; }listnode;  listnode * head; listnode * p; listnode * q; listnode * r; listnode * head1; listnode * head2; int head_num = 0; listnode* point[2];  void input()    /*输入的多项式系数和指数*/ { 	int j = 0; 	float coef;	/*节点中存放的多项式的系数*/ 	int exp;	/*节点中存放的多项式的指数*/ 	head = (listnode *)malloc(sizeof(listnode));		/* malloc 分配内存单元空间给头指针*/ 	head->next = NULL;	/*使头指针指向空，为空链表状态*/ start: printf("请输入系数和指数："); 	scanf_s("%f%d", &coef, &exp); 	if (coef == 0)//////////////系数为0退出 		goto next; 	p = (listnode *)malloc(sizeof(listnode));/*malloc 分配内存单元给指针p，通过p去预存输入的系数和指数*/ 	j++; 	p->coef = coef;	/*用p节点存储系数*/ 	p->exp = exp;		/*存储指数*/ 	p->next = head->next;	/*将下一个头指针分配给p，连接*/ 	head->next = p; 	goto start; next: head->exp = j; 	point[head_num] = head;	/*为了创建两个单链表*/ 	head_num++; }  int bubble_sort(listnode * myhead)/*用冒泡排序去将多项式的指数从高到低排序*/ { 	int i, j,num; 	listnode * swap;	/*用来交换节点*/ 	listnode * real_myhead;/*将每个多项式排序传递*/ 	real_myhead = myhead; 	num = myhead->exp; 	if (num == 1 || num == 0) 		goto next; 	for (i = 0; i<num - 1; i++) 	{ 		myhead = real_myhead; 		for (j = 0; j<num - 1; j++) /*将所有的多项式进行扫描*/ 		{ 			if ((myhead->next->exp)<(myhead->next->next->exp)) 			{ 				swap = (listnode *)malloc(sizeof(listnode)); 				swap->exp = myhead->next->exp;	/*将指数小的项传递给swap指针，存储*/ 				swap->coef = myhead->next->coef; 				myhead->next->exp = myhead->next->next->exp;/*将指数大的项排前*/ 				myhead->next->coef = myhead->next->next->coef; 				myhead->next->next->exp = swap->exp; 				myhead->next->next->coef = swap->coef; 				free(swap); 			} 			myhead = myhead->next; 		} 	}  next:return 0; }  void myprintf(listnode * node)//函数的表现形式 { 	listnode * mynode; 	mynode = (listnode *)malloc(sizeof(listnode)); 	mynode->coef = node->coef; 	mynode->exp = node->exp; 	if ((mynode->coef == 1) && (mynode->exp == 1))     //当系数为1，指数为1，输出X 		printf("x"); 	else if ((mynode->coef == 1) && (mynode->exp != 1)  		&& (mynode->exp != 0))                        //当系数为1，指数不为1和不为0，就输出X^exp 		printf("x^%d", mynode->exp); 	else if ((mynode->coef != 1) && (mynode->exp == 1))//系数不为1，指数为1时，输出 coef X 		printf("%.2fx", mynode->coef); 	else if ((mynode->coef != 0) && (mynode->exp == 0))//系数不为0，指数为0，那么X^exp为0，直接输出 coef 		printf("%.f", mynode->coef); 	else                                                //其他情况下，输出    coef X^exp 		printf("%.2fx^%d", mynode->coef, mynode->exp); 	free(mynode); }  int output(listnode * output)    /*输出函数,将多项式输出，还有两个多项式相加的结果输出*/ { 	if (output->exp == 0) 	{ 		printf("0\n"); 		return 0; 	} 	p = output->next; 	if (p->coef>0) 	{ 		myprintf(p); 		p = p->next; 	} 	for (;output->exp>1&&p; p = p->next) 	{ 		if (p->coef>=0) 			printf("+"); 		myprintf(p); 	} 	printf("\n"); 	return 0; }  void plus() { 	int i=0,m=point[0]->exp+point[1]->exp; 	listnode *p=point[0]; 	listnode *tmp; 	point[0]->exp=m; 	//连接两个链表 	for(;p->next;p=p->next); 	p->next=point[1]->next; 	//连接后整体排序 	bubble_sort(point[0]); 	//合并,多余的清0 	for(p=point[0]->next;p->next;p=p->next) 	{ 		if(p->exp==p->next->exp) 		{ 			p->next->coef=p->coef+p->next->coef; 			p->coef=0; 		} 	} 	//消除0系数结点 	for(p=point[0];p->next;) 		if(p->next->coef==0) 		{ 			tmp=p->next; 			p->next=p->next->next; 			free(tmp); 		} 		else p=p->next; }  int main() { 	printf("请输入第一个多项式的各个项的系数和指数(当系数为0时结束输入）\n"); 	input(); 	printf("请输入第二个多项式的各个项的系数和指数(当系数为0时结束输入）\n"); 	input(); 	bubble_sort(point[0]); 	bubble_sort(point[1]); 	printf("第一个多项式为："); 	output(point[0]); 	printf("第二个多项式为："); 	output(point[1]); 	printf("多项式1+多项式2=："); 	plus(); 	output(point[0]); 	printf("Press any key to exit"); 	getchar(); 	getchar(); 	return 0; }

user: user-4
content: 仅供参考：//链表实现一元多项式的加法减法乘法 #include <stdio.h> #include <stdlib.h> typedef struct node {     float coef;   //系数     int expn;     //指数     struct node *next; } PolyNode;      //多项式节点 polynomial node typedef PolyNode* Polynomial; Polynomial createPolynomial() {  //创建多项式     PolyNode *p, *q, *head = (PolyNode *)malloc(sizeof(PolyNode));   //头节点     head->next = NULL;     float coef;     int expn;     printf("输入该多项式每一项的系数和指数，每项一行，输入0 0结束！\n");     while (scanf("%f %d", &coef, &expn) && coef) {   // 默认，按指数递减排列         if (head->next) {             p = head;             while (p->next && expn < p->next->expn)                 p = p->next;             if (p->next) {                 if (expn == p->next->expn) { //有相同指数的直接把系数加到原多项式                     p->next->coef += coef;                     if (p->next->coef > -0.000001 && p->next->coef < 0.000001) { //若是相加后系数为0，则舍弃该节点                         q = p->next;                         p->next = q->next;                         free(q);                     }                 } else {                     q = (PolyNode*)malloc(sizeof(PolyNode));                     q->coef = coef;                     q->expn = expn;                     q->next = p->next;                     p->next = q;                 }             } else {                 p->next = (PolyNode*)malloc(sizeof(PolyNode));                 p = p->next;                 p->coef = coef;                 p->expn = expn;                 p->next = NULL;             }         } else {             head->next = (PolyNode*)malloc(sizeof(PolyNode));             head->next->coef = coef;             head->next->expn = expn;             head->next->next = NULL;         }     }     return head; } Polynomial multiply(Polynomial poly, float coef, int expn) {  //多项式与指定单项式相乘，该单项式为 coefx^expn     PolyNode *p, *q, *Poly = (PolyNode*)malloc(sizeof(PolyNode));     p = Poly;     q = poly->next;     while (q) {         p->next = (PolyNode*)malloc(sizeof(PolyNode));         p = p->next;         p->coef = (q->coef*coef);         p->expn = (q->expn + expn);         q = q->next;     }     p->next = NULL;     return Poly; } void add(Polynomial poly1, Polynomial poly2) {  //把 poly2 加到 poly1 上     PolyNode *p, *q, *r;     r = poly1;     p = poly1->next;  //指向第一个节点     q = poly2->next;     poly2->next = NULL;     while (p && q) {         if (p->expn > q->expn) {             r->next = p;             p = p->next;             r = r->next;         } else if (p->expn < q->expn) {             r->next = q;             q = q->next;             r = r->next;         } else {             PolyNode *t;             p->coef += q->coef;             if (!(p->coef > -0.000001 && p->coef < 0.000001)) //系数不为0             {                 r->next = p;                 r = r->next;                 p = p->next;             } else {                 t = p;                 p = p->next;                 free(t);             }             t = q;             q = q->next;             free(t);         }     }     if (p)         r->next = p;     if (q)         r->next = q; } Polynomial polySubtract(Polynomial poly1, Polynomial poly2) {  //多项式减法 poly1-poly2形成一个新的多项式     //把poly2的系数取相反数，形成一个新的多项式     Polynomial poly = (PolyNode*)malloc(sizeof(PolyNode)); //构造头节点     PolyNode *p, *q;     p = poly;     q = poly2->next;     while (q) {         p->next = (PolyNode*)malloc(sizeof(PolyNode));         p = p->next;         p->coef = -(q->coef);  //系数取反         p->expn = q->expn;         q = q->next;     }     p->next = NULL;     add(poly, poly1);  //利用加法     return poly; } Polynomial polyAdd(Polynomial poly1, Polynomial poly2) { //多项式相加 poly1+poly2形成一个新的多项式     Polynomial poly = (PolyNode*)malloc(sizeof(PolyNode));  //和多项式的头节点     poly->next = NULL;     PolyNode *p, *q, *r;     r = poly;     p = poly1->next;     q = poly2->next;     while (p&&q) {         if (p->expn > q->expn) {             r->next = (PolyNode*)malloc(sizeof(PolyNode));             r = r->next;             r->coef = p->coef;             r->expn = p->expn;             p = p->next;         } else if (p->expn < q->expn) {             r->next = (PolyNode*)malloc(sizeof(PolyNode));             r = r->next;             r->coef = q->coef;             r->expn = q->expn;             q = q->next;         } else {             float m = p->coef + q->coef;             if (!(m > -0.000001 && m < 0.000001)) {                 r->next = (PolyNode*)malloc(sizeof(PolyNode));                 r = r->next;                 r->coef = m;                 r->expn = p->expn;             }             q = q->next;             p = p->next;         }     }     while (p) {         r->next = (PolyNode*)malloc(sizeof(PolyNode));         r = r->next;         r->coef = p->coef;         r->expn = p->expn;         p = p->next;     }     while (q) {         r->next = (PolyNode*)malloc(sizeof(PolyNode));         r = r->next;         r->coef = q->coef;         r->expn = q->expn;         q = q->next;     }     r->next = NULL;     return poly; } Polynomial polyMultiply(Polynomial poly1, Polynomial poly2) {  //多项式相乘     Polynomial poly = (PolyNode*)malloc(sizeof(PolyNode));  //创建多项式和的头节点     poly->next = NULL;     PolyNode *p;     p = poly2->next;     while (p) {         add(poly, multiply(poly1, p->coef, p->expn));         p = p->next;     }     return poly; } void printPoly(Polynomial poly) {  //打印多项式     if (poly && poly->next) {         PolyNode *p = poly->next;  //p指向第一个节点         while (p->next) {             printf("%gx^%d", p->coef, p->expn);             p = p->next;             if (p && (p->coef > 0))                 printf("+");         }         if (p->expn == 0)             printf("%g", p->coef);   //打印常数项         else             printf("%gx^%d", p->coef, p->expn);         printf("\n");     } } void freePoly(Polynomial poly) {  //释放内存     if (poly && poly->next) {         PolyNode *p, *q;         p = poly;         while (p) {             q = p->next;             free(p);             p = q;         }     }     poly = NULL; } int main() {     printf("用链表实现多项式的加减法\n");     Polynomial poly1, poly2, poly3;     printf("创建多项式一\n");     poly1 = createPolynomial();     printf("多项式一：\n");     printPoly(poly1);     printf("创建多项式二\n");     poly2 = createPolynomial();     printf("多项式二：\n");     printPoly(poly2);     printf("两多项式相加，和为：\n");     poly3 = polyAdd(poly1, poly2);     printPoly(poly3);     freePoly(poly3);     printf("两个多项式相乘，积为：\n");     poly3 = polyMultiply(poly1, poly2);     printPoly(poly3);     freePoly(poly3);     printf("两多项式相减，差为：\n");     poly3 = polySubtract(poly1, poly2);     printPoly(poly3);     freePoly(poly1);     freePoly(poly2);     freePoly(poly3);     system("pause");     return 0; }