静态链表的定义
实质:在顺序表的基础上利用数组实现的单链表
作用:有效解决编程语言没有指针的情况下实现链表
顺序表数组中的元素由两个数据域组成:data和next
data用于存储数据,如下实现中data存放的地址
next用于存储下一个元素在数组中的下标
StaticList.c
#include <stdio.h>
#include <malloc.h>
#include "StaticList.h"
#define AVAILABLE -1 //数组空间是否可用标志 ,正常的是应该为正
//定义链表元素类型为一个结构体类型
//腾出来一个数组元素特殊化,这里采用的是第一个数组元素
//因为必须要有一个指向链表元素的
typedef struct _tag_StaticListNode
{
unsigned int data; //数据项 ,存放的是链表元素成员的地址,指针和unsigend int为32进行了复用
int next; //指向下一个元素的数组下标
} TStaticListNode;
typedef struct _tag_StaticList
{
int capacity; //链表的最大的元素个数
TStaticListNode header; //指向链表的第一个元素
TStaticListNode node[]; //柔性数组
} TStaticList;
StaticList* StaticList_Create(int capacity) // O(n)
{
TStaticList* ret = NULL;
int i = 0;
if( capacity >= 0 )
{
ret = (TStaticList*)malloc(sizeof(TStaticList) + sizeof(TStaticListNode) * (capacity + 1)); //为什么要加1。因为 TStaticList-> node[0],给特殊化了
}
if( ret != NULL )
{
ret->capacity = capacity;
ret->header.data = 0; //对data进行了复用,表示当前的链表长度 ,这样不好因为,要是链表元素的数据成员为多个时怎么处理
ret->header.next = 0;
for(i=1; i<=capacity; i++) //标记
{
ret->node[i].next = AVAILABLE;
}
}
return ret;
}
void StaticList_Destroy(StaticList* list) // O(1)
{
free(list);
}
void StaticList_Clear(StaticList* list) // O(n)
{
TStaticList* sList = (TStaticList*)list;
int i = 0;
if( sList != NULL )
{
sList->header.data = 0;
sList->header.next = 0;
for(i=1; i<=sList->capacity; i++)
{
sList->node[i].next = AVAILABLE;
}
}
}
int StaticList_Length(StaticList* list) // O(1)
{
TStaticList* sList = (TStaticList*)list;
int ret = -1;
if( sList != NULL )
{
ret = sList->header.data;
}
return ret;
}
int StaticList_Capacity(StaticList* list) // O(1)
{
TStaticList* sList = (TStaticList*)list;
int ret = -1;
if( sList != NULL )
{
ret = sList->capacity;
}
return ret;
}
//pos代表链表中对应的位置
int StaticList_Insert(StaticList* list, StaticListNode* node, int pos) // O(n)
{
TStaticList* sList = (TStaticList*)list;
int ret = (sList != NULL);
int current = 0;
int index = 0;
int i = 0;
ret = ret && (sList->header.data + 1 <= sList->capacity); //要有剩余空间
ret = ret && (pos >=0) && (node != NULL);
if( ret )
{
for(i=1; i<=sList->capacity; i++) //寻找一个剩余 空间 ,index为其下标
{
if( sList->node[i].next == AVAILABLE )
{
index = i;
break;
}
}
sList->node[index].data = (unsigned int)node; //data里面放入的是地址信息
sList->node[0] = sList->header;
for(i=0; (i<pos) && (sList->node[current].next != 0); i++)//移动+保护机制
{
current = sList->node[current].next;
}
sList->node[index].next = sList->node[current].next;
sList->node[current].next = index;
sList->node[0].data++;
sList->header = sList->node[0];
}
return ret;
}
//返回的是链表元素数据项的地址
StaticListNode* StaticList_Get(StaticList* list, int pos) // O(n)
{
TStaticList* sList = (TStaticList*)list;
StaticListNode* ret = NULL;
int current = 0;
int object = 0;
int i = 0;
if( (sList != NULL) && (0 <= pos) && (pos < sList->header.data) )
{
sList->node[0] = sList->header;
for(i=0; i<pos; i++)
{
current = sList->node[current].next;
}
object = sList->node[current].next;
ret = (StaticListNode*)(sList->node[object].data);
}
return ret;
}
//删除操作,返回的是要删除的数据的地址
StaticListNode* StaticList_Delete(StaticList* list, int pos) // O(n)
{
TStaticList* sList = (TStaticList*)list;
StaticListNode* ret = NULL;
int current = 0;
int object = 0;
int i = 0;
if( (sList != NULL) && (0 <= pos) && (pos < sList->header.data) )
{
sList->node[0] = sList->header;
for(i=0; i<pos; i++) //找到位置
{
current = sList->node[current].next;
}
object = sList->node[current].next;
sList->node[current].next = sList->node[object].next;
sList->node[0].data--;
sList->header = sList->node[0];
sList->node[object].next = AVAILABLE;
ret = (StaticListNode*)(sList->node[object].data);
}
return ret;
}
StaticList.h
#ifndef _STATICLIST_H_
#define _STATICLIST_H_
typedef void StaticList;
typedef void StaticListNode;
StaticList* StaticList_Create(int capacity);
void StaticList_Destroy(StaticList* list);
void StaticList_Clear(StaticList* list);
int StaticList_Length(StaticList* list);
int StaticList_Capacity(StaticList* list);
int StaticList_Insert(StaticList* list, StaticListNode* node, int pos);
StaticListNode* StaticList_Get(StaticList* list, int pos);
StaticListNode* StaticList_Delete(StaticList* list, int pos);
#endif
main.c
#include <stdio.h>
#include <stdlib.h>
#include "StaticList.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[])
{
StaticList* list = StaticList_Create(10);
int index = 0;
int i = 0;
int j = 1;
int k = 2;
int x = 3;
int y = 4;
int z = 5;
StaticList_Insert(list, &i, 0);
StaticList_Insert(list, &j, 0);
StaticList_Insert(list, &k, 0);
for(index=0; index<StaticList_Length(list); index++)
{
int* p = (int*)StaticList_Get(list, index);
printf("%d\n", *p);
}
printf("\n");
while( StaticList_Length(list) > 0 )
{
int* p = (int*)StaticList_Delete(list, 0);
printf("%d\n", *p);
}
printf("\n");
StaticList_Insert(list, &x, 0);
StaticList_Insert(list, &y, 0);
StaticList_Insert(list, &z, 0);
printf("Capacity: %d Length: %d\n", StaticList_Capacity(list), StaticList_Length(list));
for(index=0; index<StaticList_Length(list); index++)
{
int* p = (int*)StaticList_Get(list, index);
printf("%d\n", *p);
}
StaticList_Destroy(list);
return 0;
}