数据结构:线性表的顺序存储结构

网友投稿 937 2022-10-30

数据结构:线性表的顺序存储结构

数据结构:线性表的顺序存储结构

听了国嵌唐老师的课,用C语言实现:

SeqList.h

#ifndef _SEQLIST_H_#define _SEQLIST_H_typedef void SeqList;typedef void SeqListNode;SeqList* SeqList_Create(int capacity);//创建并返回一个空的线性表void SeqList_Destory(SeqList* list);//销毁线性表SeqListvoid SeqList_Clear(SeqList* list);//清空线性表int SeqList_Length(SeqList* list);//返回线性表长度int SeqList_Capacity(SeqList* list);//返回线性表的最大容量 int SeqList_Insert(SeqList* list,SeqListNode* node,int pos);//在pos插入node,返回1表示插入成功SeqListNode* SeqList_Get(SeqList* list,int pos);//获取pos位置元素,返回pos位置元素 SeqListNode* SeqList_Delete(SeqList* list,int pos);//删除pos位置元素,返回值为被删元素 #endif

SeqList.c

#include #include #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) + sizeof(TSeqListNode)*capacity); } if(ret != NULL) { ret->capacity = capacity; ret->length = 0; ret->node = (TSeqListNode*)(ret+1); } return ret;}void SeqList_Destory(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)(unsigned int*)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)) { 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;ilength;i++) { sList->node[i-1] = sList->node[i]; } sList->length--; } return ret;}

main.c

#include #include #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); 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;index0) { int *p = (int*)SeqList_Delete(list,0); printf("\n%d\n",*p); } SeqList_Destory(list); return 0;}

运行效果:

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:一个聊天应用程序能与500米内的人进行实时聊天
下一篇:Springboot从配置文件properties读取字符串乱码的解决
相关文章

 发表评论

暂时没有评论,来抢沙发吧~