线性表的顺序存储设计和实现 - API函数实现
来源:互联网 发布:python basehttpServer 编辑:程序博客网 时间:2024/05/01 18:33
基本概念
设计与实现
插入元素算法
判断线性表是否合法
判断插入位置是否合法
把最后一个元素到插入位置的元素后移一个位置
将新元素插入
线性表长度加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){intret = 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
1 0
- 线性表的顺序存储设计和实现 - API函数实现
- 线性表顺序存储设计与实现_api函数实现
- 线性表的顺序存储实现和链式存储实现
- 线性表的顺序存储结构的表示和实现
- 顺序存储操作的实现和线性表及其应用
- 线性表的顺序存储结构和实现
- 线性表的顺序存储的实现
- 线性表的顺序存储实现
- 数组实现线性表的顺序存储
- java实现线性表的顺序存储
- 线性表的Java实现--顺序存储
- 线性表的顺序存储实现
- 线性表的动态顺序存储实现
- C++实现顺序存储的线性表
- 线性表的动态顺序存储实现
- 线性表的顺序存储实现
- 线性表顺序存储的实现
- 线性表的顺序存储实现
- 转存
- POJ-1005
- 四种方案解决ScrollView嵌套ListView问题
- 王家林亲授《DT大数据梦工厂》大数据实战视频“Scala深入浅出实战经典”47
- 【数据结构之树状数组】
- 线性表的顺序存储设计和实现 - API函数实现
- 作用域链(Scope Chain)
- delphi 遍历所有文件夹
- 黑马程序员-IOS学习笔记 Xcode注释自动生成
- MySql 如果字段为NULL则返回0
- windows安装java/jdk
- Swift语言 OS X程序点击dock图标重新弹出窗口方法
- 黑马程序员-IOS学习笔记 C语言编程时常犯十八个错误
- servlet