欢迎来到天天文库
浏览记录
ID:16473669
大小:37.00 KB
页数:7页
时间:2018-08-10
《建立单链表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.
此文档下载收益归作者所有