SeqList 簡單的線性表實現
阿新 • • 發佈:2018-06-10
struct delete set del 進行 () int stdlib.h 查找
用數據來實現順序鏈表,將上層邏輯和底層鏈表的實現進行了分離,底層用void指針來滿足所有類型的數據元素,上層需要傳遞對應的元素類型,並分配內存空間,底層不進行內存的管理工作。
// 接口
#ifndef _SEQLIST_H__ #define _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//__SEQLIST_H__
#include <stdlib.h> #include <string.h> #include <stdio.h> #include "seqlist.h" //用數組來模擬線性表 typedef struct _tag_SeqList { int capacity; //存放構造鏈表時候的數據容量。 int length; // 存放鏈表中實際的數量。 int **node; //用來存放每個鏈表的數據地址。 }TSeqList; //鏈表 創建 SeqList* SeqList_Create(intcapacity) //O(1) { int ret; TSeqList *tmp = NULL; tmp = (TSeqList *)malloc(sizeof(TSeqList)); if (tmp == NULL) { ret = 1; printf("func 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("func SeqList_Create() malloc 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) //O(1) { TSeqList *tmp = NULL; if (list == NULL) { return ; } tmp = (TSeqList *)list; if (tmp->node != NULL) { free(tmp->node); } free(tmp); return ; } //鏈表 清空 void SeqList_Clear(SeqList* list) //O(1) { TSeqList *tmp = NULL; if (list == NULL) { return ; } tmp = (TSeqList *)list; tmp->length = 0; memset(tmp->node, 0, (tmp->capacity * sizeof(void *)) ); return ; } //鏈表 長度 int SeqList_Length(SeqList* list) //O(1) { TSeqList *tmp = NULL; if (list == NULL) { return -1; } tmp = (TSeqList *)list; return tmp->length; } //鏈表 容量 int SeqList_Capacity(SeqList* list) //O(1) { TSeqList *tmp = NULL; if (list == NULL) { return -1; } tmp = (TSeqList *)list; return tmp->capacity; } //鏈表 在某一個位置 插入元素 int SeqList_Insert(SeqList* list, SeqListNode* node, int pos) //O(n) { TSeqList *tList = NULL; int i = 0; if (list == NULL || node==NULL) { return -1; } tList = (TSeqList *)list; //如果滿了 if (tList->length >= tList->capacity) { return -2; } //pos位置的容錯處理 if (pos > tList->length ) { pos = tList->length; } for (i=tList->length; i>pos; i--) //n { tList->node[i] = tList->node[i-1]; } tList->node[i] = (int* )node; //ok tList->length ++; return 0; } //獲取某一個位置的鏈表結點 SeqListNode* SeqList_Get(SeqList* list, int pos) //O(1) { TSeqList *tList = NULL; SeqListNode *tmp = NULL; tList = (TSeqList *)list; if (list == NULL || pos < 0 || pos >= tList->length ) { return NULL; } tmp = tList->node[pos]; return tmp; } //刪除某一個位置的結點 SeqListNode* SeqList_Delete(SeqList* list, int pos) ////O(n) { int i = 0; TSeqList *tList = NULL; SeqListNode *tmp = NULL; tList = (TSeqList *)list; if (list == NULL || pos <0 || pos >= tList->length) { return NULL; } tmp = tList->node[pos]; // pos = 3 for (i=pos+1; i<tList->length; i++) { tList->node[i-1] = tList->node[i]; } tList->length --; return tmp; }
優缺點和明顯,查找循序,插入和刪除需要移動所有的元素。
SeqList 簡單的線性表實現