//List_SL_Head.h #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 #include#include #include #include typedef int Status; typedef int ElemType; #define MAXSIZE 1000 typedef struct { ElemType data; int cur; }component, SLinkList[MAXSIZE]; //Status CreateList_SL(SLinkList, int); void ListDisp(SLinkList, int); //打印静态链表(调试用) int Malloc_SL(SLinkList); //获取一个未使用的节点 void Free_SL(SLinkList, int); //释放一个节点 void InitSpace_SL(SLinkList); //对静态链表初始化
//List_SL.c
#include "List_SL_Head.h"
//Yan W-M: Use (SlinkList) space to record the nodes that are didn't used.
//For example: space[0].cur points to the first node didn't used.
//S = Malloc_SL(space); to get a node to use.
//S points to the head of list, R points to the rear of list.
//In a word: starts from space[0] to gets the nodes didn't used.
//to get the nodes that have recorded messages (List), have to start from space[S];
void ListDisp(SLinkList sl, int s)
// Display the static list.
{
int i = sl[s].cur;
while (i)
{
printf("%d ", sl[i].data);
i = sl[i].cur;
}
printf("n");
}
void InitSpace_SL(SLinkList sl)
{
for (int i = 0; i < MAXSIZE - 1; ++i)
{
sl[i].cur = i + 1;
}
sl[MAXSIZE-1].cur = 0;
}
int Malloc_SL(SLinkList sl)
// If still have memory, get a new node and return the index of new node.
{
int i= sl[0].cur;
if (i) sl[0].cur = sl[i].cur;
return i;
}
void Free_SL(SLinkList sl, int k)
{
int i = sl[0].cur;
sl[0].cur = k;
sl[k].cur = i;
}
静态链表创建方法:
(1) 建立一个固定大小的结构体数组,对其初始化。
即:space[0].cur = 1;space[1].cur = 2;...; space[MaxSize - 1].cur = 0;
(2) 默认从space[0]开始遍历时,获取的是未使用的节点。
(3) 对于已使用的节点(即链表),需要用整数 S 记录其下标,从S开始遍历即可遍历链表。
核心:静态链表实现;Malloc_SL() 和 Free_SL()实现;静态链表思想。



