//链式存储实现多项式
#include <stdio.h>
#include <stdlib.h>
typedef struct term *link;
struct term
{
int c; //系数
int e; //指数
link next;
};
typedef struct
{
link head; //头指针
link tail; //尾指针
}*poly_t;
link TERM(int c, int e)
{
link t = malloc(sizeof *t);
t->c = c;
t->e = e;
t->next = NULL;
return t;
}
poly_t poly_init()
{
poly_t p = malloc(sizeof *p);
p->head = p->tail = TERM(0, 0);
return p;
}
void term_read(poly_t p)
{
int i, n;
int c, e;
scanf("%d\n", &n);
for(i = 0; i < n; i++)
{
scanf("%d_%d\n", &c, &e);
p->tail = p->tail->next = TERM(c, e);
}
}
poly_t poly_destory(poly_t p)
{
link t, x;
for(t = p->head; t; free(t), t = x)
x = t->next;
free(p);
return NULL;
}
void poly_show(poly_t p)
{
link t;
if(p->head == p->tail)
return;
for(t = p->head->next; t != p->tail; t = t->next)
{
printf("%dX^%d_%c_", (t->c > 0) ? (t->c) : (-t->c), t->e, (t->next->c > 0) ? '+' : '-');
}
温馨提示:答案为网友推荐,仅供参考