什么是静态链表
静态链表( static linked list ), 就是用数组来表示链表,用数组元素的下标来模拟链表的指针.
由于是利用数组来定义的链表,属于静态储存分配, 因而叫做静态链表.
结点的定义
静态链表的节点也有由两个域组成: 数据域和游标.因此,静态链表中节点的构成用 C 语言实现为:
typedef struct StaticLinkedNode
{
char data; //数据域
int next; //游标
} *NodePtr;
跟普通链表的区别就是 next 是数组下标是int 类型.
备用链表
用来记录空间存储空间的位置,以便后期分配给新添加元素使用。
typedef struct StaticLinkedList
{
NodePtr nodes;
int* used;
} *ListPtr;
整体代码
#include<stdio.h>
#include<malloc.h>
#define DEFAULT_SIZE 5
typedef struct StaticLinkedNode
{
char data; //数据域
int next; //指针域
} *NodePtr;
typedef struct StaticLinkedList
{
NodePtr nodes;
int* used;
} *ListPtr;
/*
* Initialize the list with a header.
* @return The pointer to the header.
*/
ListPtr initLinkedList()
{
//The pointer to the whole list space.
ListPtr tempPtr = (ListPtr)malloc(sizeof(struct StaticLinkedList));
//Allocate total space.
tempPtr->nodes = (NodePtr)malloc(sizeof(struct StaticLinkedNode) * DEFAULT_SIZE);
tempPtr->used = (int*)malloc(sizeof(int) * DEFAULT_SIZE);
//the first node is the header.
tempPtr->nodes[0].data = '\0';
tempPtr->nodes[0].next = -1;
//Only the first node is used.
tempPtr->used[0] = 1;
for (int i = 1; i < DEFAULT_SIZE; i++)
{
tempPtr->used[i] = 0;
}
return tempPtr;
}
/*
* Print the list.
* @param paraListPtr The pointer to the list.
*/
void printList(ListPtr paraListPtr)
{
int p = 0;
while (p != -1)
{
printf("%c", paraListPtr->nodes[p].data);
p = paraListPtr->nodes[p].next;
}
printf("\r\n");
}
/*
* Insert an element to the given position.
* @param paraListPtr the position of the list.
* @param paraChar The given char.
* @param paraPosition The given position.
*/
void insertElement(ListPtr paraListPtr, char paraChar, int paraPosition)
{
int p, q, i;
//1.Search to the position.
p = 0;
for (i = 0; i < paraPosition; i++)
{
p = paraListPtr->nodes[p].next;
if (p == -1)
{
printf("The position %d is beyond the scope of the list.\r\n", paraPosition);
return;
}
}
//2.Construct a new node.
for (i = 1; i < DEFAULT_SIZE; i++)
{
if (paraListPtr->used[i] == 0)
{
//This is identical to malloc.
printf("Space at %d allocated.\r\n", i);
paraListPtr->used[i] = 1;
q = i;
break;
}
}
if (i == DEFAULT_SIZE)
{
printf("No space.\r\n");
return;
}
paraListPtr->nodes[q].data = paraChar;
//3.Now link.
printf("linking\r\n");
paraListPtr->nodes[q].next = paraListPtr->nodes[p].next;
paraListPtr->nodes[p].next = q;
}
/*
* Delete an element from the list.
* @param paraHeader The header of the list.
* @param paraChar The given char.
*/
void deleteElement(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;
}
if (paraListPtr->nodes[p].next == -1)
{
printf("Cannot delete %c\r\n", paraChar);
return;
}
q = paraListPtr->nodes[p].next;
paraListPtr->nodes[p].next = paraListPtr->nodes[paraListPtr->nodes[p].next].next;
//This statement is identical to free(q)
paraListPtr->used[q] = 0;
}
/*
* Unit Test.
*/
void appendInsertDeleteTest()
{
//1.Initialize an empty list.
ListPtr tempList = initLinkedList();
printList(tempList);
//2.Add some characters.
insertElement(tempList, 'H', 0);
insertElement(tempList, 'e', 1);
insertElement(tempList, 'l', 2);
insertElement(tempList, 'l', 3);
insertElement(tempList, '0', 4);
printList(tempList);
//3.Delete some characters(the first occurrence).
printf("Deleting 'e'.\r\n");
deleteElement(tempList, 'e');
printf("Deleting 'a'.\r\n");
deleteElement(tempList, 'a');
printf("Deleting 'o'.\r\n");
deleteElement(tempList, 'o');
printList(tempList);
insertElement(tempList, 'x', 1);
printList(tempList);
}
/*
* the entrance.
*/
void main()
{
appendInsertDeleteTest();
}
运行结果
使用静态链表存储数据,需要预先申请足够大的一整块内存空间,也就是说,静态链表存储数据元素的个数从其创建的那一刻就已经确定,后期无法更改。
比如,如果创建静态链表时只申请存储 10 个数据元素的空间,那么在使用静态链表时,数据的存储个数就不能超过 10 个,否则程序就会发生错误。不仅如此,静态链表是在固定大小的存储空间内随机存储各个数据元素,这就造成了静态链表中需要使用另一条链表(通常称为"备用链表")来记录空间存储空间的位置,以便后期分配给新添加元素使用。这意味着,如果你选择使用静态链表存储数据,你需要通过操控两条链表,一条是存储数据,另一条是记录空闲空间的位置。