说明:
- 该顺序表是在堆中开辟出一个段连续的空间,用以存储数据;其中包括一个表头(容量+长度+第0个数据的地址),和一段存储元素地址值的内存。
- 存储进顺序表的元素,需要在另外的内存创建(堆或栈),并将该变量的地址放入该容器内,以实现统一管理。
- 传入函数的参数SeqListNode*,其实是void*类型,以便能够存储任何类型的数据。例如传入int*,则保存其地址值,当获取元素时,再转换成int*类型即可。
- 在TSeqList的堆内存中,后面跟着的连续数组元素其实是地址,存储每一个元素的地址。
- 该数组结构的容量是固定的,但可以存储任意类型的数据。
下面将给出该数据结构的代码,每个函数的结构分析 ,以及个别主要函数的汇编分析
代码:
SeqList.h
#ifndef _SEQLIST_H_
#define _SEQLIST_H_
typedef void SeqList;
typedef void SeqListNode;
SeqList* SeqList_Create(int capacity);
void SeqList_Destroy(SeqList* list);
void SeqList_Clear(SeqList* list);
int SeqList_Length(SeqList* list);
int SeqList_Capacity(SeqList* list);
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos);
SeqListNode* SeqList_Get(SeqList* list, int pos);
SeqListNode* SeqList_Delete(SeqList* list, int pos);
#endif
SeqList.c
#include <stdio.h>
#include <malloc.h>
#include "SeqList.h"
//注意:这个是用来存储顺序元素的地址
typedef unsigned int TSeqListNode;
//第三个指针成员将指向结构体后面延长的数据顺序表的第一个元素,即node[0]
typedef struct _tag_SeqList
{
int capacity;
int length;
TSeqListNode* node;
} TSeqList;
//创建顺序表(需要传入参数确定容量)
SeqList* SeqList_Create(int capacity)
{
TSeqList* ret = NULL;
if( capacity >= 0 )
{
ret = (TSeqList*)malloc(sizeof(TSeqList) + sizeof(TSeqListNode) * capacity);
}
if( ret != NULL )
{
ret->capacity = capacity;
ret->length = 0;
ret->node = (TSeqListNode*)(ret + 1);
}
return ret;
}
//销毁顺序表
void SeqList_Destroy(SeqList* list)
{
free(list);
}
//清空顺序表
void SeqList_Clear(SeqList* list)
{
TSeqList* sList = (TSeqList*)list;
if( sList != NULL )
{
sList->length = 0;
}
}
//获取当前元素个数
int SeqList_Length(SeqList* list)
{
TSeqList* sList = (TSeqList*)list;
int ret = -1;
if( sList != NULL )
{
ret = sList->length;
}
return ret;
}
//获取容量
int SeqList_Capacity(SeqList* list)
{
TSeqList* sList = (TSeqList*)list;
int ret = -1;
if( sList != NULL )
{
ret = sList->capacity;
}
return ret;
}
//插入元素(这里存储的是要插入的数据地址,因此可以存储任何类型数据)
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos)
{
TSeqList* sList = (TSeqList*)list;
int ret = (sList != NULL);
int i = 0;
ret = ret && (sList->length + 1 <= sList->capacity);
ret = ret && (0 <= pos);
if( ret )
{
if( pos >= sList->length )
{
pos = sList->length;
}
for(i=sList->length; i>pos; i--)
{
sList->node[i] = sList->node[i-1];
}
sList->node[i] = (TSeqListNode)node;
sList->length++;
}
return ret;
}
//获取第pos个元素,获得元素后需要类型转换,其实返回的只是void*类型的地址值
SeqListNode* SeqList_Get(SeqList* list, int pos)
{
TSeqList* sList = (TSeqList*)list;
SeqListNode* ret = NULL;
if( (sList != NULL) && (0 <= pos) && (pos < sList->length) )
{
ret = (SeqListNode*)(sList->node[pos]);
}
return ret;
}
//删除指定的元素
SeqListNode* SeqList_Delete(SeqList* list, int pos)
{
TSeqList* sList = (TSeqList*)list;
SeqListNode* ret = SeqList_Get(list, pos);
int i = 0;
if( ret != NULL )
{
for(i=pos+1; i<sList->length; i++)
{
sList->node[i-1] = sList->node[i];
}
sList->length--;
}
return ret;
}
smain.c
#include <stdio.h>
#include <stdlib.h>
#include "SeqList.h"
int main(int argc, char *argv[])
{
SeqList* list = SeqList_Create(5);
int i = 10;
int j = 11;
int k = 12;
int x = 13;
int y = 14;
int z = 15;
int index = 0;
SeqList_Insert(list, &i, 0);
SeqList_Insert(list, &j, 0);
SeqList_Insert(list, &k, 0);
SeqList_Insert(list, &x, 0);
SeqList_Insert(list, &y, 0);
SeqList_Insert(list, &z, 0);
for(index=0; index<SeqList_Length(list); index++)
{
int* p = (int*)SeqList_Get(list, index);
printf("%d\n", *p);
}
printf("\n");
while( SeqList_Length(list) > 0 )
{
int* p = (int*)SeqList_Delete(list, 0);
printf("%d\n", *p);
}
SeqList_Destroy(list);
return 0;
}
函数分析:
1.SeqList_Create
2.SeqList_Destroy
3.SeqList_Clear
4.SeqList_Length
5.SeqList_Capacity
6.SeqList_Insert
7.SeqList_Get
8.SeqList_Delete
汇编分析:
1.SeqList_Create
2.SeqList_Insert
3.SeqList_Delete