线性表的顺序存储设计和实现 - API函数实现
基本概念
设计与实现
|
插入元素算法 判断线性表是否合法 判断插入位置是否合法 把最后一个元素到插入位置的元素后移一个位置 将新元素插入 线性表长度加1 |
|
获取元素操作 判断线性表是否合法 判断位置是否合法 直接通过数组下标的方式获取元素 |
|
删除元素算法 判断线性表是否合法 判断删除位置是否合法 将元素取出 将删除位置后的元素分别向前移动一个位置 线性表长度减1 |
代码:
</pre><pre name="code" class="cpp">// seqlist.h #ifndef __MY_SEQLIST_H__ #define __MY_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_Length(SeqList* list); //链表 容量 int SeqList_Capacity(SeqList* list); //链表 在某一个位置 插入元素 int SeqList_Insert(SeqList* list, SeqListNode* node, int pos); //获取某一个位置的链表结点 SeqListNode* SeqList_Get(SeqList* list, int pos); //删除某一个位置的结点 SeqListNode* SeqList_Delete(SeqList* list, int pos); #endif //__MY_SEQLIST_H__
// seqList.cpp
#include <iostream>
#include <cstdio>
#include "seqlist.h"
using namespace std;
typedef struct _tag_SeqList
{
int capacity;
int length;
int **node;
}TSeqList;
//链表 创建
SeqList* SeqList_Create(int capacity)
{
int ret = -1;
TSeqList *tmp = NULL;
tmp = (TSeqList *)malloc(sizeof(TSeqList));
if (tmp == NULL) {
ret = 1;
printf("function SeqList_Create() err:%d\n", ret);
return NULL;
}
memset(tmp, 0, sizeof(TSeqList));
tmp->capacity = capacity;
tmp->length = 0;
tmp->node = (int **)malloc(sizeof(void *) * capacity);
if (tmp->node == NULL) {
ret = 2;
printf("function SeqList_Create() err:%d\n", ret);
return NULL;
}
memset(tmp->node, 0, sizeof(void *) * capacity);
return tmp;
}
//链表 创建
int SeqList_Create2(int capacity, SeqList**handle)
{
int ret = 0;
TSeqList *tmp = NULL;
tmp = (TSeqList *)malloc(sizeof(TSeqList));
if (tmp == NULL)
{
ret = 1;
printf("func SeqList_Create2() err :%d \n", ret);
return ret;
}
memset(tmp, 0, sizeof(TSeqList));
tmp->capacity = capacity;
tmp->length = 0;
tmp->node = (int **)malloc(sizeof(void *) * capacity);
if (tmp->node == NULL)
{
ret = 2;
printf("func SeqList_Create2() malloc err :%d \n", ret);
return ret;
}
*handle = tmp;
return ret;
}
//链表 销毁
void SeqList_Destroy(SeqList* list)
{
if (list == NULL) {
return;
}
TSeqList *tmp = (TSeqList *)list;
if (tmp->node != NULL) {
free(tmp->node);
}
free(tmp);
return;
}
////链表 清空
void SeqList_Clear(SeqList* list)
{
if (list == NULL) {
return;
}
TSeqList *tmp = (TSeqList *)list;
tmp->length = 0;
memset(tmp->node, 0, sizeof(tmp->node));
return;
}
//链表 长度
int SeqList_Length(SeqList* list)
{
if (list == NULL) {
return -1;
}
TSeqList *tmp = (TSeqList *)list;
return tmp->length;
}
//链表 容量
int SeqList_Capacity(SeqList* list)
{
if (list == NULL) {
return -1;
}
TSeqList *tmp = (TSeqList *)list;
return tmp->capacity;
}
//链表 在某一个位置 插入元素
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos)
{
if (list == NULL || node == NULL || pos < 0) {
return -1;
}
TSeqList *tList = (TSeqList *)list;
// 如果满了
if (tList->length >= tList->capacity) {
return -2;
}
// 如果pos的位置超出了length,即中间空了一些位置
if (pos > tList->length) {
pos = tList->length;
}
for (int i = tList->length; i > pos; --i) {
tList->node[i] = tList->node[i - 1];
}
tList->node[pos] = (int *)node;
++tList->length;
return 0;
}
//获取某一个位置的链表结点
SeqListNode* SeqList_Get(SeqList* list, int pos)
{
TSeqList *tList = (TSeqList *)list;
if (list == NULL || pos < 0 || pos >= tList->length)
{
return NULL;
}
SeqListNode *tListNode = NULL;
tListNode = (int *)tList->node[pos];
return tListNode;
}
//删除某一个位置的结点
SeqListNode* SeqList_Delete(SeqList* list, int pos)
{
TSeqList *tList = (TSeqList *)list;
SeqListNode *tListNode = NULL;
if (list == NULL || pos < 0 || pos >= tList->length) {
return NULL;
}
tListNode = tList->node[pos];
for (int i = pos + 1; i < tList->length; ++i) {
tList->node[i - 1] = tList->node[i];
}
--tList->length; // 别忘了长度减一
return tListNode;
}
// main.cpp
#include <iostream>
#include <cstdio>
#include "seqlist.h"
using namespace std;
struct Student
{
char name[32];
int age;
};
int main()
{
Student s1, s2, s3;
s1.age = 21;
s2.age = 22;
s3.age = 23;
int ret = 0;
SeqList *list;
list = SeqList_Create(10);
ret = SeqList_Insert(list, (SeqListNode *)&s1, 0); // 头插法
ret = SeqList_Insert(list, (SeqListNode *)&s2, 0); // 头插法
ret = SeqList_Insert(list, (SeqListNode *)&s3, 0); // 头插法
// 遍历链表
for (int i = 0; i < SeqList_Length(list); ++i) {
Student *tmp = (Student *)SeqList_Get(list, i);
if (tmp == NULL) {
printf("function SeqList_Get() err: %d\n", ret);
return 0;
}
printf("age:%d\n", tmp->age);
}
// 销毁链表
while (SeqList_Length(list)) {
Student *tmp = (Student *)SeqList_Delete(list, 0);
if (tmp == NULL) {
printf("function SeqList_Delete() err: %d\n", ret);
return 0;
}
printf("age:%d\n", tmp->age);
}
SeqList_Destroy(list);
return 0;
}
优点:
无需为线性表中的逻辑关系增加额外的空间
可以快速的获取表中合法位置的元素
缺点:
插入和删除操作需要移动大量元素
当线性表长度变化较大时难以确定存储空间的容量
代码详情:Github
线性表的顺序存储设计和实现 - API函数实现的更多相关文章
- 队列顺序存储 - 设计与实现 - API函数
队列是一种特殊的线性表 队列仅在线性表的两端进行操作 队头(Front):取出数据元素的一端 队尾(Rear):插入数据元素的一端 队列不允许在中间部位进行操作! queue常用操作 销毁队列 清空队 ...
- 队列链式存储 - 设计与实现 - API函数
队列相关基础内容参我的博文:队列顺序存储 - 设计与实现 - API函数 队列也是一种特殊的线性表:可以用线性表链式存储来模拟队列的链式存储. 主要代码: // linkqueue.h // 队列链式 ...
- C 线性表的顺序存储实现及插入、删除等操作示例
一.线性表的定义 线性表(Linear List)是由同一类型元素构成的有序序列的线性结构.线性表中元素的个数称为线性表的长度:线性表内没有元素(长度为0)时,称为空表:表的起始位置称为表头,表的结束 ...
- 2.2_线性表的顺序存储结构_参考集合ArrayList
[线性表的顺序存储从结构] 指的是用一段连续的存储单元一次储存线性表的数据元素. [线性表的顺序存储的结构代码 C语言版] #define MAXSIZE 20 /*存储空间初始分配量*/ typed ...
- 线性表之顺序存储结构(C语言动态数组实现)
线性表的定义:N个数据元素的有限序列 线性表从存储结构上分为:顺序存储结构(数组)和 链式存储结构(链表) 顺序存储结构:是用一段连续的内存空间存储表中的数据 L=(a1,a2,a3....an) 链 ...
- 线性表的顺序存储结构——java
线性表的顺序存储结构:是指用一组地址连续的存储单元一次存放线性表的元素.为了使用顺序结构实现线性表,程序通常会采用数组来保存线性中的元素,是一种随机存储的数据结构,适合随机访问.java中ArrayL ...
- C++编程练习(1)----“实现简单的线性表的顺序存储结构“
线性表的顺序存储结构,指的是用一段地址连续的存储单元依次存储线性表的数据元素. 故可以用数组来实现顺序存储结构. 用C++编写的利用数组实现简单的读取.插入和删除功能的线性表. #include< ...
- 算法与数据结构(一) 线性表的顺序存储与链式存储(Swift版)
温故而知新,在接下来的几篇博客中,将会系统的对数据结构的相关内容进行回顾并总结.数据结构乃编程的基础呢,还是要不时拿出来翻一翻回顾一下.当然数据结构相关博客中我们以Swift语言来实现.因为Swift ...
- 线性表的顺序存储结构之顺序表类的实现_Java
在上一篇博文——线性表接口的实现_Java中,我们实现了线性表的接口,今天让我们来实现线性表的顺序存储结构——顺序表类. 首先让我们来看下顺序表的定义: 线性表的顺序存储是用一组连续的内存单元依次存放 ...
随机推荐
- Android重绘ListView高度
Android重绘ListView高度 经常会有这样需求,需要ListView默认将所有的条目显示出来,这就需要外层使用ScrollView,ScrollView里面放置一个重绘高度的ListView ...
- iOS 应用提交到iTunes Connect,显示"正在处理"后消失不见
打包上传iTunes Connect 成功后,进入iTunes Connect 会看到如下的构建信息: 可是,过一会再刷新该页面,构建的版本就消失了. 出现如上所述的情况,主要目前已知的有两种原因: ...
- 1小时学会JQuery
---------------------------------------------------------------------------------------------------- ...
- Swift中String和NSString的一个不同之处
我们知道在Swift中String和NSString是可以互相转换使用的-额-应该是在绝大数情况下可以互相转换使用.在某些情况下可能还有一丝丝略微的差别:比如在涉及到处理字符串中字符索引的时候. 我们 ...
- Linux 高性能服务器编程——I/O复用的高级应用
高级应用一:非阻塞connect connect系统调用的man手册中有如下的一段内容: EINPROGRESS The socket is non-blocking and the connecti ...
- iOS中 最新微信支付/最全的微信支付教程详解 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博! 亲们, 首先让我们来看一下微信支付的流程吧. 1. 注册微信开放平台,创建应用获取appid,appSecret, ...
- (NO.00004)iOS实现打砖块游戏(七):关卡类的实现
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 关卡游戏的精髓都集中在游戏的关卡里,其中包含了游戏的所有要素,至 ...
- redhat安装vsftpd
一个小问题 rpm -qa|ftp 但是出现3个ftp 只安装了一个 关于网卡ip 首先,我们看到 网卡上面有个x 说明网络是有问题的 我们双击,看到 我们先把connected和connect at ...
- Android官方命令深入分析之etc1tool
etc1tool是一个命令行工具,可以将PNG图像压缩为etc1标准,并且可以进行解压缩. 用法: etc1tool infile [--help | --encode | --encodeNoHea ...
- RabbitMQ消息队列的小伙伴: ProtoBuf(Google Protocol Buffer)
什么是ProtoBuf? 一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化.它很适合做数据存储或 RPC 数据交换格式.可用于通讯协议.数据存储等领域的语言无关.平台无关.可扩 ...