新手求助一个静态链表的问题
头文件#ifndef _STATICLIST_H_
#define _STATICSLIT_H_
typedef void StaticList;
typedef void StaticListNode;
StaticList* StaticList_Create(int capacity);
void StaticList_Destroy(StaticList* list);
void StaticList_Clear(StaticList* list);
int StaticList_Capatcity(StaticList* list);
int StaticList_Length(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
实现文件
#include <stdio.h>
#include <malloc.h>
#include "StaticList.h"
typedef struct _tag_TStaticListNode //定义插入的节点结构体
{
unsigned int data; //存放插入的地址
int next;//记录下一个节点的下标
}TStaticListNode;
typedef struct _tag_TStaticList//定义插入的链表头
{
int capacity;//存放链表的最大长度
int availible;//记录能存放的下标
TStaticListNode header;//表头
TStaticListNode node[];//存放数据的数组
}TStaticList;
StaticList* StaticList_Create(int capacity)
{
TStaticList* ret = (TStaticList*)malloc(sizeof(TStaticList) + sizeof(TStaticListNode) * (capacity + 1) );//动态申请内存空间
int i = 0;
if(ret != NULL)//链表的初始化
{
ret->capacity = capacity;
ret->header.data = 0;
ret->header.next = 0;
for(i = 1;i<capacity ; i++)//将链表初始化从1开始,
{
ret->node[i].next = (i+1);
}
ret->node[i].next = 0;
ret->availible = 1;
}
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(i = 1;i<sList->capacity;i++)
{
sList->node[i].next = (i+1);
}
sList->node[i].next = 0;
sList->availible = 1;
}
}
int StaticList_Capatcity(StaticList* list)
{
int ret = -1;
TStaticList* sList = (TStaticList*)list;
if(sList != NULL)
{
ret = sList->capacity;
}
return ret;
}
int StaticList_Length(StaticList* list)
{
int ret = -1;
TStaticList* sList = (TStaticList*)list;
if(sList != NULL)
{
ret = sList->header.data;
}
return ret;
}
int StaticList_Insert(StaticList* list, StaticListNode* node,int pos)
{
int ret = 0;
TStaticList* sList = (TStaticList*)list;
ret = (sList != NULL);
ret = ret && (0<=pos) && (sList->header.data +1 <= sList->capacity);
int index = 0;
int current = 0;
int i = 0;
if(ret)
{
index = sList->availible;
sList->node[0] = sList->header;
sList->node[index].data = (unsigned int)node;
sList->availible = sList->node[index].next;
for(i = 0;i<pos &&(sList->node[i].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)
{
StaticListNode* ret = NULL;
int current;
int i = 0;
TStaticList* sList = (TStaticList*)list;
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;
}
int index = sList->node[current].next;
ret = (StaticListNode*)(sList->node[index].data);
}
return ret;
}
StaticListNode* StaticList_Delete(StaticList* list,int pos)
{
StaticListNode* ret = NULL;
int current = 0;
int i = 0;
TStaticList* sList = (TStaticList*)list;
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;
}
int index = sList->node[current].next;
ret = (StaticListNode*)(sList->node[index].data);
sList->node[current].next = sList->node[index].next;
sList->node[index].next = sList->availible;
sList->availible = index;
sList->node[0].data--;
sList->header = sList->node[0];
}
return ret;
}
测试文件
#include <stdio.h>
#include <malloc.h>
#include "StaticList2.0.h"
int main(void)
{
StaticList* list = StaticList_Create(5);
int v1 = 0;
int v2 = 1;
int v3 = 2;
int v4 = 3;
int v5 = 4;
int i = 0;
StaticList_Insert(list,&v1,0);
StaticList_Insert(list,&v2,0);
StaticList_Insert(list,&v3,0);
StaticList_Insert(list,&v4,0);
StaticList_Insert(list,&v5,0);
for(i= 0;i<StaticList_Length(list);i++)
{
printf("%d\n",*(int*)StaticList_Get(list,4));
//printf("%d\n",i);
}
return 0;
}
用for循环打印链表会出错,但是把i<StaticList_Length(list)改成i<5就不会错,
for(i= 0;i<StaticList_Length(list);i++)
{
printf("%d\n",i);
}
这样测试了一下也没错,求教各位大神