数据结构之线性表的链式表示及其实现
来源:互联网 发布:linux安装vnc客户端 编辑:程序博客网 时间:2024/04/27 18:04
记录一下自己学习过程中写的代码。以下是我看严蔚敏老师的数据结构教材后,结合教材所讲用C语言实现了关于线性表链式表示及其实现的基本操作,供以后复习所用。
编译软件:VC++6.0
编译软件:VC++6.0
测试用例结果截图如下:
源代码如下:
/********************************** 线性表的链式表示和实现(完整代码,C实现)Author:大地在我腳下 Date:2016-7-25Email:jsrcdjcyy@163.com **********************************/ #include<stdio.h> #include<stdlib.h> typedef struct LNode { int data; struct LNode *next; }LNode, *LinkList; LinkList CreateList(int); //初始化一个单链表 void ClearList(LinkList); //清除单链表 void DestroyList(LinkList);//线性表L已存在情况下,执行销毁线性表L操作 int LengthList(LinkList); //求单链表长度 bool EmptyList(LinkList); //判断单链表是否为空表 int GetList(LinkList,int); //取单链表第某个位置上的元素 bool InsertList(LinkList,int,int);//向单链表插入一个元素 bool DeleteList(LinkList,int);//从单链表中删除一个元素void TraverseList(LinkList); //遍历单链表并输出 void BubsortList(LinkList); //对链表内数据进行冒泡排序void main(){int n,len,m,h,k,t; LinkList LHead=NULL;//创建链表,排序并遍历输出 printf("Please input the original number of the linear list:"); //请输入线性表元素的原始个数 scanf("%d",&n); printf("\n"); LHead=CreateList(n); BubsortList(LHead); printf("After sorting and traversing,now the datas in the list are:\n"); //经过排序后遍历输出链表数据为 TraverseList(LHead);//输出链表的原始长度len=LengthList(LHead);if(!EmptyList(LHead)) printf("Now the current length of the list is:%d\n",len);printf("\n");//取单链表第某个位置上的元素 printf("Please input the location of the element which should be taken out:"); //请输入要取出的元素的位置scanf("%d",&m);if(m>n) { printf("Input error!please check your input!");//所要查找的元素位置不存在,请检查输入! printf("\n"); printf("Please input the location again:"); //请输入要取出的元素的位置 scanf("%d",&m);}printf("\n");printf("Congratulations!input correct!\n");printf("The element which should be taken out is:%d\n",GetList(LHead,m)); //要取出的元素值为printf("\n");//向链表中插入数据,插入后输出当前链表长度并遍历输出链表数据printf("Please input the being inserted number and its location:"); //请输入要插入的位置和要插入的数scanf("%d%d",&h,&k);printf("\n");if(InsertList(LHead,h,k)) printf("Insert succeed!\n");else printf("Insert failed!\n");printf("\n");printf("Now the datas in the list are:\n"); TraverseList(LHead);len=LengthList(LHead);if(!EmptyList(LHead)) printf("Now the current length of the list is:%d\n",len);printf("\n");//从链表中删除数据,并重新遍历输出链表数据及其长度printf("Please input the element's location which must be deleted:"); //请输入要删除的位置scanf("%d",&t);printf("\n");if(DeleteList(LHead,t)) printf("Delete succeed!\n");else printf("Delete failed!\n");printf("\n");printf("Now the datas in the list are:\n"); TraverseList(LHead); len=LengthList(LHead);if(!EmptyList(LHead)) printf("Now the current length of the list is:%d\n",len);//判断单链表是否为空表 if(!EmptyList(LHead)) printf("Now the list has numbers!\n");else printf("Now the list is empty!\n");//清空链表,遍历后输出(实际上并无数据输出) ClearList(LHead);printf("\nAfter clearing,the list is:");TraverseList(LHead);printf("\n");//判断单链表是否为空表 if(!EmptyList(LHead)) printf("Now the list has numbers!");else printf("Now the list is empty!");printf("\n");}//逆序输入n个元素值,建立带头结点的单链线性表L并返回头指针LinkList CreateList(int n){LinkList LHead,p;int i;LHead=(LinkList)malloc(sizeof(LNode));LHead->next=NULL; //先建立一个带头结点的单链表if(LHead==NULL){printf("Malloc failed!"); exit(-1);}printf("Please input the linear list:");//请输入链表各结点值for(i=n;i>0;--i) { p=(LinkList)malloc(sizeof(LNode)); //生成新结点 if(p==NULL) {printf("Malloc failed!"); exit(-1); } scanf("%d",&p->data); p->next=LHead->next; LHead->next=p; }printf("\n");return LHead;} //清除单链表 void ClearList(LinkList L){LinkList p=L->next; L->next=NULL; //头指针指针域为空 DestroyList(p); //销毁p所指的单链表}//线性表L已存在情况下,执行销毁线性表L操作 void DestroyList(LinkList L){LinkList q; while(L) //当L非空的时候,一直执行循环操作 {q=L->next; free(L); L=q; }}//求单链表长度 int LengthList(LinkList L) {LinkList q=L->next; int count=0; while(q) //当L非空的时候,一直执行循环操作 { count++;q=q->next; }return count;}//判断单链表是否为空表 bool EmptyList(LinkList L) {if(L->next) return false; else return true;}//取单链表第某个位置上的元素 int GetList(LinkList L,int pos) {LinkList p=L->next;int j=1;while(p&&j<pos) {p=p->next; j++; }if(!p||j>pos) return 0;return p->data;}//向单链表某处插入一个元素 bool InsertList(LinkList L,int e,int pos) {LinkList q,p=L->next;int i=1;if(pos>LengthList(L)) {printf("Input error!please check the input again!");//所要查找的元素位置不存在,请检查输入! return false; }while(p&&i<pos-1) {p=p->next; i++; }if(!p||i>pos) return false;q=(LinkList)malloc(sizeof(LNode));if(q==NULL){printf("Malloc failed!"); exit(-1);}q->data=e;q->next=p->next;p->next=q;return true;}//从单链表中删除某位置元素bool DeleteList(LinkList L,int pos) {LinkList q,p=L->next;int i=1;if(pos>LengthList(L)) {printf("Input error!please check the input again!");//所要查找的元素位置不存在,请检查输入! return false; }while(p&&i<pos-1) {p=p->next; i++; }if(!p||i>pos) return false;q=p->next;p->next=p->next->next;free(q);q=NULL;return true;}//遍历单链表并输出 void TraverseList(LinkList L) {LinkList p=L->next; while(p) {printf("%d",p->data); p=p->next;putchar(32); }printf("\n");}//对链表内数据进行冒泡排序void BubsortList(LinkList L) {LinkList p,q;int temp; for(p=L->next;p;p=p->next) for(q=p->next;q;q=q->next) if(p->data>q->data) {temp=p->data; p->data=q->data; q->data=temp; }}
0 0
- 数据结构之线性表的链式表示及其实现
- 数据结构之线性表的链式表示和实现
- 数据结构之栈的链式表示及其实现
- 数据结构之队列的链式表示及其实现
- 数据结构—线性表的链式表示和实现
- 数据结构---线性表的链式表示与实现
- 数据结构学习----线性表的链式表示(Java实现)
- 线性表的链式表示和实现(数据结构)
- 《数据结构》线性表的链式表示和实现(三)
- 数据结构(2):线性表的链式表示和实现
- 数据结构--线性表的链式表示与实现
- 数据结构---线性表的链式表示和实现(一)
- 数据结构---线性表的链式表示和实现(二)
- 数据结构学习----线性表的链式表示之升序排序的单链表(Java实现)
- 数据结构学习----线性表的链式表示之循环单链表(Java实现)
- 数据结构学习----线性表的链式表示之循环双链表(Java实现)
- 《数据结构》进行曲(三)线性表的链式表示和实现之二
- 线性表及其链式表示
- HDU1285确定比赛名次(拓扑排序)
- php+yar+apache+windows7
- jquery元素遍历
- 通讯录(静态版)一
- easyui-combobox 绑定数据的俩中方式
- 数据结构之线性表的链式表示及其实现
- Android自定义View:如何实现一个模拟时钟?
- hdu1013-字符相加
- HDU 1443 Joseph
- 简表报表配置以及ftl文件的一些功能
- ios-anchorPoint、position理解
- 58.leetcode Length of Last Word(easy)[字符串 分割]
- Hexo结合Github创建静态博客
- 浅析Java中的final关键字