建立单链表c语言程序2

建立单链表c语言程序2

ID:16473669

大小:37.00 KB

页数:7页

时间:2018-08-10

建立单链表c语言程序2_第1页
建立单链表c语言程序2_第2页
建立单链表c语言程序2_第3页
建立单链表c语言程序2_第4页
建立单链表c语言程序2_第5页
资源描述:

《建立单链表c语言程序2》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库

1、实现功能:1.创建一个新链表。2.插入节点。3.删除节点。4.插入法排序链表(从小到大)。5.选择法排序链表(从小到大)。6.显示当前链表。0.退出程序。.../*单向链表的相关操作*//*2010.03.27*/..#include"stdio.h"#include"malloc.h"#include"stdlib.h"#definenull0intlength;structList{intdata;structList*next;}List;structList*InitList(){stru

2、ctList*head,*p,*q;intd;printf("创建一个链表:");head=(structList*)malloc(sizeof(structList));head->next=null;q=head;scanf("%d",&d);while(d<=0){  printf("您所输入的数据有误,请重新输入");  scanf("%d",&d);}while(d>0){  p=(structList*)malloc(sizeof(structList));  

3、p->next=null;  p->data=d;  q->next=p;  q=p;  scanf("%d",&d);  length++;}returnhead;}voidListInsert(structList*head,inti,inte){structList*p,*q;intj=1;if(i>0&&i<=length+1&&e>0){  p=head;  while(jnext;   j++;  }  q=(structList*)malloc(sizeo

4、f(structList));  q->data=e;  q->next=p->next;  p->next=q;  length+=1;}elseprintf("对不起您所输入的节点位置或数据出错,数据插入失败!");}intListDelete(structList*head,inti,inte){structList*p,*q;intj=0;if(i>0&&i<=length){  p=head;  while(jnext;   j++;  }  e=

5、p->next->data;  q=p->next;  p->next=q->next;  free(q);  length-=1;  returne;}else{   printf("对不起您所输入的节点位置出错,数据删除失败!");   returnnull;  }}voidListInsertSort(structList*head){structList*p,*q,*s;q=head->next->next;head->next->next=null;while(q){  s=q-

6、>next;  p=head;  while(p->next&&p->next->datadata)  p=p->next;  q->next=p->next;  p->next=q;  q=s;}}voidListChooseSort(structList*head){structList*p,*q;intt;for(p=head->next;p->next;p=p->next)   for(q=p->next;q;q=q->next)  if(p->data>q->data)    {

7、   t=p->data;   p->data=q->data;   q->data=t;   }}voidprint(structList*head){structList*p;if(head){  p=head->next;  while(p)  {   printf("%5d",p->data);   p=p->next;  }  printf("");}}voidmain(){structList*head;inti,e,n;head=null;do{  system("cls"); 

8、 printf("                         单向链表的相关操作");  printf("                         1.创建一个新链表。");  printf("                         2.插入节点。");  printf("                         3.删除节点。");  printf("                         4.

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。