博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据结构-线性表-顺序表
阅读量:6449 次
发布时间:2019-06-23

本文共 3739 字,大约阅读时间需要 12 分钟。

/********************* *顺序表 *********************/ #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>

你可能感兴趣的文章
在Centos中yum安装和卸载软件的使用方法
查看>>
深入DB2性能调优:DB2数据库管理最佳实践
查看>>
张新民-O2O时代的Wi-Fi新应用
查看>>
在视频处理控件TVideoGrabber中如何设置音频捕捉设备
查看>>
android 推送消息
查看>>
开源 java CMS - FreeCMS2.3 评论管理
查看>>
MATLAB调用OpenCV2.4库函数的全过程及问题解决
查看>>
mysql binlog日志恢复数据
查看>>
搭建Spring MVC 入门程序
查看>>
java List 排序 Collections.sort() 对 List 排序
查看>>
前置机
查看>>
Tomcat配置SSL
查看>>
MyEclipse崩溃 mac版删除代码崩溃--亲测可用
查看>>
myeclipse中项目出现红叉
查看>>
打印GC日志
查看>>
Runtime全方位装逼指南
查看>>
Mac下golang开发环境配置
查看>>
Web / JavaScript 全栈工程规范
查看>>
关于LinuxDeepin下装完显卡驱动,重启后,用户正常登陆不了图形界面的问题
查看>>
Apache启用GZIP压缩网页传输方法
查看>>