一元多項式相加的鏈式實現
阿新 • • 發佈:2019-01-22
#include"PLOY.h"
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
//動態申請結點
NODE * alloc_node(int coef,int exp)
{
NODE * tmp = (NODE *)malloc(sizeof(NODE));
assert(tmp);
tmp ->coef = coef;
tmp ->exp = exp;
tmp->next = NULL;
return tmp;
}
//建立有序一元多項式
NODE *create_ploy()
{
NODE *phead = alloc_node(0,0);
int coef;
int exp;
NODE *tmp;
NODE *s;
NODE *p;
while(1)//同指數合併(未考慮)
{
printf("請輸入多項式(輸入0,0表示停止輸入)\n");
scanf("%d,%d",&coef,&exp);
if (coef == 0 && exp == 0)
{
break;
}
if (coef == 0)
{
continue;
}
tmp = alloc_node(coef,exp);
p = phead;
s = phead;
while(p != NULL && p->exp <= tmp->exp)
{
s = p;
p = p->next;
}
tmp->next = p;
s->next = tmp;
}
return phead;
}
//一元多項式判空
bool is_empty(NODE *phead)
{
if (phead == NULL)
{
return false;
}
return phead->next == NULL;
}
//一元多項式合併同類項
bool create_final_ploy(NODE *phead)
{
if (phead == NULL || is_empty(phead))
{
return false;
}
NODE *r = phead;
NODE *p = phead->next;
NODE *s = phead->next;
while (p->next != NULL)
{
p = p->next;
if (p->exp == s->exp)
{
p->coef = p->coef + s->coef;
r->next = p;
free(s);
}
s = p;
}
return true;
}
//輸出顯示一元多項式
void show_ploy(NODE *phead)
{
NODE *p = phead->next;
while(p != NULL)
{
if (p->coef > 0)
{
printf(" +%dx^%d ",p->coef,p->exp);
}
else
{
printf(" %dx^%d ",p->coef,p->exp);
}
p = p->next;
}
printf("\n");
}
//一元多項式合併
NODE *add_ploy(NODE *pa, NODE *pb)
{
NODE *pc = alloc_node(0,0);
NODE *p = pa->next;
NODE *s = pb->next;
NODE *r = pc;
NODE *tmp;
while(p != NULL && s != NULL)
{
if (p->exp < s->exp)
{
tmp = alloc_node(p->coef,p->exp);
r->next = tmp;
r= tmp;
p = p->next;
}
else if(p->exp > s->exp)
{
tmp = alloc_node(s->coef,s->exp);
r->next = tmp;
r= tmp;
s = s->next;
}
else
{
tmp = alloc_node(p->coef+s->coef,p->exp);
r->next = tmp;
r= tmp;
p = p->next;
s = s->next;
}
}
while(p != NULL)
{
tmp = alloc_node(p->coef,p->exp);
r->next = tmp;
r= tmp;
p = p->next;
}
while(s != NULL)
{
tmp = alloc_node(s->coef,s->exp);
r->next = tmp;
r= tmp;
s = s->next;
}
return pc;
}
//銷燬一元多項式
bool destory_ploy(NODE *phead,elem_type *coef,elem_type *exp)
{
if (phead == NULL)
{
return false;
}
NODE *p = phead;
while (p->next != NULL)
{
del_tail_ploy(phead,coef,exp);
}
free(phead);
return true;
}
//尾刪一元多項式
bool del_tail_ploy(NODE *phead,elem_type *coef,elem_type *exp)
{
if (phead == NULL || is_empty(phead))
{
return false;
}
NODE *p = phead;
while (p->next->next != NULL)
{
p = p->next;
}
*coef = p->next->coef;
*exp = p->next->exp;
free(p->next);
p->next = NULL;
return true;
}
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
//動態申請結點
NODE * alloc_node(int coef,int exp)
{
NODE * tmp = (NODE *)malloc(sizeof(NODE));
assert(tmp);
tmp ->coef = coef;
tmp ->exp = exp;
tmp->next = NULL;
return tmp;
}
//建立有序一元多項式
NODE *create_ploy()
{
NODE *phead = alloc_node(0,0);
int coef;
int exp;
NODE *tmp;
NODE *s;
NODE *p;
while(1)//同指數合併(未考慮)
{
printf("請輸入多項式(輸入0,0表示停止輸入)\n");
scanf("%d,%d",&coef,&exp);
if (coef == 0 && exp == 0)
{
break;
}
if (coef == 0)
{
continue;
}
tmp = alloc_node(coef,exp);
p = phead;
s = phead;
while(p != NULL && p->exp <= tmp->exp)
{
s = p;
p = p->next;
}
tmp->next = p;
s->next = tmp;
}
return phead;
}
//一元多項式判空
bool is_empty(NODE *phead)
{
if (phead == NULL)
{
return false;
}
return phead->next == NULL;
}
//一元多項式合併同類項
bool create_final_ploy(NODE *phead)
{
if (phead == NULL || is_empty(phead))
{
return false;
}
NODE *r = phead;
NODE *p = phead->next;
NODE *s = phead->next;
while (p->next != NULL)
{
p = p->next;
if (p->exp == s->exp)
{
p->coef = p->coef + s->coef;
r->next = p;
free(s);
}
s = p;
}
return true;
}
//輸出顯示一元多項式
void show_ploy(NODE *phead)
{
NODE *p = phead->next;
while(p != NULL)
{
if (p->coef > 0)
{
printf(" +%dx^%d ",p->coef,p->exp);
}
else
{
printf(" %dx^%d ",p->coef,p->exp);
}
p = p->next;
}
printf("\n");
}
//一元多項式合併
NODE *add_ploy(NODE *pa, NODE *pb)
{
NODE *pc = alloc_node(0,0);
NODE *p = pa->next;
NODE *s = pb->next;
NODE *r = pc;
NODE *tmp;
while(p != NULL && s != NULL)
{
if (p->exp < s->exp)
{
tmp = alloc_node(p->coef,p->exp);
r->next = tmp;
r= tmp;
p = p->next;
}
else if(p->exp > s->exp)
{
tmp = alloc_node(s->coef,s->exp);
r->next = tmp;
r= tmp;
s = s->next;
}
else
{
tmp = alloc_node(p->coef+s->coef,p->exp);
r->next = tmp;
r= tmp;
p = p->next;
s = s->next;
}
}
while(p != NULL)
{
tmp = alloc_node(p->coef,p->exp);
r->next = tmp;
r= tmp;
p = p->next;
}
while(s != NULL)
{
tmp = alloc_node(s->coef,s->exp);
r->next = tmp;
r= tmp;
s = s->next;
}
return pc;
}
//銷燬一元多項式
bool destory_ploy(NODE *phead,elem_type *coef,elem_type *exp)
{
if (phead == NULL)
{
return false;
}
NODE *p = phead;
while (p->next != NULL)
{
del_tail_ploy(phead,coef,exp);
}
free(phead);
return true;
}
//尾刪一元多項式
bool del_tail_ploy(NODE *phead,elem_type *coef,elem_type *exp)
{
if (phead == NULL || is_empty(phead))
{
return false;
}
NODE *p = phead;
while (p->next->next != NULL)
{
p = p->next;
}
*coef = p->next->coef;
*exp = p->next->exp;
free(p->next);
p->next = NULL;
return true;
}