P(x),Q(x)分别为两个一元稀疏多项式计算器,利用单链表存储。实现相加,相减,就地逆置两者之差。
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
typedef struct term
{
float coef;//系数
int expn;//次方
struct term *next;
}Position;
typedef struct
{
Position *head;
Position *tail;
int len;
}Linklist;
void Initlist(Linklist *l)
{
l->head=NULL;
l->tail=NULL;
l->len=0;
}
Position *create(Position *head,Linklist *l)
{
int m,i=1;
Position *p,*q;
head=(Position *)malloc(sizeof(Position));
p=q=head;
head->next=NULL;
printf("这个一元多项式共有几项:");
scanf("%d",&m);
l->head=head;
l->len=m;
while(i<=m)
{
p=(Position *)malloc(sizeof(Position));
printf("第%d项的系数:",i);
scanf("%f",&p->coef);
printf("第%d项的次方:",i);
scanf("%d",&p->expn);
q->next=p;
q=p;
i++