/********************* *顺序表 *********************/ #ifndef _SeqList_H_ #define _SeqList_H_ typedef void SeqList; typedef void SeqListNode; /* 该方法用于创建并且返回一个空的线性表 */ SeqList* SeqList_Create(int capacity); /* 该方法用于销毁一个线性表list */ void SeqList_Destroy(SeqList* list); /* 该方法用于将一个线性表list中的所有元素清空 使得线性表回到创建时的初始状态 */ void SeqList_Clear(SeqList* list); int SeqList_Empty(SeqList* list); /* 该方法用于返回一个线性表list中的所有元素个数 */ int SeqList_Length(SeqList* list); int SeqList_Capacity(SeqList* list); /* 该方法用于向一个线性表list的pos位置处插入新元素node 返回值为1表示插入成功,0表示插入失败 */ int SeqList_Insert(SeqList* list, SeqListNode* node, int pos); /* 该方法用于获取一个线性表list的pos位置处的元素 返回值为pos位置处的元素,NULL表示获取失败 */ SeqListNode* SeqList_Get(SeqList* list, int pos); /* 该方法用于删除一个线性表list的pos位置处的元素 返回值为被删除的元素,NULL表示删除失败 */ SeqListNode* SeqList_Delete(SeqList* list, int pos); #endif #include <stdio h=""> #include "malloc.h" #include "SeqList.h" typedef unsigned int TSeqListNode; 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)+capacity*sizeof(TSeqListNode)); } 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_Empty(SeqList* list) { TSeqList* slist = (TSeqList*)list; int ret = 0; if((slist != NULL) && (slist->length ==0)) { ret = 1; } return ret; } 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); ret = ret && (slist->length + 1 <= slist->capacity) && (0 <= pos) && (pos <= slist->capacity); int i=0; 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; } SeqListNode* SeqList_Get(SeqList* list, int pos) { TSeqList* slist = (TSeqList*)list; SeqListNode* ret = NULL; if((slist != NULL) && (0 <= pos) && (pos < slist->length)) //pos 必须小于 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; } </slist-></stdio>#include <stdio h=""> #include <stdlib h=""> #include "SeqList.h" /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char *argv[]) { SeqList* list = SeqList_Create(5); char i = 'a'; char j = 'b'; char k = 'c'; char x = 'd'; char y = 'e'; char z = 'f'; 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="" char="" p="(char*)SeqList_Get(list," index="" printf="" c="" n="" p="" printf="" n="" while="" seqlist_length="" list=""> 0 ) { char* p = (char*)SeqList_Delete(list, 0); printf("%c\n", *p); } printf("\nSeqList is Empty(%d)\n",SeqList_Empty(list)); SeqList_Destroy(list); return 0; } </seqlist_length></stdlib></stdio>