線性表之鍊錶的實現(二) 靜態鍊錶實現

2021-07-27 12:45:02 字數 3377 閱讀 2756

這一部分的內容主要參照了這篇帖子[靜態鍊錶 c實現]的內容。並且貼上的說明圖也是來自於這篇帖子,再次特做宣告。

什麼是靜態鍊錶

用全域性資料實現的鍊錶叫做靜態鍊錶。由於全域性陣列是儲存在靜態區,又叫做靜態鍊錶。

優缺點

優點:

缺點:

如何實現

乙個陣列邏輯分成兩部分,空閒鍊錶部分和非空閒鍊錶部分。他們都是通過指標來連線的,空閒鍊錶由空閒頭結點來連線起來,非空閒鍊錶由非空閒頭結點連線起來。空閒鍊錶本質上被是做需要進行管理的記憶體,當插入節點時,向管理的記憶體部分申請空間,當刪除節點時,向管理的記憶體部分釋放空間

個人感覺對於靜態鍊錶的實現最有價值的部分,就是模擬對於記憶體的管理,怎麼樣去實現邏輯到物理的對應,這讓我有機會從系統程式設計師的角度寫**!!!

如下圖所示,下標為0的節點是空閒鍊錶的頭結點。

下標為1的節點是非空閒鍊錶的頭結點。由他們兩個非洲維護這兩塊區域。

(ps:上面這張圖並非我原創,來自於本文一開始的鏈結當中)

#ifndef common_h

#define common_h

/* 函式結果狀態碼 */

#define true 1

#define false 0

#define ok 1

#define error 0

#define infeasible -1

//#define overflow -2

/* 型別定義 */

typedef int status; // status是函式的型別,其值是函式結果狀態碼

typedef int elemtype; // elemtype是資料型別

#endif

#ifndef slinkedlist_h

#define slinkedlist_h

#include "common.h"

#define maxsize 1024 //鍊錶的最大長度

/* 靜態鍊錶節點宣告 */

struct slistnode

slistnode( elemtype x ) : data(x), next(-1) {}

};typedef slistnode* slinkedlist;

/* 靜態鍊錶記憶體管理操作 */

status init_sl( slinkedlist slist ); // 初始化為靜態鍊錶空間

int malloc_sl( slinkedlist slist ); // 開闢節點空間

status free_sl( slinkedlist slist, int k ); // **節點空間

/* 靜態鍊錶常用操作 */

status create_slinkedlist( slinkedlist slist, const elemtype* arr, int n ); // 根據陣列建立靜態鍊錶-頭插法

status print_slinkedlist( const slinkedlist slist ); // 列印靜態鍊錶

status insert_slinkedlist( slinkedlist slist, int i, int target ); // 在第i位置插入節點

status delete_slinkedlist( slinkedlist slist, int i); //在第i個位置刪除節點

#endif

status init_sl( slinkedlist slist )

slist[0].next = 2; // slist[0]是空閒表的頭結點

slist[1].next = -1; // slist[1]是非空閒表的頭結點

for(int i = 2; i < maxsize - 1; ++i)

slist[maxsize - 1].next = -1;

return

ok;}

int malloc_sl( slinkedlist slist )

int i = slist[0].next;

if(i != -1)

return i;

}

status free_sl( slinkedlist slist, int k )

slist[k].next = slist[0].next;

slist[0].next = k;

return

ok;}

status create_slinkedlist( slinkedlist slist, const elemtype* arr, int n )

for( int i = 0; i < n; ++i )

slist[s].data = arr[i];

slist[s].next = slist[1].next;

slist[1].next = s;

}return ok;

}status print_slinkedlist( const slinkedlist slist )

int cur = slist[1].next;

while(cur != -1)

return ok;

}status insert_slinkedlist( slinkedlist slist, int i, int target )

int cur = 1; // 頭結點

for( int cnt = 0; cnt < i-1 && cur > -1 ; ++cnt )

if(-1 == cur) return error;

else

}status delete_slinkedlist( slinkedlist slist, int i)

int cur = 1; // 頭結點

for( int cnt = 0; cnt < i - 1; ++cnt)

if(-1==cur) return error;

else

}

線性表(一) 鍊錶之靜態鍊錶

五 靜態鍊錶的實現 定義靜態節點 template class staticlistnode template class staticlist 鍊錶是否為空 bool empty 獲取頭結點 listnode get head 插入節點 void insert const int index,co...

線性表 靜態鍊錶

靜態鍊錶的優缺點 優點 1 在插入和刪除操作時,只需要改變游標cur,不需要移動元素,從而改進了在順序儲存結構中的插入和刪除中需要移動大量元素的缺點 缺點 1 沒有解決連續儲存分配帶來的表長難以確定的問題 2 失去了順序儲存結構隨機訪問的特性 includeusing namespace std d...

線性表 靜態鍊錶

靜態鍊錶,是通過游標來記錄下乙個節點的位置,約定第乙個游標指向備用列表的頭,最後乙個游標指向靜態鍊錶的頭,比如 函式宣告 status static list init static list space status search bp positon static list space stat...