静态链表有助于理解系统空间的分配机制,他既有链表的运作逻辑,也长有与顺序表相似的模样,静态链表代码如下:
#include<stdlib.h>
#include<stdio.h>
#include<malloc.h>
#define DEFUALT_SIZE 5
typedef struct StaticLinkedNode {
char data;
int next;
}*NodePtr;
typedef struct StaticLinkedList {
NodePtr nodes;
int* use;
}*ListPtr;
ListPtr initLinkedList()
{
ListPtr tempPtr = (ListPtr)malloc(DEFUALT_SIZE * sizeof(struct StaticLinkedList));
tempPtr->nodes = (NodePtr)malloc(DEFUALT_SIZE*sizeof(struct StaticLinkedNode));
tempPtr->use = (int*)malloc(DEFUALT_SIZE * sizeof(int));
tempPtr->nodes[0].data = '\0';
tempPtr->nodes[0].next = -1;
tempPtr->use[0] = 1;
for (int i = 1; i < DEFUALT_SIZE; i++)
{
tempPtr->use[i] = 0;
}//off for
return tempPtr;
}//off initStaticLinkedList
void printList(ListPtr paraListPtr)
{
int p = 0;
while (p != -1)
{
printf("%c", paraListPtr->nodes[p].data);
p = paraListPtr->nodes[p].next;
}//off while
printf("\r\n");
}//off printfList
void insertElement(ListPtr paraListPtr, char paraChar, int paraPosition)
{
int p, q, i;
p = 0;
for (i = 0; i < paraPosition; i++)
{
p = paraListPtr->nodes[p].next;
if (p == -1)
{
printf("the position %d beyond the scope of the list\r\n", paraPosition);
return;
}//off if
}//off for
for (i = 1; i < DEFUALT_SIZE; i++)
{
if (paraListPtr->use[i] == 0)
{
printf("space at %d allocated\r\n", i);
paraListPtr->use[i] = 1;
q = i;
break;
}//off if
}//off for i
if (i == DEFUALT_SIZE)
{
printf("no space\r\n");
return;
}//off if
paraListPtr->nodes[q].data = paraChar;
printf("linking\r\n");
paraListPtr->nodes[q].next = paraListPtr->nodes[p].next;
paraListPtr->nodes[p].next = q;
}//off insertElement
void deleElement(ListPtr paraListPtr, char paraChar)
{
int p, q;
p = 0;
while (paraListPtr->nodes[p].next != -1 && paraListPtr->nodes[paraListPtr->nodes[p].next].data != paraChar)
{
p = paraListPtr->nodes[p].next;
}//off while
if (paraListPtr->nodes[p].next == -1)
{
printf("Cannot delete the %c\r\n", paraChar);
return;
}//off if
q = paraListPtr->nodes[p].next;
paraListPtr->nodes[p].next = paraListPtr->nodes[paraListPtr->nodes[p].next].next;
paraListPtr->use[q] = 0;
}//off deleElement
void appendinsertdeleteTest()
{
ListPtr tempList = initLinkedList();
printList(tempList);
insertElement(tempList, 'H', 0);
insertElement(tempList, 'e', 1);
insertElement(tempList, 'l', 2);
insertElement(tempList, 'l', 3);
insertElement(tempList, 'o', 4);
printList(tempList);
printf("deeting 'e'\r\n");
deleElement(tempList, 'e');
printf("deeting 'a'\r\n");
deleElement(tempList, 'a');
printf("deeting 'o'\r\n");
deleElement(tempList, 'o');
printList(tempList);
insertElement(tempList, 'x', 1);
printList(tempList);
}//off appendinsertdeleTest
int main()
{
appendinsertdeleteTest();
return 0;
}
下面是运行结果:
1.相似的外形
为什么说静态链表有着与顺序表相似的模样,主要还是因为这两个表的结构体部分和初始化部分。顺序表的结构体与初始化部分如下:
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define Data_Max 10
typedef struct SeqList {
int data[Data_Max];
int actualLength;
}*SeqListPtr;
//创建一个指针函数,返回初始化完成的指针
SeqListPtr SeqListInit(int paraData[],int paraLength)
{
SeqListPtr resultPtr = (SeqListPtr)malloc(sizeof(struct SeqList));//动态空间函数开辟一个大小是sieof(SeqList)的动态空间、
//如果申请空间失败,退出程序
if (resultPtr == NULL)
{
printf("fail in request memory");
exit(-1);
}//off if
int i = 0;
for (i = 0; i < paraLength; i++)
{
resultPtr->data[i] = paraData[i];
}//off for i
resultPtr->actualLength = paraLength;
return resultPtr;
}//off SeqListInit
————————————————
版权声明:本文为CSDN博主「红烧鲈鱼」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_73995622/article/details/129768102
再来看静态链表的结构体与初始化:
#include<stdlib.h>
#include<stdio.h>
#include<malloc.h>
#define DEFUALT_SIZE 5
typedef struct StaticLinkedNode {
char data;
int next;
}*NodePtr;
typedef struct StaticLinkedList {
NodePtr nodes;
int* use;
}*ListPtr;
ListPtr initLinkedList()
{
ListPtr tempPtr = (ListPtr)malloc(DEFUALT_SIZE * sizeof(struct StaticLinkedList));
tempPtr->nodes = (NodePtr)malloc(DEFUALT_SIZE*sizeof(struct StaticLinkedNode));
tempPtr->use = (int*)malloc(DEFUALT_SIZE * sizeof(int));
tempPtr->nodes[0].data = '\0';
tempPtr->nodes[0].next = -1;
tempPtr->use[0] = 1;
for (int i = 1; i < DEFUALT_SIZE; i++)
{
tempPtr->use[i] = 0;
}//off for
return tempPtr;
}//off initStaticLinkedList
不难发现静态链表结构体StaticLinkedList中的NodePtr nodes声明经过动态内存函数申请后变成了可以容纳DEFUALT_SIZE(5)个结构体StaticLinkedList的数组,是不是与顺序表结构体中整形数组data[DATA_MAX]可以存放DATE_MAX(10)个整形起着相似的作用?不过前者申请的空间不一定是连续的,后者开辟的空间是连续的。因为这相似的外形,作者在刚接触静态链表时自然而然地将静态链表当成顺序表,导致后面对静态链表的运行逻辑一头雾水。
2.静态链表的运行逻辑
静态链表的运行分为两个模块,不必问其中一种肯定是链表的将数据储存在数据域和通过指针域访问下一节点的模块;另一种则是记录链表每个节点的空间是否为空并防止越界的模块。
ListPtr initLinkedList()
{
ListPtr tempPtr = (ListPtr)malloc(DEFUALT_SIZE * sizeof(struct StaticLinkedList));
tempPtr->nodes = (NodePtr)malloc(DEFUALT_SIZE*sizeof(struct StaticLinkedNode));
tempPtr->use = (int*)malloc(DEFUALT_SIZE * sizeof(int));
tempPtr->nodes[0].data = '\0';
tempPtr->nodes[0].next = -1;
tempPtr->use[0] = 1;
for (int i = 1; i < DEFUALT_SIZE; i++)
{
tempPtr->use[i] = 0;
}//off for
return tempPtr;
}//off initStaticLinkedList
首先在初始化函数中,我们将nodes[0]作为表头,表头的数据域data赋值'\0',指针域next赋值-1表示链表指针域的NULL,如果要添加数据直到完全占满nodes数组,next则随着nodes数组的下标赋值为0,1,2,3,4,当然表尾要赋值为-1而不是5。表头占了一个nodes的第一个下标的空间,所以这个静态链表能给我们储存数据的空间只有FUALT_SIZE-1即4个空间,同时,use数组第一个下标赋值1,其他下标用for循环赋值0表示从nodes数组下标1到下标4的空间未被使用,可以添加新数据。
insertElement(tempList, 'H', 0);
void insertElement(ListPtr paraListPtr, char paraChar, int paraPosition)
{
int p, q, i;
p = 0;
for (i = 0; i < paraPosition; i++)
{
p = paraListPtr->nodes[p].next;
if (p == -1)
{
printf("the position %d beyond the scope of the list\r\n", paraPosition);
return;
}//off if
}//off for
for (i = 1; i < DEFUALT_SIZE; i++)
{
if (paraListPtr->use[i] == 0)
{
printf("space at %d allocated\r\n", i);
paraListPtr->use[i] = 1;
q = i;
break;
}//off if
}//off for i
if (i == DEFUALT_SIZE)
{
printf("no space\r\n");
return;
}//off if
paraListPtr->nodes[q].data = paraChar;
printf("linking\r\n");
paraListPtr->nodes[q].next = paraListPtr->nodes[p].next;
paraListPtr->nodes[p].next = q;
}//off insertElement
再到插入函数,这里同时也做添加元素函数使用。就拿添加第一个元素'H'来说,在上文我们已经提及nodes数组的第一个下标已经被占用,现在这个数组只剩4个下标供我们使用,那我们不妨把实际数组的下标1当作想象中数组的下标0来使用,更符合数组的下标是从0开始,所以插入函数的下标输入的是0。这里我们在函数开头就用for循环找到实际数组的下标0中存放的next,更通俗易懂的意思是我输入的是下标0,但是实际我们添加数据是添加在下标0的下一个没有存放其他数据的下标中。请注意,nodes数组只能存放5个StaticLinkedList结构体,所以我们要判断是否有剩余空间,于是用for循环检查use数组中哪个下标存放着0,若是use全部存放着1,那就没有空间存放新数据,处理得好就打印没有空间,不进行操作,处理不好编译器直接报错越界。
deleElement(tempList, 'e');
void deleElement(ListPtr paraListPtr, char paraChar)
{
int p, q;
p = 0;
while (paraListPtr->nodes[p].next != -1 && paraListPtr->nodes[paraListPtr->nodes[p].next].data != paraChar)
{
p = paraListPtr->nodes[p].next;
}//off while
if (paraListPtr->nodes[p].next == -1)
{
printf("Cannot delete the %c\r\n", paraChar);
return;
}//off if
q = paraListPtr->nodes[p].next;
paraListPtr->nodes[p].next = paraListPtr->nodes[paraListPtr->nodes[p].next].next;
paraListPtr->use[q] = 0;
}//off deleElement
最后再来到删除函数,我们这里删除元素'e',于是静态链表保存的数据就从Hell变成了Hll,这里的逻辑是让真实的nodes数组中下标1中next赋值的整形2修改成整形3,就如同链表让第一个节点指针域保存的第二个节点的地址变成第三个节点的地址。下面是便于理解的表格。
未删除元素'e':
use下标 | 0 | 1 | 2 | 3 | 4 |
use保存值 | 1 | 1 | 1 | 1 | 1 |
nodes下标 | 0 | 1 | 2 | 3 | 4 |
next保存值 | 1 | 2 | 3 | 4 | NULL |
删除元素'e' :
use下标 | 0 | 1 | 2 | 3 | 4 |
use保存值 | 1 | 1 | 0 | 1 | 1 |
nodes下标 | 0 | 1 | 2 | 3 | 4 |
next保存值 | 1 | 3 | NULL | 4 | NULL |
从这两个表格很容易看出,删除元素'e'后nodes下标2的空间是空的,如果要添加元素或者插入元素就会使用这块空间,而不是在4后面再开辟空间,因为我们当初申请的空间就是这几个,当然如果空间不够我们还可以用realloc函数拓宽原来的空间,不过这样就更加麻烦和不便捷了。