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

付费下载

下载本文档

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

文档简介

1、日期:2012年9月28日题目一:单链表的删除、插入。1、删除代码:stdio.h stdiib.h struct node#i nclude#i nclude typ edef int data;struct node * next; Node; void creat( struct node *head) struct node *p, *q;int flag =1,x;p =head;printf( input x:);while (flag)q=( struct node*)malloc( sizeof (struct no de);%d, &x);if (x !=- 999)q-da

2、ta =x;p -n ext =q;p =p- n ext;elseflag =0;p -n ext =NULL;void outline( struct node *h)struct node *p;p =h- n ext;while (p !=NULL)printf( %d , p-data); p =p- n ext;void linkdel( struct node *h, int Node *p, *s;int j =0;p=h;s=p;while (p !=NUL& n ext; j +;if (! (p - next)printf();删除的位置不合理!s=p-n ext; p-

3、 n ext =s- n ext;free(s);i)node *h;mai n() structint i;h =( struct node*)malloc( sizeof (struct no de);h- next=NULL;creat(h);pnntf(ninput i node :scanf( %d, &);lin kdel(h,i);outl in e(h););运行结果:插入代码:#include stdio.h#include stdlib.h typ edef struct node int data; struct node * next; Node;void creat(

4、 struct node *head)struct node *p, *q;int flag =1,x;p =head;printf( input x:);while (flag)q=( struct node*)malloc( sizeof (struct no de); scanf( %d, &);if (x !=- 999)q-data =x;p -n ext =q;p =p- n ext;elseflag =0;p -n ext =NULL;void outline(struct node *h)struct node *p;p =h- next;while (p !=NULL)pri

5、ntf( %d ,p -data);p =p- n ext;void linkins(Node*h, int i, int x)Node *p, *s;int j =0;P =h;while (p !=NUL&j n ext;j +;if (i j & data =x; s- n ext =p- n ext; p-n ext =s;mai n() struct int i,x; printf(node *h;inp ut numbers (输入-999时停止):); h=( struct *)malloc( sizeof (struct no de);运行结果:nodeh- next =NUL

6、L; creat(h);pnntf( ninput i node number:);scanf( %d,%d, &, &x); lin ki ns(h,i,x); outl in e(h);I C;V M SOFICVu Va nVbi nwwtem p.exeinputinput1 2 3numbers: input k:1 2 3 4 o 6 -999i node 和 x number;, S4 8 5 6Press any key to continue. C:JMiSOFTCVuiYdnbinwwtemp.exeinput numbers输入-999时停止):input 掘:1 3 3

7、 4 5-999input i node 和 x number:0 88 12 3 4 5 6Press any key to continue : C:JMS-OFTCVijVflrttjirtwwlenipLexeinput numhiETE【输入-gggEzJ : input x: 1 2 3 4 5 6 -999input i node 和蓋 number:S, 81 2 3 4 5 3 6Hess any key to continuE.题目:I对无序链表排序#include #include struct nodeint date;struct node *next; ;void

8、creat( struct node *head) struct node *m,* n;int x;n=head;printf(请输入无序的数据:n);scanf( %d, &x);while (x !=- 999)mF( struct node*)malloc( sizeof (struct no de);n- date fx;n-next Fm;nFm;scanf( %d, &x);n- next fNULL;struct node arrange( struct node *head)struct node *p, *q,*r, *t; r Fheac- next;t Fr - nex

9、t;while (t 匸NULL)p二head; q =head-n ext;while (q != r &8q- date date) p=q;q =q- next; if (q=r&Sr-date date) r-next =t;r=t;t =t - n ext;r -next =NULL;else p- next =t;P=t;t =t - n ext;p-next =q;void out( struct node * head) struct node * m;m=heac- next;while (m!=NULL)printf( %d ,m-date); m=m next;void

10、main()struct node head=( struct *)malloc( sizeof node );*head; node (structhead- next =NULL; creat(head);arran ge(head); out(head);4jljIF蠢M*q 疇 薨轸懿瘵謡鑲亠3vSl丿嗨屮II.W1J厶IIr癒/ JwFbdJ弓IIIIIIIIIIIIIIIL詡III.w .JsIII九题目二:对无序单链表中数据的进行排序。1、排序代码:#inelude #include struct nodeint data;struct node *next;- data =x;

11、- next=NULL;- n ext =p;=p;%d, &x);=head; =p- n ext;(q != NULI&q-data data) =q;=q- n ext;=p-n ext;- n ext =r - n ext;-n ext =p;=u;whilevoid 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);PPrrscanf( void outline( struct node *head)struct node * p =head- next;printf( n);while (p != NULL)printf( %5d, p-data);p =p- n ext;void arrange( struct node * head)struct node *q, *r, *p, *u; p =head- n ext;head -next =NULL;while (p !=NULL)rquPrP int

温馨提示

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

评论

0/150

提交评论