#ifndef _STATICLSIT_H_ #define _STATICLIST_H_ typedef void StaticList; typedef void StaticListNode; /* 该方法用于创建并且返回一个空的线性表 */ StaticList* StaticList_Create(int capacity); /* 该方法用于销毁一个线性表list */ void StaticList_Destroy(StaticList* list); /* 该方法用于将一个线性表list中的所有元素清空 使得线性表回到创建时的初始状态 */ void StaticList_Clear(StaticList* list); /* 该方法用于返回一个线性表list中的所有元素个数 */ int StaticList_Length(StaticList* list); int StaticList_Empty(StaticList* list); int StaticList_Capacity(StaticList* list); /* 该方法用于向一个线性表list的pos位置处插入新元素node 返回值为1表示插入成功,0表示插入失败 */ int StaticList_Insert(StaticList* list, StaticListNode* node, int pos); /* 该方法用于获取一个线性表list的pos位置处的元素 返回值为pos位置处的元素,NULL表示获取失败 */ StaticListNode* StaticList_Get(StaticList* list, int pos); /* 该方法用于删除一个线性表list的pos位置处的元素 返回值为被删除的元素,NULL表示删除失败 */ StaticListNode* StaticList_Delete(StaticList* list, int pos); #endif #include <stdio h=""> #include <malloc h=""> #include "StaticList.h" #define AVAILABLE -1 typedef struct _tag_StaticListNode { unsigned int data; int next; } TStaticListNode; typedef struct _tag_SaticList { int capacity; TStaticListNode header; TStaticListNode node[]; } TStaticList; StaticList* StaticList_Create(int capacity) { TStaticList* ret = NULL; int i = 0; ret = (TStaticList*)malloc(sizeof(TStaticList)+(capacity+1)*sizeof(TStaticListNode)); if (ret != NULL) { ret->capacity = capacity; ret->header.data = 0; ret->header.next = 0; for(i=1;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(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_Empty(StaticList* list) { TStaticList* slist = (TStaticList*)list; int ret = 0; if ((slist != NULL)&&(slist->header.data == 0)) { ret = 1; } 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 = 0; int index = 0; int current = 0; int i = 0; if((slist != NULL)&&(node != NULL)&&(0 <=pos)&&(slist->header.data + 1 <slist->capacity)) { ret = 1; } if(ret) { for(i=1;i<=slist->capacity;i++) { if(slist->node[i].next == AVAILABLE) { index = i; break; } } slist->node[0] = slist->header; slist->node[index].data = (unsigned int)node; 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 i = 0; int current = 0; int object = 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) { TStaticList* slist = (TStaticList*)list; StaticListNode* ret = NULL; int current = 0; int i = 0; int object = 0; if((slist != NULL)&&(0 <=pos)) { slist->node[0] = slist->header; for(i=0;(i<pos slist-="">node[current].next != 0);i++) { current = slist->node[current].next; } object = slist->node[current].next; ret = (StaticListNode*)(slist->node[object].data); slist->node[current].next = slist->node[object].next; slist->node[0].data--; slist->header = slist->node[0]; slist->node[object].next = AVAILABLE; } return ret; } #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); printf("the StaticList capacity is %d\n",StaticList_Capacity(list)); char v1 = 'a'; char v2 = 'b'; char v3 = 'c'; char v4 = 'd'; char v5 = 'e'; char v6 = 'f'; char v7 = 'g'; int i = 0; StaticList_Insert(list,(StaticListNode*)&v1,0); StaticList_Insert(list,(StaticListNode*)&v2,0); StaticList_Insert(list,(StaticListNode*)&v3,0); StaticList_Insert(list,(StaticListNode*)&v4,0); StaticList_Insert(list,(StaticListNode*)&v5,0); StaticList_Insert(list,(StaticListNode*)&v6,0); StaticList_Insert(list,(StaticListNode*)&v7,0); for(i=0;i<StaticList_Length(list);i++) { char* p = (char *)StaticList_Delete(list,StaticList_Length(list)-1); printf("the StaticList length is %d\n",StaticList_Length(list)); printf("the %d elem is %c\n",6-StaticList_Length(list),*p); } printf("\n"); StaticList_Clear(list); printf("the StaticList is Empty(%d)\n",StaticList_Empty(list)); StaticList_Destroy(list); return 0; }