多项式加法(单链表 c语言)
2014-03-31 20:29
387 查看
#include<stdio.h> #include<malloc.h> #include<stdlib.h> typedef struct pnode { float coef; int expn; struct pnode *next; } pnode; pnode * creat() { float input_coef; int input_expn; pnode *head=NULL; pnode *prev, *current; while(scanf("%f%d",&input_coef,&input_expn)&&(input_expn>=0)) { current=(pnode *)malloc(sizeof(pnode)); if(head==NULL) head=current; else prev->next=current; current->next=NULL ; current->coef=input_coef; current->expn=input_expn; prev=current; // printf("input coef,expn:\n"); } return head; } void display(pnode *head) { pnode *current; current=head; while(current!=NULL) { printf("%.1f %d\n",current->coef,current->expn); current=current->next; } printf("\n"); } pnode * add(pnode *heada,pnode *headb) { pnode *headc=NULL; pnode *p=heada,*q=headb; pnode *current,*prev; while(p!=NULL&&q!=NULL) { current=(pnode *)malloc(sizeof(pnode)); if(headc==NULL) headc=current; else prev->next=current; current->next=NULL ; if(p->expn==q->expn ) { //printf("%d\n",p->expn); if(p->coef+q->coef!=0) { current->coef=p->coef+q->coef; current->expn=p->expn; prev=current; } q=q->next; p=p->next; } else if(p->expn>q->expn) { //printf("%d %d\n",p->expn,q->expn); current->coef=p->coef; current->expn=p->expn; prev=current; p=p->next; } else { //printf("%d %d\n",p->expn,q->expn); current->coef=q->coef; current->expn=q->expn; prev=current; q=q->next; } } while(p!=NULL) { current=(pnode *)malloc(sizeof(pnode)); if(headc==NULL) headc=current; else prev->next=current; current->next=NULL; current->coef=p->coef; current->expn=p->expn; prev=current; p=p->next; } while(q!=NULL) { current=(pnode *)malloc(sizeof(pnode)); if(headc==NULL) headc=current; else prev->next=current; current->next=NULL; current->coef=q->coef; current->expn=q->expn; prev=current; q=q->next; } return headc; } void BubbleSort(pnode *head) { pnode *i=NULL,*j=NULL; int temp_expn; float temp_coef; for(i = head; i!= NULL; i = i -> next) for(j=i->next; j!=NULL; j=j->next) if(i->expn<j->expn) { temp_expn=j->expn; temp_coef=j->coef; j->coef=i->coef; j->expn=i->expn; i->expn=temp_expn; i->coef=temp_coef; } } int Delete(pnode *head) { pnode *temp; while(head!=NULL) { temp=head->next; free(head); head=temp; } if(head==NULL) return 1; } int main() { freopen("input.txt","r",stdin); pnode * a,*b,*c; printf("input the first:\n"); a=creat(); BubbleSort(a); printf("input the second:\n"); b=creat(); BubbleSort(b); c=add(a,b); printf("the first:\n"); display(a); printf("the second:\n"); display(b); printf("sum is:\n"); display(c); Delete(a); Delete(b); Delete(c); return 0; }
相关文章推荐
- C语言:用链表实现一元多项式的加法
- 多项式加法利用链表实现(c语言)
- 链表实现多项式加法和乘法(C语言实现)
- 用单向链表实现一元多项式加法 推荐
- 数据结构作业 ------ 用链表实现简单多项式加法
- 多项式乘法与加法运算(链表实现)
- 单链表应用之多项式加法
- 习题3.6 用链表求多项式的加法
- 链表的多项式加法链表的多项式加法
- PAT 3-04 一元多项式的乘法与加法运算(C语言实现)
- 一元多项式的乘法与加法运算——链表实现
- 多项式的加法运算实现(利用链表结构)
- 链表实现多项式加法和乘法
- 链表的应用:单元多项式的加法、减法、乘法
- 数据结构第二次上机实验【链表实现多项式的加法和乘法】
- PAT 一元多项式的乘法与加法运算(链表 c++版)
- 多项式加法,链表方法实现
- 【数据结构_链表_List_1040】一元多项式加法运算的实现
- 用链表实现一元多项式的加法运算
- PAT 一元多项式的乘法与加法运算(链表 c++版)