單鏈表的構建

2021-09-17 20:35:56 字數 1070 閱讀 4123

鍊錶可以沒有頭結點,但是不能沒有頭指標.

單鏈表中第乙個結點的儲存位置叫做頭指標,最後乙個結點的指標域為空,但是有資料域.整個鍊錶的訪問從頭指標開始.如果鍊錶有頭結點,那麼頭指標就是指向頭結點資料域的指標,如下圖所示:

沒有頭結點的單鏈表如下所示;

**①頭結點是為了操作方便和統一設立的,它的資料域一般無意義(儲存鍊錶一些資訊).

②無論鍊錶是否為空,頭指標都不為空.

#include using namespace std;

struct node

;**//尾插法構建不帶頭結點的單鏈表**

struct node * creat_wf(int n)

q->next = null;

return head;

}**//尾插法構建帶頭結點的單鏈表**

struct node * creat_wt(int n)

p->next = null;

return head;

}**//頭插法構建單鏈表**

struct node * creat_t(int n)

return head;

}**//輸出鍊錶**

void out_l(struct node *head)

}int main()

動態建立鍊錶並且進行測試:

單鏈表的構建 插入 刪除等等

標頭檔案 pragma once typedef int elemtype typedef struct node struct node next 指標域 lnode,linklist typedef struct head hlist,phead lnode head void initlink...

單鏈表(合併單鏈表)

單鏈表遍歷 單鏈表遍歷是從單鏈表頭指標head開始訪問,沿著next指標所指示的方向依次訪問每乙個結點,且每個結點只能訪問依次,直到最後乙個結點為止。遍歷時注意,不要改變head指標的指向。因此一般設定另外的乙個指標變數如p,p從head開始依次訪問乙個結點,直到鍊錶結束,此時p null,完成依次...

Python演算法 構建單鏈表,新增刪除元素

把鍊錶元素和鍊錶索引分別儲存在兩個list裡。listvalue 1,5,6,2,7,3 listright 3,2,4,5,1,1 元素新增時,要先讓新元素指標指向後面的元素,再讓他前面的元素指標指向新元素!不然,先讓前面的元素 a 指向新元素 b 再找新元素後面的元素 c 時,c的索引本來在a裡...