静态链表

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3
  4 //typedef 80 MAXSIZE;
  5 #define MAXSIZE 20
  6
  7 typedef struct Node{
  8     int data;
  9     int cursor;
 10 }Node,StaticList[MAXSIZE];
 11
 12 void InitialSpace(StaticList Space){
 13     int i;
 14     for(i = 0;i < MAXSIZE - 1; i++){
 15         Space[i].cursor = i + 1;
 16     }
 17     Space[MAXSIZE - 1].cursor = 0;
 18 }
 19 int Malloc_StaticList(StaticList Space){
 20     int index;
 21     index = Space[0].cursor;
 22     if(index){
 23         Space[0].cursor = Space[index].cursor;
 24         return index;
 25     }
 26     else{
 27         printf("Space Full, Failed!\n");
 28         exit(1);
 29     }
 30 }
 31 void Free_StaticList(StaticList Space,int index){
 32     Space[index].data = 0;
 33     Space[index].cursor = Space[0].cursor;
 34     Space[0].cursor = index;
 35 }
 36 int InitialStaticList(StaticList Space){
 37     int i;
 38     i = Malloc_StaticList(Space);
 39     Space[i].cursor = 0;
 40     return i;
 41 }
 42 void ClearStaticList(StaticList Space,int n_List){
 43     int temp_n_List = n_List;
 44     n_List = Space[n_List].cursor;
 45     int t_cursor;
 46     while(n_List != 0){
 47         t_cursor = Space[n_List].cursor;
 48         Space[n_List].data = 0;
 49         Free_StaticList(Space,n_List);
 50         n_List = t_cursor;
 51     }
 52     Space[temp_n_List].cursor = 0;
 53     printf("Clear Static List Executed!\n");
 54 }
 55 void StaticListEmpty(StaticList space,int n_List){
 56     if(space[n_List].cursor)
 57         printf("No Empty\n");
 58     else
 59         printf("Empty!\n");
 60 }
 61 int StaticList_Length(StaticList Space,int n_List){
 62     int count = 0;
 63     while(Space[n_List].cursor){
 64         count++;
 65         n_List = Space[n_List].cursor;
 66     }
 67     return count;
 68 }
 69 void InsertStaticList(StaticList Space,int n_List,int position,int e){
 70 /*
 71  *
 72  *
 73  * */
 74     int length = StaticList_Length(Space,n_List);
 75     int copy_n_List = n_List;
 76     if(position < 1 || position > length + 1){
 77         printf("Insert funtion ERROR: position\n");
 78         exit(1);
 79     }
 80     int new_i = Malloc_StaticList(Space);
 81     if(new_i){
 82         Space[new_i].data = e;
 83         int count = 0,i;
 84         for(i = 1; i < position; i++){ //
 85             n_List = Space[n_List].cursor;
 86         }
 87         Space[new_i].cursor = Space[n_List].cursor;
 88         Space[n_List].cursor = new_i;
 89         printf("INFORMATION: Insert %d to List%d with position=%d executed!\n",e,copy_n_List,position);
 90     }
 91 }
 92 void DeleteStaticList(StaticList space,int n_List,int position){
 93     int length = StaticList_Length(space,n_List);
 94     int t_n_List = n_List;
 95     if(position < 1 || position > length){
 96         printf("function DeleteSataicList Error:position!\n");
 97     }
 98     else
 99     {
100         int count = 0,result;
101         while(count < position - 1){
102             n_List = space[n_List].cursor;
103             count++;
104         }
105         result = space[space[n_List].cursor].data;
106         printf("deleteSataisList executed: element %d in position %d of List%d was deleted\n",result,position,t_n_List);
107         int temp = space[n_List].cursor;
108         space[n_List].cursor = space[space[n_List].cursor].cursor;
109         Free_StaticList(space,temp);
110     }
111 }
112 int GetStaticListElement(StaticList space,int n_List,int position){
113     int count = 0;
114     int temp_n_List = n_List;
115     while(space[n_List].cursor && count < position){
116         n_List = space[n_List].cursor;
117         count++;
118     }
119     if(space[n_List].cursor == 0){
120         printf("INFORMATION:get staticList element Error: position!\n");
121     }
122     else{
123         int e;
124         e = space[n_List].data;
125         printf("GetStaticListElement with position=%d from List%d executed! the result is %d\n",position,temp_n_List,e);
126     }
127 }
128 int LocateStaticList(StaticList space,int n_List,int e){
129     n_List = space[n_List].cursor;
130     while(n_List){
131         if(space[n_List].data == e){
132             return n_List;
133         }
134         n_List = space[n_List].cursor;
135     }
136     return 0;
137 }
138 void NextElement_SL(StaticList space,int n_List,int currentElement){
139     if(LocateStaticList(space,n_List,currentElement) == 0)
140         printf("function NextElement Error:currentElement%d dose not exist\n",currentElement);
141     else if(space[LocateStaticList(space,n_List,currentElement)].cursor == 0)
142             printf("function NextElement Error:the currentElement%d is last one of list%d\n",currentElement,n_List);
143     else
144         printf("the nextElement of %d is %d\n",currentElement,space[space[LocateStaticList(space,n_List,currentElement)].cursor].data);
145 }
146 void PriorElement_SL(StaticList space,int n_List,int currentElement){
147     if(!LocateStaticList(space,n_List,currentElement))
148         printf("function PriorElement Error:currentElement%d does not exist\n",currentElement);
149     else if(space[n_List].cursor == LocateStaticList(space,n_List,currentElement))
150         printf("function PriorElement Error:the currentElement%d is the first of the List%d\n",currentElement,n_List);
151     else{
152         n_List = space[n_List].cursor;
153         int temp;
154         while(space[n_List].data != currentElement){
155             temp = n_List;
156             n_List = space[n_List].cursor;
157         }
158         printf("the PriorElement of %d is %d\n",currentElement,space[temp].data);
159     }
160 }
161 void DisplaySpace(StaticList Space){
162     int i;
163     for(i = 0;i < MAXSIZE; i++)
164         printf("%4d",Space[i].cursor);
165     printf("\n");
166     for(i = 0;i < MAXSIZE; i++)
167         printf("%4d",Space[i].data);
168     printf("\n");
169     for(i = 0;i < MAXSIZE; i++)
170         printf("%4d",i);
171     printf("\n");
172 }
173 /*****************************************************/
174 void CreateStaticList(StaticList space,int n_List,int length){
175     if(length > FreeSpaceLength(space)){
176         printf("No remaining Space!\n");
177     }
178     else
179     {
180         int i,new_index;
181         printf("Create Static List %d:\n",n_List);
182         for(i = 1;i <= length;i++){
183             printf("--------enter %d element:  ",i);
184             new_index = Malloc_StaticList(space);
185             scanf("%d",&space[new_index].data);
186             space[n_List].cursor = new_index;
187             n_List = new_index;
188         }
189         space[new_index].cursor = 0;
190     }
191 }
192 int FreeSpaceLength(StaticList space){
193     int length = 0;
194     int index = space[0].cursor;
195     while(index){
196         length++;
197         index = space[index].cursor;
198     }
199     return length;
200 }
201 void setdatato0inordertodisplay(StaticList space){
202     int i;
203     for(i = 0;i < MAXSIZE; i++)
204         space[i].data  =  0;
205 }
206 void main(){
207     StaticList space;
208     setdatato0inordertodisplay(space);
209     InitialSpace(space);
210
211     int list1 = InitialStaticList(space);
212     int list2 = InitialStaticList(space);
213     int list3 = InitialStaticList(space);
214
215     printf("   SHOW SPACE\n");
216     DisplaySpace(space);
217
218     int length3 = StaticList_Length(space,list3);
219     printf("\nthe length of list3 is %d\n",length3);
220     CreateStaticList(space,list3,5);
221     CreateStaticList(space,list2,25);
222     DisplaySpace(space);
223
224     length3 = StaticList_Length(space,list3);
225     printf("the length of list3 is %d\n",length3);
226     int length2 = StaticList_Length(space,list2);
227     printf("the length of list2 is %d\n",length2);
228
229     CreateStaticList(space,list2,3);
230     DisplaySpace(space);
231     length2 = StaticList_Length(space,list2);
232     printf("the length of list2 is %d\n",length2);
233     InsertStaticList(space,list3,4,666);
234
235     DisplaySpace(space);
236     printf("the length of list3 is %d\n",StaticList_Length(space,list3));
237
238     GetStaticListElement(space,list3,3);
239     GetStaticListElement(space,list3,9);
240     GetStaticListElement(space,list1,1);
241     GetStaticListElement(space,list2,1);
242
243     if(LocateStaticList(space,list3,4)){
244         printf("the position of element 4 in List3 is %d\n",LocateStaticList(space,list3,4));
245     }else
246         printf("locateStaticlist in list3 with 4 is Failed!\n");
247
248     if(LocateStaticList(space,list3,2)){
249         printf("the position of element 2 in List3 is %d\n",LocateStaticList(space,list3,4));
250     }else
251         printf("locateStaticlist in list3 with 2 is Failed!\n");
252
253     printf("\n\ntest NextElemet\n");
254     NextElement_SL(space,list3,9);
255     NextElement_SL(space,list3,7);
256     NextElement_SL(space,list3,3);
257
258     printf("\n\ntest PriorElement\n");
259     PriorElement_SL(space,list3,3);
260     PriorElement_SL(space,list3,8);
261     PriorElement_SL(space,list3,1);
262
263     printf("\n");
264     DeleteStaticList(space,list3,5);
265     printf("\nthe length of list3 is %d\n",StaticList_Length(space,list3));
266     DeleteStaticList(space,list2,35);
267     printf("\nthe length of list2 is %d\n",StaticList_Length(space,list2));
268
269     InsertStaticList(space,list3,2,888);
270     DisplaySpace(space);
271
272     ClearStaticList(space,list3);
273     DisplaySpace(space);
274 }
时间: 2024-08-05 19:35:03

静态链表的相关文章

c语言:建立简单的静态链表,它由3个学生数据的结点组成,要求输出各结点的数据

建立简单的静态链表,它由3个学生数据的结点组成,要求输出各结点的数据. 解:将第1个结点的起始地址赋给头指针head,将第2个结点的起始地址赋给第1个结点的next成员,将第3个结点的起始地址赋给第2个结点的next成员.第3个结点的next成员赋予NULL,这就形成了链表.为了建立链表,使head指向a结点,a.next指向b结点,b.next指向c结点,c.next=NULL的作用是使c.next不指向任何有用的存储单元. 程序: #include<stdio.h> struct Stud

静态链表 初始化 定位 Malloc Free 插入 删除

#include <stdio.h> #include <stdlib.h> #define OK 1 #define TRUE 1 #define ERROR -1 #define FALSE -1 #define OVERFLOW -2 #define ElemType int #define Status int typedef int ElemType typedef int Status #define MAX_SIZE 1000;//表最大空间 /* //线性表的基本操

第二章:3.线性表---静态链表的表示和实现

前言: 由于一些高级程序设计语言中,并没有 "指针" 类型,因此上一节中用指针来描述的单链表不能被实现,这时候我们就会使用另一种形式链表:静态链表. 目录: 1.线性表的链式表示和实现 1.1线性链表 单链表(指针型线性链表) 静态链表 1.2循环链表 1.3双向链表 正文: 线性表的静态单链表存储结构: #define MAXSIZE 100; //链表的最大长度 typedef   struct{ ElemType  data; int  cur; }component, SLin

静态链表用C语言实现

静态链表便于在不设指针类型的高级语言使用链表结构,静态链表用数组描述,数组的一个分量表示一个结点,同时用游标(指示器cur)代替指针来表示结点在数组中的相对位置. 另外我们对数组第一个和最后一个元素作为特殊元素处理,不存数据.数组的第一个元素,即下标为0的元素的cur存放备用链表的第一个结点的下标,而数组 最后一个cur则存放第一个有效数值的下标,相当于单链表中有结点的作用. /* 2016年10月11日10:47:23 静态链表 */ #include<stdio.h> #define MA

静态链表实现_详细注释

1 //SLinkList.h 2 3 #ifndef _SLINK_LIST_ 4 #define _SLINK_LIST_ 5 6 template<typename T> 7 class SLinkList 8 { 9 public: 10 SLinkList(int maxz = 100); 11 ~SLinkList(); 12 void CreateList(int n); 13 void Insert(int i, T e); 14 T Delete(int i); 15 T G

数据结构之线性表-链式存储之静态链表(二)

本人文笔较差,语文从来不及格,基础不好,写此类文章仅供自己学习,理解队列及其他知识,高手大神请略过.参考书籍 <数据结构与算法分析-Java语言描述>.<大话数据结构> 1.1 静态链表简介 用数组描述的链表叫静态链表.官方是这么给的定义.另一种描述方法叫游标实现法.先不管这些无聊的定义.先按我个人理解描述,再来个Java版的实现比较符合我的风格.另有一些树上说早期存在其他的高级语言没有对象,引用或者指针这样的东西,但是需要对于快插快删等批量操作的这种链式存储,这时候静态链表就起了

静态链表----数据结构(C语言版) 算法2.17 心得

今天学习了数据结构中的静态链表,刚开始有些语句不能理解,研究了大半天终于想通了.记录于此留待以后查看,同时,对于同样对静态链表有些许不理解的人一点启发.文章中难免有理解上的问题,请指正.下面进入正文. 一.静态链表结构 静态链表可以在不设"指针"类型的高级程序设计语言中使用链表结构.静态链表如下所示:   首先,了解一下静态链表.静态链表中跟单链表类似,包含有很多结点,第i个结点中有包括数据space[i].data,和游标cur,游标space[i].cur的值为下一个结点的数组下标

使用C语言描述静态链表和动态链表

静态链表和动态链表是线性表链式存储结构的两种不同的表示方式. 静态链表的初始长度一般是固定的,在做插入和删除操作时不需要移动元素,仅需修改指针,故仍具有链式存储结构的主要优点. 动态链表是相对于静态链表而言的,一般地,在描述线性表的链式存储结构时如果没有特别说明即默认描述的是动态链表. 下面给出它们的简单实现,关于线性表更为详尽的C语言的实现,可以参考 http://www.cnblogs.com/choon/p/3876606.html 静态链表 #define _CRT_SECURE_NO_

数组元素循环右移及静态链表

1. 静态链表 https://github.com/BodhiXing/Data_Structure/tree/master/StaticListDemo 2. 数组元素循环右移 https://pta.patest.cn/pta/test/17/exam/4/question/262 思路:不做循环,只是换方式打印输出. 1 #include <stdio.h> 2 #include <math.h> 3 4 5 int main() { 6 int n,m,i; 7 scan

能判断是否还有剩余空间的静态链表

第一次系统的学习数据结构是在半年前,看小甲鱼的数据结构与算法视频,自学的话有许多不懂得地方,什么AVL树,红黑树,图的最短路径,最小生成树...但总归对数据结构与算法有一个大体的印象,到现在随着不断写代码,做OJ题,愈发认识到数据结构与算法的重要性,打算再看一遍,现在看着:大话数据结构(程杰著),数据结构(C语言版严蔚敏著),不推荐新手使用 数据结构与算法分析(Mark Allen Weiss 著)这本书真的很难懂. 回归正题,我看了许多书有关静态链表的描述和代码发现都没有判断是否还有剩余空间,