1.静态链表是用数组来实现链式存储结构,实际上就是一个结构体数组。
静态链表的free其实就是令used处为零。
2.静态链表优点:插入和删除时,只需要修改游标,不用移动元素,克服了顺序表中进行插入和删除操作需要移动大量元素的缺点。
静态链表缺点:没有解决连续存储分配带来的表长度难以确定的问题。
失去了顺序存储结构存储随机存取的特性。
3.代码
//静态链表
#include<stdio.h>
#include<malloc.h>
#define DEFAULT_SIZE 5
typedef struct StaticLinkedNode
{
char data;
int next;
} *NodePtr;
typedef struct StacticLinkedList
{
NodePtr nodes;
int* used;
} *ListPtr;
//函数申明
ListPtr init_LinkedList();
void print_List(ListPtr paraListPtr);
void insert_Element(ListPtr paraListPtr, char paraChar, int paraPosition);
void delete_Element(ListPtr paraListPtr, char paraChar);
void append_insert_delete_Test();
/**
*Initialize the list with a header.
*@return the pointer the header.
*/
ListPtr init_LinkedList()
{
//The pointer to the whole list space.
ListPtr tempPtr = (ListPtr)malloc(sizeof(struct StacticLinkedList));
//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;
}//of for i
return tempPtr;
}//of Init_LinkedList
/**
*Print the list.
*@Param paraListPtr the pointer to the list.
*/
void print_List(ListPtr paraListPtr)
{
int p = 0;
while(p != -1)
{
printf("%c", paraListPtr->nodes[p].data);
p = paraListPtr->nodes[p].next;
}//of while
printf("\n");
}//of print_List
/**
*Insert an element the given position.
*@param paraListPtr the position of the list.
*@param paraChar tne given char.
*@param paraPosition the given position.
*/
void insert_Element(ListPtr paraListPtr, char paraChar, int paraPosition)
{
int p, q, i;
//Step1.Search to the position.
p = 0;
for(i = 0;i < paraPosition;i ++)
{
p = paraListPtr->nodes[p].next;
if(p == -1)
{
printf("The positon %d is beyond tne scope of the list\n", paraPosition);
return;
}//of if
}//of for i
//Step2.Constrcut a new node.
for(i = 1;i < DEFAULT_SIZE;i ++)
{
if(paraListPtr->used[i] == 0)
{
//This is identical malloc.
printf("Space at %d allozated.\n", i);
paraListPtr->used[i] = 1;
q = i;
break;
}//of if
}//of for i
if(i == DEFAULT_SIZE)
{
printf("no space.\n");
return;
}//of if
paraListPtr->nodes[q].data = paraChar;
//Step3.Now link.
printf("linking\n") ;
paraListPtr->nodes[q].next = paraListPtr->nodes[p].next;
paraListPtr->nodes[p].next = q;
}//of insert_Element
/**
*Delete an element from the list.
*@param paraHeader the header of the list.
*@paaram paraChar the given char.
*/
void delete_Element(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;
}//of while
if(paraListPtr->nodes[p].next == -1)
{
printf("Cannot delete %c\n ", paraChar);
return;
}//of if
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;
}//of delete_Element
/**
*Unit test.
*/
void append_insert_delete_Test()
{
//Step1.Initialize an empty list.
ListPtr tempList = init_LinkedList();
print_List(tempList);
//Step2.Add some characters.
insert_Element(tempList, 'H', 0);
insert_Element(tempList, 'e', 1);
insert_Element(tempList, 'l', 2);
insert_Element(tempList, 'l', 3);
insert_Element(tempList, 'o', 4);
print_List(tempList);
//Step3.Delete some characters (the first occurence).
printf("Deleting 'e'\n");
delete_Element(tempList, 'e');
printf("Deleting 'a'\n");
delete_Element(tempList, 'a');
printf("Deleting 'o'\n");
delete_Element(tempList, 'o');
print_List(tempList);
insert_Element(tempList, 'x', 1);
print_List(tempList);
}//of append_insert_delete_Test
/**
* The entrance.
*/
int main()
{
append_insert_delete_Test();
return 0;
}// Of main
4.运行结果