哈哈哈
#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);//创建一个可以容纳5个节点的顺序表 int i = 0;//变量声明和初始化 int j = 1; int k = 2; int x = 3; int y = 4; int z = 5; 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);//将返回的地址赋值给指针时要进行类型强制转换,因为地址是一个uint类型的值,将uint赋值给指针的时候要把它转换为同类型的指针 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; }
#include <stdio.h> #include <malloc.h> #include "SeqList.h" typedef unsigned int TSeqListNode;//节点结构体实际定义 typedef struct _tag_SeqList{ int length; int capacity; 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);//对于顺序表可以直接调用free销毁链表 } void SeqList_Clear(SeqList* list) { TSeqList* sList = (TSeqList*)list; if(sList != NULL){ sList->length = 0;//只考虑长度成员,而节点指针和容量不做修改 } } int SeqList_Insert(SeqList* list,SeqListNode* node,int pos) { int ret = 0; int i = 0; TSeqList* sList = (TSeqList*)list;//几乎只有销毁函数不会定义TSeqList* 变量 ret = (sList != NULL) && (pos >= 0) && (node != NULL) && (sList->length < sList->capacity);//合法性检测,位置,长度与容量的匹配,要插入的位置是否合法,链表是否存在 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;//成功插入返回1,否则返回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;//获取成功则返回实际容量,否则返回-1 } SeqListNode* SeqList_Delete(SeqList* list,int pos) { TSeqList* sList = (TSeqList*)list; SeqListNode* ret = SeqList_Get(list,pos);//根据代码复用的原则,利用Get函数获得将要删除的数据的地址并作为返回值返回 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;//返回的还是地址,是一个uint数据 } SeqListNode* SeqList_Get(SeqList* list,int pos) { SeqListNode* ret = NULL; TSeqList* sList = (TSeqList*)list; if((sList != NULL)&&(pos >= 0)&&(pos < sList->length)){//合法性检测 ret = (SeqListNode*)sList->node[pos];//将指定位置的数据的地址返回 } return ret;//返回的是地址 }
#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_Insert(SeqList* list,SeqListNode* node,int pos); int SeqList_Length(SeqList* list); int SeqList_Capacity(SeqList* list); SeqListNode* SeqList_Delete(SeqList* list,int pos); SeqListNode* SeqList_Get(SeqList* list,int pos); #endif