資料結構之鍊錶

2021-10-20 22:51:34 字數 4217 閱讀 2072

//#pragma once

#ifndef mylinklist_h_

#define mylinklist_h_

#pragma region header

#include #include #pragma endregion

#pragma region some rudimentary definition

using elemtype = int;

constexpr int maxsize = 1000;

#pragma endregion

#pragma region struct of node

struct lnode ;/*單鏈表*/

struct dulnode ;/*雙向鍊錶*/

#pragma endregion

#pragma region function of single linked list

lnode* initlist_l(int n = 0);

int creatlist_l_head(lnode* head, int n);

int creatlist_l_tail(lnode* head, int n);

lnode* getelem_l(elemtype& e, lnode* head, int i);

lnode* locateelem_l(lnode* head, elemtype e, int (*compare)(elemtype, elemtype));

int listinsert_l(lnode* head, int i, elemtype e);

int listdelete_l(lnode* head, int i, elemtype& e);

int clearlist_l(lnode* head);

int destorylist_l(lnode* head);

int listlength_l(lnode* head);

//int mergelist_l(lnode la, lnode lb, lnode& lc);

int union_l(lnode& la, lnode lb);

#pragma endregion

#pragma region function of double linked list

dulnode* initlist_dl(int n);

dulnode* getelem_dl(elemtype& e, dulnode* head, int i);

int listinsert_dl(dulnode* head, int i, elemtype e);

int listdelete_dl(dulnode* head, int i, elemtype& e);

#pragma endregion

#pragma region function of circular linked list

/*操作與單鏈表相似,只需將迴圈條件是否為null改為是否為頭節點*/

lnode* initlist_loop(int n = 0);

int creatlist_loop(lnode* head, int n);

#pragma endregion

#endif // !mylinklist_h_

#include "mylinklist.h"

#pragma region some rudimentary definition

constexpr int ok = 0;

constexpr int error = -1;

constexpr int true = 1;

constexpr int false = 0;

#pragma endregion

#pragma region temp function compare

/* input b */

static int compare_equal(elemtype a, elemtype b)

#pragma endregion

#pragma region function of single linked list

/* 初始化單鏈表 */

lnode* initlist_l(int n)

/* 建立單鏈表 前插法(倒序) */

int creatlist_l_head(lnode* head, int n)

return 0;

}/* 建立單鏈表 後插法(順序) */

int creatlist_l_tail(lnode* head, int n)

return 0;

}/* 第i位置單鏈表讀取元素值和指標 */

lnode* getelem_l(elemtype& e, lnode* head, int i)

if (!p || j > i) return nullptr;

e = p->data;

return p;

}/* 定位元素e位置 */

lnode* locateelem_l(lnode* head, elemtype e, int (*compare)(elemtype, elemtype))

if (!p) return nullptr;

return p;

}/* 第i位置單鏈表插入 */

int listinsert_l(lnode* head, int i, elemtype e)

if (!p || j > i - 1) return error;

lnode* new_l = new lnode;

new_l->data = e;

new_l->next = p->next;

p->next = new_l;

return ok;

}/* 第i位置單鏈表刪除 */

int listdelete_l(lnode* head, int i, elemtype& e)

if (!p || j > i - 1) return error;

lnode* q = p->next;

p->next = q->next;

e = q->data;

delete q;

return ok;

}/* 單鏈表長度 */

int listlength_l(lnode* head)

return length;

}/* 清空單鏈表 */

int clearlist_l(lnode* head)

head->next = nullptr;

return ok;

}/* 摧毀單鏈表 */

int destorylist_l(lnode* head)

/* 合併單鏈表 */

int union_l(lnode* la, lnode* lb)

} return ok;

}#pragma endregion

#pragma region function of double linked list

/* 初始化雙向鍊錶 */

dulnode* initlist_dl(int n)

/* 第i位置雙向鍊錶的元素值和指標 */

dulnode* getelem_dl(elemtype& e, dulnode* head, int i)

if (!p || j > i) return nullptr;

e = p->data;

return p;

}/* 第i位置雙向鍊錶插入 */

int listinsert_dl(dulnode* head, int i, elemtype e)

/* 第i位置雙向鍊錶刪除 */

int listdelete_dl(dulnode* head, int i, elemtype& e)

#pragma endregion

#pragma region function of circular linked list

/* 初始化迴圈鍊錶 */

lnode* initlist_loop(int n)

/* 建立迴圈鍊錶 */

int creatlist_loop(lnode* head, int n)

return ok;

}#pragma endregion

學習筆記,歡迎指正。

資料結構 表之煉表

頭插法建立 尾插法建立 顯示 銷毀 include include using namespace std typedef int elemtype typedef struct lnode linklist void createlinklistf linklist l,elemtype a,in...

資料結構之鍊錶

頭結點 第乙個有效結點之前的那個結點 頭結點並不存有效資料 加頭結點的目的主要是為了方便對鍊錶的操作 頭指標 指向頭結點的指標變數 尾指標 指向尾節點的指標變數 如果希望通過乙個函式對鍊錶進行處理,只需要乙個引數 頭指標 首先要定義乙個單鏈表儲存結構 然後建立乙個空表,即初始化,我寫的這個提前設定好...

資料結構之鍊錶

鍊錶是一種基本的資料結構型別,它由乙個個結點組成。每乙個結點包括乙個資料的儲存和乙個指向下乙個結點的引用。在這個定義中,結點是乙個可能含有任意型別資料的抽象實體,它所包含的指向結點的應用顯示了它在構造鍊錶之中的作用。和遞迴程式一樣,遞迴資料結構的概念一開始也令人費解,但其實它的簡潔性賦予了它巨大的價...