一、静态链表的介绍
静态链表就是借助数组来描述线性表的链式存储结构,此时的数组的分量就是我们自己定义的结构体,数组中的一个分量表示一个结点,同时用游标(int
类型的元素)代替指针描述结点在数组中的相对位置,即数组的分量形式是结点+游标。此外,数组的第0个分量可以看成链表中的头结点,其游标即链表中的指针域则就指向链表的第一个结点。
这种存储结构是需要预先分配一个较大的空间,但是在线性表的插入和删除时是不需要移动元素的,仅需要修改游标,因此是具有链式存储结构的主要优点的。
使用静态链表存储数据,数据全部存储在数组中(和顺序表一样),但存储位置是随机的,数据之间"一对一"的逻辑关系通过一个整形变量(称为"游标",和指针功能类似)维持(和链表类似)。
二、代码实现
#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;
}
运行结果:
Space at 1 allocated.
linking
Space at 2 allocated.
linking
Space at 3 allocated.
linking
Space at 4 allocated.
linking
No space.
Hell
Deleting 'e'.
Deleting 'a'.
Cannot delete a
Deleting 'o'.
Cannot delete o
Hll
Space at 2 allocated.
linking
Hxll
Press any key to continue
三、心得体会
学习静态链表的过程中,我对数据结构的理解进一步加深,也对链表这种数据结构的特点和实现方式有了更深刻的认识。
首先,静态链表是在数组的基础上实现的链表。它的实现方式是利用数组中的元素来存储链表中的结点,每个结点除了存储数据还需要额外存储一个指向下一个结点的指针或索引。这样可以避免链表中使用指针带来的额外空间开销和管理负担。
其次,在静态链表中删除元素比较困难,因为需要手动管理指针或索引指向。为了解决这个问题,我学习了很多基于静态链表的优化算法,例如头插法、尾插法等,这些算法可以使插入和删除操作更加高效。
最后,我也认识到静态链表是一种比较受限的数据结构,因为它的容量在初始化时就被固定下来,无法在运行时动态改变。因此,使用静态链表还需要考虑到具体的应用场景和需求。
通过学习静态链表,我不仅掌握了一种特定的数据结构和实现方式,更重要的是锻炼了自己分析问题和设计算法的能力。这种能力可以在将来的学习和工作中发挥重要作用。