# 静态链表的基本原理

由于C语言中有指针操作的概念,其能直接操作计算机内存,
但有些编程语言没有指针的概念,
因此倘若想在没有指针操作下创建一个链表,只能通过对链表的概念进行模拟来实现。
此处通过一维数组来模拟实现一个简单的单链表结构。

# 静态链表的简单创建

_Static_Link.h(头文件)
#pragma once
#include<stdio.h>
#define ElemType char
#define MAX_SPACE 8
struct Static_Link_Node
{
	ElemType data;
	int cur;
};
void Init_Static_Link(struct Static_Link_Node (*SL_Node)[MAX_SPACE]);
void SL_Head_Insert(struct Static_Link_Node(*SL_Node)[MAX_SPACE], const ElemType data);
void Show_Static_Link(struct Static_Link_Node(*SL_Node)[MAX_SPACE]);
void SL_Head_Delete(struct Static_Link_Node(*SL_Node)[MAX_SPACE]);
_Static_Link.c
#include"_Static_Link.h"
void Init_Static_Link(struct Static_Link_Node(*SL_Node)[MAX_SPACE])
{
	int i = 0;
	for (i = 1; i < MAX_SPACE - 1; ++i)
	{
		SL_Node[i]->cur = i + 1;
	}
	SL_Node[0]->cur = -1;
	SL_Node[MAX_SPACE - 1]->cur = 0;
}
static Malloc_SL(struct Static_Link_Node(*SL_Node)[MAX_SPACE])
{
	return !SL_Node[1]->cur ? 0 : SL_Node[1]->cur;
}
void SL_Head_Insert(struct Static_Link_Node(*SL_Node)[MAX_SPACE],const ElemType data)
{
	int index = Malloc_SL(SL_Node);
	if (!index)
	{
		printf("error: Not Enough Space\n");
		return;
	}
	
	SL_Node[1]->cur = SL_Node[index]->cur;
	if (-1 == SL_Node[0]->cur)
	{
		SL_Node[index]->cur = -1;
	}
	else
	{
		if (MAX_SPACE - 1 == index)
			SL_Node[index]->cur = 0;
		SL_Node[index]->cur = index - 1;
	}
	SL_Node[index]->data = data;
	SL_Node[0]->cur = index;
}
static void Free_SL(struct Static_Link_Node(*SL_Node)[MAX_SPACE], int index)
{
	SL_Node[1]->cur = index;
}
void SL_Head_Delete(struct Static_Link_Node(*SL_Node)[MAX_SPACE])
{
	Free_SL(SL_Node, SL_Node[0]->cur);
	if (-1 == SL_Node[0]->cur)
	{
		return;
	}
	if (-1 == SL_Node[SL_Node[0]->cur]->cur)
	{
		SL_Node[0]->cur = - 1;
	}
	else
	{
		SL_Node[0]->cur = SL_Node[1]->cur - 1;
	}
}
_Static_Link_main.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"_Static_Link.h"
#define EndForm '#'
static void Show_Menu()
{
	printf("\n************************************************\n");
	printf(" [1] SL_Head_Insert ******** [2] SL_Head_Delete *\n");
	printf(" [0]  quit_system                               *\n");
	printf("*************************************************\n");
}
static void Show_Static_Link(struct Static_Link_Node(*SL_Node)[MAX_SPACE])
{
	int P_SL = SL_Node[0]->cur;
	while (-1 != P_SL)
	{
		printf("%c", SL_Node[P_SL]->data);
		printf("->");
		P_SL = SL_Node[P_SL]->cur;
	}
	printf("NULL\n");
}
int main()
{
	int option = 0;
	ElemType data = 0;
	struct Static_Link_Node SL_Node[MAX_SPACE];
	Init_Static_Link(&SL_Node);
	Show_Menu();
	while (printf("选择为> "), scanf("%d", &option), printf("\n"), option)
	{
		getchar();
		switch (option)
		{
			case 1:
				printf("输入为('#'终止)> ");
				while (scanf("%c", &data), data != EndForm)
				{
					SL_Head_Insert(&SL_Node, data);
				}
				Show_Static_Link(&SL_Node);
				break;
			case 2:
				SL_Head_Delete(&SL_Node);
				Show_Static_Link(&SL_Node);
				break;
			default:
				printf("选择输入错误\n");
		}
	}
	printf("已退出\n");
	return 0;
}