线性表实践-多项式(一)
多项式的实现有两种方式:
一种为顺序存储结构的顺序表,另一种为线性表的链式存储的链表,本文采用后者。
1。链表创建以及初始化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| #include <stdio.h> #include <stdlib.h> typedef struct Polynode{ int coef; int exp; struct Polynode *next; } Polynode,*Polylist; Polylist PolyCreat(){ Polynode *head,*rear,*s; int c,e; head = (Polynode*)malloc(sizeof(Polynode)); rear = head; scanf("%d %d",&c,&e); while(c!=0){ s = (Polynode*) malloc(sizeof(Polynode)); s->coef = c; s->exp = e; rear->next = s; rear =s; scanf("%d %d",&c,&e); } rear->next = NULL; return(head); } void print(Polynode *head){ Polynode *current = head; while(current != NULL){ printf("%d*x^%d ", current->coef, current->exp); current = current->next; } printf("\n"); } int main(){ Polylist la = NULL; la = PolyCreat(); print(la); Polynode *temp;
while (la!=NULL){ temp = la; la = la->next; free(temp); }
return 0; }
|