列表順序儲存
阿新 • • 發佈:2018-05-14
destroy pre col hlist color eache fine 打印 ins
sqlist.h
/* *列表順序儲存 */ #ifndef __SQLIST_H_ #define __SQLIST_H_ #include <stdio.h> #include <stdlib.h> #include <string.h> //隱藏真實的類型 typedef void Listing; typedef void ListingNode; //實現一個句柄 儲存信息 Listing* list_init(int n); //列表插入元素 n從0開始 int list_insert(Listing* list, ListingNode* listnode, intn); //銷毀內存 void list_destroy(Listing* list); //清空列表 void list_clear(Listing* list); //獲得某個位置的結點 ListingNode* list_catch(Listing* list,int n); //刪除某個位置結點 ListingNode* list_delete(Listing* list, int n); //返回列表長度 int list_size(Listing* list); //返回列表容量 int list_capacity(Listing* list); #endif // !__SQLIST_H_
sqlist.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "sqlist.h" //定義一個結構體用作句柄 typedef struct SQMLIST { int capacity; int size; int * node; }thList; void* list_init(int n) { thList * tl = NULL; //傳入無效數字 if (n <= 0) { return NULL; }//開辟內存空間 tl = (thList*)malloc(sizeof(thList)+n*sizeof(int *)); if (!tl) { return NULL; } tl->capacity = n; tl->size = 0; //tl為thList類型 步長為thlist tl->node = (int*)(tl+1); return tl; } int list_insert(Listing* list, ListingNode* listnode, int n) { int i = 0; thList * tl = (thList*)list; //指針為空 if (list == NULL || listnode == NULL) { return -1; } //插入的位置不正確 if (n<0 || n >= tl->capacity) { return -2; } //已經滿了 if (tl->size >= tl->capacity) { return -3; } //超過長度則在後面插入 if (n>tl->size) { n = tl->size; } //遍歷後移 for (i = tl->size; i > n; i--) { tl->node[i] = tl->node[i - 1]; } //插入數據 tl->node[n] = (int)listnode; //長度自增 tl->size++; return 0; } void list_destroy(Listing* list) { if (list != NULL) { free(list); } return; } void list_clear(Listing* list) { if (list == NULL) { return; } thList * tl = (thList*)list; //歸零 tl->size = 0; return; } ListingNode* list_catch(Listing* list, int n) { thList * tl = (thList*)list; if (list == NULL || n >= tl->size || n < 0) { return NULL; } return (Listing*)tl->node[n]; } ListingNode* list_delete(Listing* list, int n) { int i = 0; thList * tl = (thList*)list; ListingNode* temp = NULL; if (list == NULL || n >= tl->size || n < 0) { return NULL; } //保存緩存 temp = (ListingNode*)tl->node[n]; //遍歷前移 for ( i = n; i < tl->size; i++) { tl->node[i] = tl->node[i + 1]; } //自減 tl->size--; return temp; } int list_size(Listing* list) { if (list == NULL) { return 0; } thList * tl = (thList*)list; return tl->size; } int list_capacity(Listing* list) { if (list == NULL) { return 0; } thList * tl = (thList*)list; return tl->capacity; }
main.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "sqlist.h" typedef struct __Teacher { int age; char name[100]; }Teacher; void main() { //建立一堆插入的結點 Teacher a, b, c, d, e; memset(&a, 0, sizeof(Teacher)); memset(&b, 0, sizeof(Teacher)); memset(&c, 0, sizeof(Teacher)); memset(&d, 0, sizeof(Teacher)); memset(&e, 0, sizeof(Teacher)); a.age = 1; b.age = 2; c.age = 3; d.age = 4; e.age = 5; memcpy(a.name, "a",1); memcpy(b.name, "b",1); memcpy(c.name, "c",1); memcpy(d.name, "d",1); memcpy(e.name, "e",1); Listing * ls = NULL; //創建列表 ls = list_init(10); //插入數據 list_insert(ls, (ListingNode*)&a, 0); list_insert(ls, (ListingNode*)&b, 0); list_insert(ls, (ListingNode*)&c, 0); list_insert(ls, (ListingNode*)&d, 0); list_insert(ls, (ListingNode*)&e,0); //打印當前長度和容量 printf("%d:%d!\n", list_size(ls), list_capacity(ls)); //獲取元素 printf("%s catch successfully.\n", ((Teacher*)list_catch(ls, 1))->name); //刪除元素 printf("%s delete successfully.\n", ((Teacher*)list_delete(ls, 2))->name); //獲取元素 printf("%s catch successfully.\n", ((Teacher*)list_catch(ls, 2))->name); //清空列表 list_clear(ls); //銷毀列表 list_destroy(ls); system("pause"); }
列表順序儲存