多项式加减:用单链表表示多项式,并进行多项式加减运算
1.结点结构:
typedef struct linkednode
{
int coef;
int exp;
struct linkednode *next;
}snode,*ptr;
coef是系数,exp是指数,next是值域。
snode是结点类型名,ptr是结点指针类型名。
2.链表创建:
ptr creatlinked_B()
{
ptr head,last,p;
int x,y;
head=last=(ptr)malloc(sizeof(snode));
last->next=NULL;
printf("输入系数:\n");
scanf("%d",&x);
printf("输入指数:\n");
scanf("%d",&y);
printf("\n");
while(x!=0 && y!=0)
{
p=(ptr)malloc(sizeof(snode));
p->coef=x;
p->exp=y;
last->next=p;
p->next=NULL;
last=p;
printf("输入系数:\n");
scanf("%d",&x);
printf("输入指数:\n");
scanf("%d",&y);
printf("\n");
}
p=head;
head=head->next;
free(p);
return head;
}
3.链表输出:
void outlinkA(ptr p)
{
printf("多项式为:\n");
while(p!=NULL)
{
if(p->coef>0)
{
printf("+");
printf("%d",p->coef);
}
if(p->coef<0)
printf("%d",p->co