user-0 提问: 【新手求教】能不能帮我看下这个有什么问题呢，数据结构，一元多项式相加

回复:
user: user-0
content: 真心谢谢大家，正在看，正在学习

user: user-1
content: 引用 4 楼 cc946079647 的回复: #include<stdio.h> #include<malloc.h> #include<stdlib.h>  typedef struct Polynode { 	int coef; 	int exp; 	struct Polynode *next; }Polynode,*Polylist;  void polycreate(Polynode **p,int m) {    	Polynode *head,*s; 	head=(Polynode*)malloc(sizeof(Polynode)); 	head->next=NULL; 	int c,e,i; 	for(i=0;i<m;i++) 	{ 		printf("请输入多项式的系数以及指数.\n"); 		scanf("%d,%d",&c,&e); 		if(c!=0) 		{ 			s=(Polynode*)malloc(sizeof(Polynode)); 			s->coef=c; 			s->exp=e; 			s->next=head->next; 			head->next=s; 		} 	} 	*p=head; }  void polyadd(Polylist polya,Polylist polyb) { 	Polynode *p,*q,*tail,*temp; 	int sum; 	p=polya->next; 	q=polyb->next; 	tail=polya; 	while(p!=NULL&&q!=NULL) 	{ 		if(p->exp<q->exp) 		{tail->next=p;tail=p;p=p->next;} 		else if(p->exp==q->exp) 		{    sum=p->coef+q->coef; 		if(sum!=0) 		{    p->coef=sum; 		tail->next=p;tail=p; 		p=p->next; 		temp=q;q=q->next;free(temp); 		} 		else 		{    temp=p;p=p->next;free(temp); 		temp=q;q=q->next;free(temp); 		} 		} 		else 		{tail->next=q;tail=q;q=q->next;} 	} 	if(p!=NULL) 		tail->next=p; 	else 		tail->next=q; }  void print(Polynode *p) { 	while(p->next!=NULL) 	{    		p=p->next; 		printf("%dx^%d",p->coef,p->exp); 	} }   void main() { 	Polylist pa=NULL; 	Polylist pb=NULL; 	int n,m; 	printf("请输入多项式pa项数：\n"); 	scanf("%d",&m); 	polycreate(&pa,m); 	print(pa); 	printf("\n"); 	printf("请输入多项式pb的项数：\n"); 	scanf("%d",&n); 	polycreate(&pb,n); 	print(pb); 	printf("\n"); 	polyadd(pa,pb); 	print(pa); }   注意，想要在polycreate内部修改pa，要用指向Polynode指针的指针

user: user-1
content: #include<stdio.h> #include<malloc.h> #include<stdlib.h>  typedef struct Polynode { 	int coef; 	int exp; 	struct Polynode *next; }Polynode,*Polylist;  void polycreate(Polynode **p,int m) {    	Polynode *head,*s; 	head=(Polynode*)malloc(sizeof(Polynode)); 	head->next=NULL; 	int c,e,i; 	for(i=0;i<m;i++) 	{ 		printf("请输入多项式的系数以及指数.\n"); 		scanf("%d,%d",&c,&e); 		if(c!=0) 		{ 			s=(Polynode*)malloc(sizeof(Polynode)); 			s->coef=c; 			s->exp=e; 			s->next=head->next; 			head->next=s; 		} 	} 	*p=head; }  void polyadd(Polylist polya,Polylist polyb) { 	Polynode *p,*q,*tail,*temp; 	int sum; 	p=polya->next; 	q=polyb->next; 	tail=polya; 	while(p!=NULL&&q!=NULL) 	{ 		if(p->exp<q->exp) 		{tail->next=p;tail=p;p=p->next;} 		else if(p->exp==q->exp) 		{    sum=p->coef+q->coef; 		if(sum!=0) 		{    p->coef=sum; 		tail->next=p;tail=p; 		p=p->next; 		temp=q;q=q->next;free(temp); 		} 		else 		{    temp=p;p=p->next;free(temp); 		temp=q;q=q->next;free(temp); 		} 		} 		else 		{tail->next=q;tail=q;q=q->next;} 	} 	if(p!=NULL) 		tail->next=p; 	else 		tail->next=q; }  void print(Polynode *p) { 	while(p->next!=NULL) 	{    		p=p->next; 		printf("%dx^%d",p->coef,p->exp); 	} }   void main() { 	Polylist pa=NULL; 	Polylist pb=NULL; 	int n,m; 	printf("请输入多项式pa项数：\n"); 	scanf("%d",&m); 	polycreate(&pa,m); 	print(pa); 	printf("\n"); 	printf("请输入多项式pb的项数：\n"); 	scanf("%d",&n); 	polycreate(&pb,n); 	print(pb); 	printf("\n"); 	polyadd(pa,pb); 	print(pa); }

user: user-2
content: #include<stdio.h> #include<malloc.h> #include<stdlib.h>  typedef struct Polynode {     int coef;     int exp;     struct Polynode *next; }Polynode,*Polylist;  void polycreate(Polynode *p,int m)//参数p传入函数后就没有用过 {        Polynode *head,*rear,*s;     int c,e,i;     head=(Polynode*)malloc(sizeof(Polynode));     rear=head; for(i=0;i<m;i++) { printf("请输入多项式的系数以及指数.\n");     scanf("%d,%d",&c,&e);     while(c!=0)//这是一个死循环，已经有for了还需要这个东西吗？     { s=(Polynode*)malloc(sizeof(Polynode)); s->coef=c; s->exp=e; rear->next=s; rear=s; scanf("%d,%d",&c,&e);     }     rear->next=NULL; } }  void polyadd(Polylist polya,Polylist polyb) {     Polynode *p,*q,*tail,*temp;     int sum;     p=polya->next;     q=polyb->next;     tail=polya;     while(p!=NULL&&q!=NULL)     { if(p->exp<q->exp)     {tail->next=p;tail=p;p=p->next;} else if(p->exp==q->exp) {    sum=p->coef+q->coef;     if(sum!=0) {    p->coef=sum;      tail->next=p;tail=p;      p=p->next;      temp=q;q=q->next;free(temp); }     else     {    temp=p;p=p->next;free(temp);  temp=q;q=q->next;free(temp);     } } else     {tail->next=q;tail=q;q=q->next;}     }     if(p!=NULL) tail->next=p;     else tail->next=q; }  void print(Polynode *p) { while(p->next!=NULL) {    p=p->next; printf("%dx^%d",p->coef,p->exp); } }   void main() { Polylist pa,pb;定义了二个空指针，却不分配内存 int n,m; printf("请输入多项式pa项数：\n"); scanf("%d",&m); polycreate(pa,m); print(pa); printf("\n"); printf("请输入多项式pb的项数：\n"); scanf("%d",&n); polycreate(pb,n); print(pb); printf("\n"); polyadd(pa,pb); print(pa); } 错的地方太多了，自己好好重新写一个吧

user: user-1
content: 引用 1 楼 cc946079647 的回复:polycreate(pa,m); 声明是传地址吧。。这里应该是polycreate(&pa,m); 看错了。。

user: user-1
content: polycreate(pa,m); 声明是传地址吧。。这里应该是polycreate(&pa,m);