说明:
该静态链表实则是SeqList顺序表的改版,因其每一个元素都有一个指向下一个元素的下标编号,通过更改2个下标的数值就可以替代传统数组进行插入操作时需要移动数组每一个元素的行为,其插入数值的操作将变得更高效。(有大量的插入操作时,该数据结构适合使用)
插入的数据,都是将目标数据的地址存入数组下一个空的位置。其顺序是通过TStaticListNode.next下标来确定的;
TStaticList使用了变长结构体的方法,如下:
Struct Packet
{
int state;
int len;
char cData[0]; //这里的0长数组就为变长结构体提供了非常好的支持
};
首先对0长数组做一个解释:
用途 :长度为0的数组的主要用途是为了满足需要变长度的结构体。
用法 :在一个结构体的最后 ,申明一个长度为0的数组,就可以使得这个结构体是可变长的。对于编译器来说,此时长度为0的数组并不占用空间,因为数组名本身不占空间,它只是一个偏移量容量限定,可以存储任何数据类型,无格式要求。且其具备高效率的插入操作;
下面将给出该数据结构的代码,每个函数的结构分析 ,以及个别主要函数的汇编分析
代码:
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 // _STATICLIST_H_
StaticList.c
#include<stdio.h>
#include<malloc.h>
#include "StaticList.h"
#define AVAILABLE -1
typedef struct _tag_StaticListNode
{
unsigned int data;
int next;
} TStaticListNode;
//该结构体的第3个成员是不占内存的,实际占用内存仅有capacity和header;
typedef struct _tag_StaticList
{
int capacity;
TStaticListNode header;
TStaticListNode node[];
} TStaticList;
StaticList* StaticList_Create(int capacity)
{
TStaticList* ret = NULL;
int i = 0;
if (capacity >= 0)
{
ret = (TStaticList*)malloc(sizeof(TStaticList) + sizeof(TStaticListNode)*(capacity + 1));
}
if (ret != NULL)
{
ret->capacity = capacity;
ret->header.data = 0;
ret->header.next = 0;
for (i; i <= capacity; i++)
{
ret->node[i].next = AVAILABLE;
}
}
return ret;
}
void StaticList_Destroy(StaticList* list)
{
free(list);
}
void StaticList_Clear(StaticList* list)
{
TStaticList* sList = (TStaticList*)list;
int i = 0;
if (sList != NULL)
{
sList->header.data = 0;
sList->header.next = 0;
for (int i = 1; i <= sList->capacity; i++)
{
sList->node[i].next = AVAILABLE;
}
}
}
int StaticList_Length(StaticList* list)
{
TStaticList* sList = (TStaticList*)list;
int ret = -1;
if (sList != NULL)
{
ret = sList->header.data;
}
return ret;
}
int StaticList_Capacity(StaticList* list)
{
TStaticList* sList = (TStaticList*)list;
int ret = -1;
if (sList != NULL)
{
ret = sList->capacity;
}
return ret;
}
int StaticList_Insert(StaticList* list, StaticListNode* node, int pos)
{
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++)
{
if (sList->node[i].next == AVAILABLE)
{
index = i;
break;
}
}
sList->node[index].data = (unsigned int)node;
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)
{
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 = (StaticList*)(sList->node[object].data);
}
return ret;
}
StaticListNode* StaticList_Delete(StaticList* list, int pos)
{
TStaticList* sList = (TStaticList*)list;
StaticListNode* ret = NULL;
int current = 0;
int object = 0;
int i = 0;
if ((sList != NULL) && (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;
}
smain.c
#include <stdio.h>
#include <stdlib.h>
#include "StaticList.h"
int main(int argc, char* argv[])
{
StaticList* list = StaticList_Create(10);
int index = 0;
int i = 77;
int j = 88;
int k = 99;
int x = 3;
int y = 4;
int z = 5;
StaticList_Insert(list, &i, 0);
StaticList_Insert(list, &j, 0);
StaticList_Insert(list, &k, 0);
StaticList_Insert(list, &x, 0);
StaticList_Insert(list, &y, 0);
StaticList_Insert(list, &z, 3);
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);
}
return 0;
}
函数结构分析:
1.StaticList_Create
![](http://i.imgur.com/JVN9n5B.png=400x400)
2.StaticList_Destroy
![](http://i.imgur.com/OYczRWM.png=400x400)
3.StaticList_Clear
![](http://i.imgur.com/LYHGfCZ.png=400x400)
4.StaticList_Length
![](http://i.imgur.com/vwLp9gH.png=400x400)
5.StaticList_Capacity
![](http://i.imgur.com/Nzz1QcX.png=400x400)
6.StaticList_Insert
![](http://i.imgur.com/SFQLgnq.png=400x400)
7.StaticList_Get
![](http://i.imgur.com/tHXmEq2.png=400x400)
8.StaticList_Delete
![](http://i.imgur.com/JuXVN1T.png=400x400)
汇编分析:
1.StaticList_Create
![](http://i.imgur.com/Qvehd3Q.png=400x400)
2.StaticList_Insert
![](http://i.imgur.com/Ywd8q3O.png=400x400)
3.StaticList_Delete
![](http://i.imgur.com/7HwH6iU.png=400x400)