数据结构 求补充完整一元多项式减法的运算
#include <stdio.h>#include <malloc.h>
typedef struct
{
int cff;//系数
int exp;//指数
} datatype;
typedef struct Node
{
datatype data_1;
struct Node *next;
}listnode;//自定义struct数据类型为linklist,而不是定义linklist,所以不能在这里定义pa,pb
typedef listnode* linklist;
linklist create_list()//创建带头结点的链表,返回头结点,尾插法
{
linklist head=(linklist)malloc(sizeof(listnode));
linklist p, r;
int xs;
int zs;
r=head;
while(scanf("%d%d",&xs, &zs)
&& xs != 0
|| zs != 0)//输入00是结束
{
p=(linklist)malloc(sizeof(listnode));
p->data_1.cff = xs;
p->data_1.exp = zs;
r->next = p;
r = p;
}
r->next = NULL;
return (head);
}
void Select_sort(linklist l)//选择排序,由高到低
{
linklist p, r, q;
r = l->next;
p = (linklist)malloc(sizeof(listnode));
while( r != NULL)
{
q = r->next;
while( q != NULL)
{
if( q->data_1.exp > r->data_1.exp )
{
p->data_1.cff = r->data_1.cff;
p->data_1.exp = r->data_1.exp;
r->data_1.cff = q->data_1.cff;
r->data_1.exp = q->data_1.exp;
q->data_1.cff = p->data_1.cff;
q->data_1.exp = p->data_1.exp;
}
q = q->next;
}
r = r->next;
}
}
void merge_list(linklist l)//合并同类项
{
linklist r, q, p;
r = l;
while( r != NULL && r->next != NULL)
{
q = r->next;
while( q->next != NULL && q != NULL
&& (q->data_1.exp == q->next->data_1.exp))
{
p = q->next;
q->data_1.cff += p->data_1.cff;
q->next = p->next;
free(p);
}
r = r->next;
}
//清楚系数为0的项
r = l;
while(r != NULL && r->next != NULL)
{
if( r->next->data_1.cff == 0)
{
q = r->next;
r->next = q->next;
free(q);
}
r = r->next;
}
}
linklist add_list(linklist l_fir, linklist l_sec)//表一表二相加并将结果存放在表三中
{
linklist head=(linklist)malloc(sizeof(listnode));
linklist p, q, r, s;
r = head;
p = l_fir->next;
q = l_sec->next;
while( p != NULL && q != NULL )//表一表二中均有数据
{
s = (linklist)malloc(sizeof(listnode));
if(p->data_1.exp == q->data_1.exp)
{
s->data_1.cff = p->data_1.cff + q->data_1.cff;
s->data_1.exp = p->data_1.exp;
p = p->next;
q = q->next;
}
else if(p->data_1.exp > q->data_1.exp)
{
s->data_1.cff = p->data_1.cff;
s->data_1.exp = p->data_1.exp;
p = p->next;
}
else if(p->data_1.exp < q->data_1.exp)
{
s->data_1.cff = q->data_1.cff;
s->data_1.exp = q->data_1.exp;
q = q->next;
}
r->next = s;
r = s;
}
while( p != NULL)//仅表一中有数据
{
s = (linklist)malloc(sizeof(listnode));
s->data_1.cff = p->data_1.cff;
s->data_1.exp = p->data_1.exp;
p = p->next;
r->next = s;
r =s;
}
while( q != NULL)//仅表二中有数据
{
s = (linklist)malloc(sizeof(listnode));
s->data_1.cff = q->data_1.cff;
s->data_1.exp = q->data_1.exp;
q = q->next;
r->next = s;
r =s;
}
r->next = NULL;
//清楚系数为0的项
r = head;
while(r != NULL && r->next != NULL)
{
if( r->next->data_1.cff == 0)
{
q = r->next;
r->next = q->next;
free(q);
}
r = r->next;
}
return (head);
}
void result_out(linklist p)//输出结果
{
linklist k;
k = p;
if(p->next == NULL)
printf("empty list\n");
while( p->next != NULL)
{
if( p->next->data_1.cff < 0)
{
if(p == k)
printf("-");
else
printf("- ");
printf("%d^%d ", -p->next->data_1.cff, p->next->data_1.exp);
}
else if(p != k && p->next->data_1.cff > 0)
{
printf("+ ");
printf("%d^%d ", p->next->data_1.cff, p->next->data_1.exp);
}
else if(p == k && p->next->data_1.cff > 0)
{
printf("%d^%d ", p->next->data_1.cff, p->next->data_1.exp);
}
p = p->next;
}
}
void free_list(linklist p)//释放空间
{
linklist k, q;
k = p;
while(p != NULL && p->next != NULL)
{
q = p->next;
p->next = q->next;
free(q);
}
}
void main()
{
linklist pa, pb, pc;
printf("请输入第一个多项式:\n");
pa = create_list();
Select_sort(pa);
merge_list(pa);
printf("排序化简后的多项式为:\n");
printf(" \n");
result_out(pa);
printf("\n \n\n");
printf("请输入第二个多项式:\n");
pb = create_list();
Select_sort(pb);
merge_list(pb);
printf("排序化简后的多项式为:\n");
printf(" \n");
result_out(pb);
printf("\n \n\n");
printf("相加后的多项式为:\n");
printf(" \n");
pc = add_list(pa,pb);
result_out(pc);
printf("\n \n\n");
free_list(pa);
free(pa);
free_list(pb);
free(pb);
free_list(pc);
free(pc);
}