用链表实现两个多项式的运算
而且还要使用前插法构成链表
我自已做了一个,但运行不出来
能帮忙看一下吗?
代码如下
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#define ERROR 0
#define OK 1
#define EQUAL 1
#define OVERFLOW -1
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
struct POLY{
int coef;
int expn;
};
typedef struct POLY ElemType;
struct LNODE
{
ElemType data;
struct LNODE *next;
};
typedef struct LNODE LNode;
typedef struct LNODE *Linklist;
int init(Linklist *L)
{
*L=(LNode*)malloc(sizeof(LNode));
if(!L)exit(OVERFLOW);
(*L)->next=NULL;
return OK;
}/*init*/
int EqualList(ElemType *e1,ElemType *e2)
{
if(strcmp(e1->expn,e2->expn)==0)
return 1;
else
return 0;
}
int Less_EqualList(ElemType *e1,ElemType *e2)
{
if(strcmp(e1->expn,e2->expn)<=0)
return 1;
else
return 0;
}
int printlist(Linklist L)
{
Linklist p;
p=L;
printf("coef expn/n");
while(p->next)
{
p=p->next;
printf("%d %d/n",p->data.coef, p->data.expn);
}
printf("/n");
}
int ListInsert(Linklist L,int i,ElemType e)//前插法
{ int j=0;
Linklist p,s;
p=L;
j=0;
while(p&&j<i-1)
{ p=p->next;
++j;
}
if(!p||j>i-1)return ERROR;
s=(Linklist)malloc(sizeof(LNode));
s->data=e;
s->next=p->next;
p->next=s;return OK;
}
void AddList(Linklist a,Linklist b,Linklist *c)//这是多项式相加。
{Linklist pa, pb, pc;
c=(LNode)malloc(sizeof(LNode));
pa=a->next;pb=b->next;pc=*c;
while(!pa&&!pb){
if(EqualList(&pa->data,&pb->data))
{pc->data.coef=pa->data.coef+pb->data.coef;}
if(pc->data.coef==0)
{pa++;pb++;}
else{pc=pc->next;pa++;pb++;}
}
else(Less_EqualList(&pa->data,&pb->data))
{
pc->next=pa;pc=pc->next;pa++;}
else(Less_EqualList(&pb->data,&pa->data))
{pc->next=pb;
pc=pc->next;
pb++;
}
}
while(!pa) {pc->next=pa;pc=pc->next;pa++;}
while(!pb) {pc->next=pb;pc=pc->next;pb++;}
}
main()
{ struct POLY e;
Linklist La,Lb,Lc;
clrscr();
printf("/n/n--------List Demo is running...------/n/n");
printf("First is InsertList function./n");
init(&La);
e.coef=7;
e.expn=0;
ListInsert(La,1,e);
e.coef=3;
e.expn=1;
ListInsert(La,2,e);
printlist(La);
getch();
init(&Lb);
e.coef=8;
e.expn=1;
ListInsert(Lb,1,e);
e.coef=22;
e.expn=7;
ListInsert(Lb,2,e);
printlist(Lb);
AddList(La,Lb,&Lc);
printlist(Lc);
getch();
}
希望懂的能给我一点建议和理解
我将不胜感激