#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct LNode{
float coef;
int exp;
struct LNode *next;
} LNode,*LinkList;
//创建链表
void InitList(LinkList &q) {
q=(LNode*)malloc(sizeof(struct LNode));
q->next=NULL;
}
//加法时的插入
LinkList InsertList(LinkList L){
LinkList p;
while(L->next==NULL){
p=(LNode*)malloc(sizeof(struct LNode));
printf("\n输入各项系数和指数(系数,指数)输入0,0为结束: ");
scanf("%f,%d",&p->coef,&p->exp);
p->next=L->next;
L->next=p;
}
do {p=(LNode*)malloc(sizeof(struct LNode));
printf("\n输入各项系数和指数(系数,指数)输入0,0为结束: ");
scanf("%f,%d",&p->coef,&p->exp);
LinkList r=L,q=L->next;
while (q!=NULL){
if(p->exp<q->exp){
p->next=q;
r->next=p;
break;
}
else if(p->exp==q->exp){
q->coef=p->coef+q->coef;
break;
}
else {q=q->next;
r=r->next;
if(q==NULL){
p->next=q;
r->next=p;
}
}
}
}while(p->coef!=0||p->exp!=0);
- 1
- 2
- 3
前往页