数据结构作业(3_第1页
数据结构作业(3_第2页
数据结构作业(3_第3页
数据结构作业(3_第4页
数据结构作业(3_第5页
已阅读5页,还剩3页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、班级:2011级计算机科学与技术一班姓名:弥沛学号:2011222223日期:2012年9月28日题目一:整理两个一元多项式相加程序代码:stdio.hstdiib.h struct node#i nclude#i ncludetyp edefint cof,exp;struct node * next; no de;void creat( struct node *h) int x,y;struct node *r, *p;r=h;printK input x,y:);scanf( %d,%d, &x, &y); while (x !=- 999)p=( struct node*)mallo

2、c( sizeof (struct no de); p- cof =x;p-ex p=y;r-n ext =p;r=p;scanf( %d,%d, &x, &y);r- next =NULL;void outline( struct node *h)struct node *p;p=h- n ext;while (p != NULL)void add (node *h1,node *h2) int x;struct node *r, * p, *q,*s;p=h1-n ext;q=h2- n ext;r=h1;while (p !=NUL&|!=NULL)ifrrP(P - expq- exp

3、)- next =p;=P;=p-n ext;printf( %dxA%d-,p - cof,p -e xp);p=p-n ext;printf( n); elserr q elsex=p-cof +q- cof;if (x =0)/s=p;p=p- n ext;/free(s);/s=q;q=q- n ext;/free(s);elsep- cof =x; r-n ext =p; r=p;if (p - expvq- exp )- next =q;=q;=q-n ext;ififp=Fh n ext;/s=q;q=q- n ext;/free(s);(p != NULL)r-n ext =p

4、;(q!=NULL)r-n ext =q;free(h2);运行结果:void main()struct node *h1,*h2;h1 =( structnode*)malloc( sizeof (structn ode);h2 =( struct node*)malloc( sizeof (struct no de);creat(h1);outli ne(h1);creat(h2);outli ne(h2);add(h1,h2);outli ne(h1); CrJM SOFTC VuYdfiXbinwwtem p. exeinput x y: 5, 4 6, 32 4, 1 L, 0-99

5、95s*4-6?:*3-5s*2-4x*l-lx*0-input X, y: 3, 3 5, 2 1, 0-9995x*3-5x*2-lx*0-ox - 4-11k3-10x2-4k1-2x O-Press any key to continue题目将两个有序单链表合并,并保持它的有序性题目二:单链表中数据的就地逆置。1、就地逆置代码:mai n()#include #include typedef struct Nodeint data;struct Node * n ext; Node, *LinkList;void create( struct Node *head)struct Nod

6、e *p, *q;int flag =1,x;p =head;printf( input x:);while (flag)q=( struct Node*)malloc( sizeof (struct Node); scanf( %d, &x);if (x !=- 999)q-data =x;p -n ext =q;p =p- n ext;elseflag =0;p -n ext =NULL; void NIZHI(Li nkList L) Node *p, *q; p =L- n ext;L- next =NULL; while (p !=NULL) 运行结果:q=p-n ext;p- n

7、ext =L- n ext;L- n ext =p;p=q;voidLin kList l;Node *p;l =(Node* )malloc( sizeof (Node);l - next = NULL;printf(请输入链表数据,以-999 结束!n);create(l);printf(输入的单链表为:n); p = I -next;while (p !=NULL)printf( %dn, p-data); p=p-n ext; NIZHI(l);printf(逆置后的单链表为:n);p = I -next;while (p !=NULL)printf( %dn,p-data); p=p

8、-n ext;scanf(- data =x;- n ext =NULL;- n ext =p;=p;%d, &x);node * p= head-next; n);(p !=NULL)#include #include struct nodeint data;struct node *next;void create( struct node * head)/*尾插法创建链表*/struct node *p, *r=head; int x;scanf(%d, &x);while (x !=- 999)p =( struct node*)malloc( sizeof (struct no de

9、);PPrrvoid outline(struct node *head)/*链表的输出*/structprintf(whileprintf( %5d, p-data);p =p- n ext;void mergelist( struct node *head1, struct node *head2) /*将两个有序链表合并*/struct node *p, *q, *r;=head1- n ext;=head2- n ext;=head1;while (p != NUL&|!= NULL)-n ext =p;=P;=p- next;-n ext =q;=q;=q- next;if (p- data v=q-data) rrP elserrqif (q =NULL)r -n ext = p; elser -n ext =q;mai n()struct node *head1, *head2; headl =( structnode*)malloc( sizeof (struct node);head2 =( structnode*)malloc( sizeof (struct node);head1 -n ext=NULL;head2 - next =NULL; create(head1);create(head2);printsout put arran ge

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论