单链表多项式相加
来源:互联网 发布:轻兵器杂志 知乎 编辑:程序博客网 时间:2024/06/03 19:30
/* time:2014-10-06 00.17 author:halfopen version: 1*/#include <stdio.h>#include <stdlib.h>typedef struct eNode{ int b; int e; eNode *next;}LinkList;void InitList( LinkList *&L );bool InsertList( LinkList *&L, int b, int e);void DispList( LinkList *&L);LinkList* ListFind( LinkList *&L, int e);void Add( LinkList *&L1, LinkList *&L2);int main(){ LinkList *L1,*L2; int b,e; InitList( L1 ); InitList( L2 ); puts("Add ax^b to L1,please input \"a b\""); while(scanf("%d%d",&b,&e) == 2) { if( 0 == b)break; InsertList( L1, b, e); DispList( L1); puts("input \"a b\""); } puts("Add ax^b to L2,please input \"a b\""); while(scanf("%d%d",&b,&e) == 2) { if( 0 == b)break; InsertList( L2, b, e); DispList( L2); puts("input \"a b\""); } printf("End inputs:\n"); printf("1: ");DispList( L1); printf("2: ");DispList( L2); puts("Add:"); Add( L1, L2); DispList( L1); return 0;}void Add( LinkList *&L1, LinkList *&L2){ LinkList *p = L2->next; while(p!=NULL) { InsertList( L1, p->b,p->e); p = p->next; } //DispList(L1);}void InitList( LinkList *&L ){ L = ( LinkList *)malloc( sizeof(LinkList)); L->b = 0; L->e = 0; L->next = NULL;}bool InsertList( LinkList *&L, int b, int e){ LinkList *p = L, *s; while(p->next != NULL) { if(e == p->e)//1 { p->b += b; return false; } if(p->next != NULL && p->next->e <e)break;//move to the right place p = p->next; if(p->next == NULL&& e == p->e)//2 { p->b += b; return false; } } s = (LinkList *)malloc( sizeof( LinkList)); //create a new node ,and insert it after p if(p->next != NULL) { s->next = p->next; //link to the next node if it exists } else s->next = NULL; p->next = s; s->b = b; s->e = e; return true;}LinkList* ListFind( LinkList *&L, int e)// find e in L{ LinkList *p = L->next; while(NULL != p->next) { if( e == p->e)return p; p = p->next; } return NULL;}void DispList( LinkList *&L)//ok{ LinkList *p = L->next; while( p!= NULL) { //printf("BE%d %d \n",p->b,p->e); if(p->b != 1)printf("%d",p->b); // x if(p->e != 0) // if p->e == 0, it should be only a number { if(p->e == 1)printf("x"); else printf("x^%d",p->e); } if(p->b == 1 && p->e == 0)printf("1"); if( p->next !=NULL)printf(" + ");// if it need to add + else{ puts("");break;} p = p->next; } }
0 0
- 多项式相加 单链表实现
- 单链表多项式相加
- 单链表多项式相加
- 单链表实现多项式相加
- 多项式相加单链表实现
- 单链表实现多项式相加
- 单链表实现一元多项式相加
- 多项式相加
- 多项式相加
- 多项式相加
- 多项式相加
- 多项式相加
- 多项式相加
- 多项式相加
- 多项式相加
- 多项式相加
- 多项式相加
- 多项式相加
- 我读过的最好的epoll讲解
- javascript 正则表达式验证身份证号码
- TCP 的那些事儿(上)
- TCP 的那些事儿(下)
- Python SocketServer
- 单链表多项式相加
- leo学习系列之五——leo基础用法@root
- SVG中一些较为详细的文档
- HDU 1754 I Hate It (线段树 & 树状数组)
- void LocateNode(DLinkList *&L, ElemType x)
- 类似qq空间点击就放大图片的代码
- void ConList(LinkList *&ha, LinkList *&hb, LinkList *&hc)
- leo学习系列之六——leo事例:一个文档的组织结构
- 手游运营相关