70,034
社区成员
发帖
与我相关
我的任务
分享
head=(seqlist *)malloc(sizeof(seqlist));
head->next = NULL;
if(a!=0)
{
p->coef=a;
p->exp=b;
q->next=p;
q=p;
p=p->next;
}
p都没分配空间,你就直接操作其成员了
建议
p = (seqlist *)malloc(sizeof(seqlist));
p->next = NULL;
#include <stdio.h>
#include <malloc.h>
typedef struct seqlist
{
int coef;
int exp;
struct seqlist *next;
}seqlist;
/*
*功能:输入多项式的系数和指数
*返回值:多项式的首地址
*
*/
seqlist *inputMult()
{
seqlist *head, *p1, *p2;
head = (seqlist*)malloc(sizeof(seqlist));
p1 = head;
printf("请输入多项式(当系数和指数均为0时结束):\n");
while(1)
{
printf("系数: ");
scanf("%d", &p1->coef);
printf("指数: ");
scanf("%d", &p1->exp);
if(p1->coef == 0 && p1->exp == 0)
break;
p2 = p1;
p1 = (seqlist*)malloc(sizeof(seqlist));
p2->next = p1;
}
p2->next = NULL;
free(p1);
return head;
}
/*
*功能:输出多项式
*参数:多项式的首地址
*
*/
void display(seqlist *head)
{
while(head != NULL)
{
printf("%dX^%d", head->coef, head->exp);
if(head->next != NULL)
printf(" + ");
head = head->next;
}
printf("\n");
}
int main()
{
seqlist *head;
head = inputMult();
display(head);
return 0;
}