單鏈表基本操作

2021-09-07 05:19:30 字數 1119 閱讀 4952

主要是單鏈表的一些常見操作:像建立鍊錶,刪除結點,插入結點,鍊錶逆序,按大小排序等

1 #include 2

using

namespace

std;34

struct

node59

};10

11//

建立帶有頭結點的單鏈表

12void create(node *head)

1327}28

29//

獲得鍊錶長度

30int getsize(node *head)

3139

return

num;40}

41//

輸出鍊錶元素

42void print(node *head)

4350}51

52//

刪除值為x的結點

53void delete(node *head, int

x)54

60if (p->next)

6166

else

6770}71

72//

鍊錶逆序

73void reverse(node *head)

74 84}

8586

//在公升序鍊錶中插入結點使之仍然有序

87void insert(node *head, node *t)

8894 t->next = p->next;

95 p->next =t;96}

97//

按公升序排列

98void sort(node *head)

99110

}111

112void

tips()

113122

intmain()

123154

tips();

155}

156157 }

測試結果:

單鏈表基本操作

include include include include includeusing namespace std typedef struct node node,plinklist plinklist createfromhead node pstnode node malloc sizeof...

單鏈表基本操作

單鏈表的初始化,建立,插入,查詢,刪除。author wang yong date 2010.8.19 include include typedef int elemtype 定義結點型別 typedef struct node node,linkedlist 單鏈表的初始化 linkedlist...

單鏈表基本操作

include using namespace std define namelenth 20 define ok 0 define error 1 typedef struct flagnode node 生成結點 inline node newnode 銷毀化煉表 void destroylin...