#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int xishu;
int zhishu;
struct Node *next;
}NoDe;
void Build(NoDe *L){
NoDe *p,*q;
int a,b;
//p=(NoDe*)malloc(sizeof(NoDe));
p=L;
printf("请输入多项式的系数和指数以0结束\n");
scanf("%d %d",&a,&b);
while (a)//a!=0
{
q=(NoDe*)malloc(sizeof(NoDe));
p->next=q;
q->xishu=a;
q->zhishu=b;
p=p->next;
scanf("%d %d",&a,&b);
}
q->next=NULL;
}
void Print(NoDe *L)//计算单链表的长度,然后输出单链表
{
int num=0;
NoDe *p;
p=L->next;
while(p)
{
num++;
printf("%dx+",p->xishu);
printf("^%d ",p->zhishu);
p=p->next;
}
printf("\n长度为%d:\n",num);
}
int main(){
NoDe *L;
L=(NoDe*)malloc(sizeof(NoDe));
Build(L);
Print(L);
}
时间: 2024-11-08 17:46:19