資料結構之單鏈表 雙鏈表的基本操作

2022-04-25 02:14:47 字數 2140 閱讀 1311

單鏈表雙鏈表中經常糾結的概念:

1、鍊錶是否有含有頭結點:

(1)帶頭結點的煉表頭指標head指向頭結點,資料域不含任何資訊,只是指向鍊錶的第乙個儲存資訊的結點,head->next等於null則表示鍊錶為空;

(2)不帶頭結點的煉表頭指標head直接指向儲存資訊的第乙個結點,head==null,表示鍊錶為空。

說道這裡,有人會對頭指標,頭結點的概念迷糊了。。。

頭指標:指向鍊錶的第乙個結點,不管是不是帶頭結點的;

頭結點:只針對帶頭結點的鍊錶而言,只作為鍊錶存在的標誌,不含資訊。

一般建立鍊錶最好用帶頭結點的。

1 #include 2 #include 3 typedef struct lnode//

單鏈表結點

4lnode;

89 typedef struct dnode//

雙鏈表結點

10dnode;

1415

intmain()16;

36int b[4]=;

37 createlistbytail(list,a,5

);38

//createdlistbyhead(dlist,a,5);

39printlist(list);

40//

printdlist(dlist);

41 createlistbytail(lista,b,4

);42

printlist(lista);

43 printf("

after list&dlist merged:\n");

44mergetwolist(list,lista);

45printlist(list);

46//

searchanddeletelist(list,4);

47//

searchanddeletedlist(dlist,4);

48//

printlist(list);

49//

printdlist(dlist);

50return0;

51}52void createlistbyhead(lnode *c,int a,int

length)

5363}64

void createlistbytail(lnode *c,int a,int

length)

6577}78

79void createdlistbytail(dnode *c,int a,int

length)

8093}94

void createdlistbyhead(dnode *c,int a,int

length)

95106

}107

int searchanddeletedlist(dnode *dlist,int

x)108

117if(p->next==null)

118return0;

119else

120127

}128

int searchanddeletelist(lnode *list,int

x)129

138if(p->next==null)

139return0;

140else

141147

}148

void printlist(lnode *list)

149158 printf("\n"

);159

}160

void printdlist(dnode *dlist)

161170 printf("\n"

);171

}172

173//

合併兩個按順序排列的單鏈表,合成結果是a鍊錶

174void mergetwolist(lnode *a,lnode *b)

175190

else

191196

}197 r->next=null;

198if(p!=null)

199 r->next=p;

200else r->next=q;

201 }

未完待續。。。

資料結構 單鏈表雙鏈表9 18

單鏈表結點類node宣告如下,成員變數data表示結點的資料域,儲存資料元素,資料型別為t,next表示結點的指標域,儲存後繼結點的位址。檔名為node.h template class node public t data node next node this next null node t ...

資料結構 談談單鏈表和雙鏈表

陣列的特點 在記憶體中連續 利用下標定位元素,因此查詢操作的時間複雜度為o 1 增加與刪除元素時,需要進行移動,因此增加與刪除操作的時間複雜度為o n 陣列大小固定,不能直接擴容。如果需要擴容,也是建立乙個更大的陣列,再將元素複製過去。鍊錶的特點 在記憶體中可以不連續 增加與刪除元素的操作很方便,只...

資料結構之陣列 單鏈表和雙鏈表的介紹

線性表是一種線性結構,它是具有相同型別的n n 0 個資料元素組成的有限序列。本章先介紹線性表的幾個基本組成部分 陣列 單向鍊錶 雙向鍊錶 陣列有上界和下界,陣列的元素在上下界內是連續的。陣列的特點是 資料是連續的 隨機訪問速度快。陣列中的難點是多維陣列和動態陣列。多維陣列本質上也是通過一維陣列實現...