基本概念
设计与实现
插入元素算法 判断线性表是否合法 判断插入位置是否合法 把最后一个元素到插入位置的元素后移一个位置 将新元素插入 线性表长度加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
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/zyq522376829/article/details/46837371