静态链表的创建以及基本操作
2013-09-26 23:28
447 查看
#include <stdio.h> #include <stdlib.h> #define Max 100 typedef int ElemType; /*定义每个节点的数据信息*/ typedef struct Node { ElemType data;//节点中存放的抽象数据 int next;//记录下一个节点的位置(位置由下标唯一决定)这里的next就是下个元素在结构体数组中的下标 }SLNode; /*静态链表的定义*/ typedef struct Static_List { SLNode store[Max];//静态的申请了连续的Max个结构体大小的空间 int free_h;//首个空节点的下标,类比如链表中的入口地址 }Sqlist; int main() { int Init_Static_List(Sqlist *l); int Creat_Static_List(Sqlist *l); int Length_List(Sqlist l); int Judge_List_Empty(Sqlist l); int Judge_List_Full(Sqlist l); int Insert_List(Sqlist *l); int Research_List(Sqlist l); int Delete_List(Sqlist *l); void Show_List(Sqlist l); Sqlist SL; Init_Static_List(&SL);//静态链表的初始化操作 Creat_Static_List(&SL);//创建静态链表 /*调用插入函数,判断插入位置是否合法,不合法则重新输入*/ while(-1==Insert_List(&SL)) { printf("The position you insert is illegal!\nPlease input the right position:\n"); Insert_List(&SL); } /*提示链表的状态。为空或者满或者非空非满*/ if(0==Judge_List_Empty(SL)) { printf("Current List is empty!\n"); } else { printf("Current List is not empty!\n"); if(1==Judge_List_Full(SL)) printf("Current List is full!\n"); else printf("The length of current list is:%d\n",Length_List(SL)); } /*调用数据查找函数,地址不合法则重新输入*/ if(-1==Research_List(SL)) { printf("POSITION ERROR\nPlease input the position again\n"); Research_List(SL); } /*调用删除元素函数,地址不合法则重新输入*/ if(-1==Delete_List(&SL)) { printf("POSITION ERROR\nPlease input the position again\n"); Delete_List(&SL); } /*显示静态链表的内容*/ Show_List(SL); return 0; } int Init_Static_List(Sqlist *l) { int i; (*l).store[0].next=-1;//(1)静态的链表刚刚建立的时候其头节点也是尾节点,而尾节点是以其中的next元素为-1定义 (*l).free_h=1;//链表刚刚建立的时候,所有的空间都是空的,第一个为空的节点下标为1,free_h赋值为1 for(i=(*l).free_h;i<=Max-2;i++) {(*l).store[i].next=i+1;} (*l).store[Max-1].next=-1;//(1)中的-1表示的是数据元素空间的结尾,而在这里指的是空闲空间的结尾 printf("The static list initialed success!!!\n"); return 0; } /*创建静态链表(这里运用的是尾插法)*/ int Creat_Static_List(Sqlist *l) { int n,i,tail=0; printf("Please input the length of the static list:"); scanf("%d",&n); for(i=1;i<=n;i++) { printf("Please input NO.%d's data:",i); scanf("%d",&(*l).store[(*l).free_h].data);/*首先为空闲链表的第一个元素赋值,它将被接到原先的表尾*/ /*将(*l).free_h看成一个整体,它代表空闲链表的首个元素地址,同时也是空闲链表的入口参数*/ (*l).store[tail].next=(*l).free_h;/*将取出的首个元素接到非空闲链表的表尾*/ tail=(*l).free_h;/*表尾移动,新插入的元素为新的表尾*/ (*l).free_h=(*l).store[tail].next;/*空闲链表的原先的第一个元素被取走了,所以起始元素的地址向下移动一个单位*/ (*l).store[tail].next=-1;/*表尾指向-1,这是表尾的标志*/ } return 0; } /*求长函数的定义*/ int Length_List(Sqlist l) { int count=0; int i=0; while(-1!=l.store[i].next) { i=l.store[i].next;//i为下一个元素的地址,相当于后移节点,继续判断 count++;//说明这个节点不为空,count自增一直到循环到链表的结尾,此时得到的就是链表的长度了 } return count;//将链表的长度返回 } /*判空函数定义*/ int Judge_List_Empty(Sqlist l) { if(-1!=l.store[0].next) return -1;//说明此时的静态链表不为空,首节点的next不等于-1,与上述的(1)相对应 return 0; } /*判满函数定义*/ int Judge_List_Full(Sqlist l) { if(Max-1==Length_List(l)) return 1;//说明此时的静态链表为满 return 0; } /*静态链表显示函数*/ void Show_List(Sqlist l) { int i=0,k=1; i=l.store[i].next;//得到第一个元素的地址 while(-1!=l.store[i].next) { printf("No.%d 's data is %d\n",k++,l.store[i].data); i=l.store[i].next; }/*最后一个数据是没有被输出出来的,所以以下对于最后一个元素做了单独的输出*/ printf("No.%d 's data is %d\n",i,l.store[i].data); printf("There is no data left\n"); } /*静态链表插入数据函数*/ int Insert_List(Sqlist *l) { int n,i=1,q,p=0; int k=1; printf("Please input the position of the new data:"); scanf("%d",&n); if(n<1||n>Length_List(*l)+1) return -1;//判断插入的位置是否合法 for(i=1;i<n;i++) {p=(*l).store[p].next;}//找到插入位置的上一个位置 q=(*l).free_h;//找到空闲链表的首位置,给新元素预留 printf("Please input the new data:"); scanf("%d",&(*l).store[q].data);//在预留的位置上赋值新元素的内容 (*l).free_h=(*l).store[q].next;//空闲链表首位置下移 (*l).store[q].next=(*l).store[p].next;//将指定位置的上个元素的next位置改给新元素 (*l).store[p].next=q;//将新元素接入到指定位置的上一个元素后面 return 0; } int Research_List(Sqlist l) { int n,i,p=0; printf("Please input the position of the data be searched:"); scanf("%d",&n); if(n<1||n>Length_List(l))/*判断查找位置的合法性*/ return -1; else { for(i=1;i<=n;i++) {p=l.store[p].next;}//循环查找到相应的位置 printf("The data you rearched is :%d\n",l.store[p].data); } return 0; } int Delete_List(Sqlist *l) { int i,n,p=0,q; printf("Please input the delete_data's position:"); scanf("%d",&n); if(n<1||n>Length_List(*l)) return -1;//判断删除的位置是否合法 for(i=1;i<n;i++) {p=(*l).store[p].next;}//找到删除位置的上一个位置 q=(*l).store[p].next;//找到删除的元素位置 (*l).store[p].next=(*l).store[q].next;//将删除元素的上一个元素的next指向删除元素的下一个位置 (*l).store[q].next=(*l).free_h;//将删除的元素的空间加入到空闲链表的头部 (*l).free_h=q;//空闲链表的起始地址改变为新加入的删除元素的位置 return 0; }
相关文章推荐
- 二叉树的基本操作精集(创建、遍历、求深度结点以及叶子结点个数)
- 队列的创建以及基本操作
- 利用Linux文件编辑器vi创建文件以及vi的最基本操作
- 【mysql】表的创建以及基本操作
- 嵌入式 QT创建数据库以及数据库基本操作问题
- 创建repository上传到github以及基本操作
- ORACLE创建表空间以及增加用户基本权限操作
- 创建数据库以及基本操作
- 【数据库】创建表以及一些基本的表操作
- 递归创建二叉树以及一些基本操作
- 创建线性表,以及表中的基本操作
- 栈的创建 以及基本操作出栈 入栈,判空
- Maven学习记录之maven基本操作命令,maven本地工厂的创建,maven骨架的生成,以及在eclipse中创建maven工程:
- MySql之DDL操作创建表(添加主键, 外键约束以及基本的数据类型)
- MySql入门之一:DDL操作创建表(添加主键, 外键约束以及基本的数据类型)
- 树的基本结构,以及利用链表实现树的各项操作(创建、添加/删除/打印树节点、销毁等等)
- 8天掌握EF的Code First开发系列之3 管理数据库创建,填充种子数据以及LINQ操作详解
- C语言实现链表的创建、增加、删除、查询、逆序、清空等基本操作
- 创建表的基本操作
- PHP创建文件(夹)以及目录操作