C语言单链表的实现

2025-05-29 0 59

单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。

链表结构:

C语言单链表的实现

SList.h

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29
#pragma once

typedef int DataType;

typedef struct SListNode

{

DataType data;

struct SListNode* next;

}SListNode;

// 如果要修改链表就必须加引用

SListNode* _BuyNode(DataType x); //建立节点

void PrintSlist(SListNode* pHead); //打印单链表

void PushBack(SListNode* & pHead, DataType x); //尾插 (这里用了引用,指明是list的别名,调用时传参,不用传地址)(引用在.c文件中不可用)

//void PushBack(SListNode** pHead, DataType x); // 这里的第一个参数指向链表第一个节点的指针的地址(调用时传参,传的是地址)

void PopBack(SListNode* & pHead); //尾删

void PushFront(SListNode* & pHead, DataType x); //头插

void PopFront(SListNode* & pHead); //头删

void DestoryList(SListNode*& pHead); //清空整个链表

int GetSize(SListNode* pHead); //获取链表长度

SListNode* Find(SListNode* pHead, DataType x); //查找

void Insert(SListNode* pos, DataType x); //某位置后插入数据

void Erase(SListNode*& pHead, SListNode* pos); //删除某位置的数据

void DelNonTailNode(SListNode* pos); //删除一个无头单链表的非尾节点

void InsertFrontNode(SListNode* pos, DataType x); // 在无头单链表的一个非头节点前插入一个节点

SListNode* FindMidNode(SListNode* pHead); //查找中间节点

SListNode* FindKNode(SListNode* pHead, int k); //查找倒数第k个节点(要求只能遍历一次)

void PrintTailToHead(SListNode* pHead); //倒着打印单链表(递归)

//SListNode* Reverse_(SListNode* pHead); //逆置单链表(需要接收返回值),原链表会面目全非

void Reverse(SListNode*& pHead); // 将原链表逆置

SListNode* Merge(SListNode* pHead1, SListNode* pHead2); //合并两个有序链表(合并后依然有序)(递归)

void Sort(SListNode* pHead); //冒泡排序

SList.cpp

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318
#include"SList.h"

#include <stdio.h>

#include<assert.h>

#include <malloc.h>

SListNode* _BuyNode(DataType x) //建立节点

{

SListNode* tmp = (SListNode*)malloc(sizeof(SListNode));

tmp->data = x;

tmp->next = NULL;

return tmp;

}

void PrintSlist(SListNode* pHead) // 打印单链表

{

SListNode* cur = pHead;

while (cur)

{

printf("%d->", cur->data);

cur = cur->next;

}

printf("NULL\\n");

}

//void PushBack(SListNode** ppHead, DataType x) //尾插

//{

// assert(ppHead);

// // 1.空

// // 2.不为空

// if(*ppHead == NULL)

// {

// *ppHead = _BuyNode(x);

// }

// else

// {

// // 找尾

// SListNode* tail = *ppHead;

// while(tail->next != NULL)

// {

// tail = tail->next;

// }

//

// tail->next = _BuyNode(x);

// }

//}

void PushBack(SListNode* & pHead, DataType x) //尾插

{

// 1.空

// 2.不为空

if (pHead == NULL)

{

pHead = _BuyNode(x);

}

else

{

// 找尾

SListNode* tail = pHead;

while (tail->next != NULL)

{

tail = tail->next;

}

tail->next = _BuyNode(x);

}

}

void PopBack(SListNode* & pHead) // 尾删

{

//

// 1.空

// 2.一个节点

// 3.多个节点

//

if (pHead == NULL)

{

return;

}

else if (pHead->next == NULL)

{

free(pHead);

pHead = NULL;

}

else

{

SListNode* tail = pHead;

SListNode* prev = NULL;

while (tail->next)

{

prev = tail;

tail = tail->next;

}

free(tail);

prev->next = NULL;

}

}

void PushFront(SListNode* & pHead, DataType x) //头插

{

// 1.空

// 2.不空

if (pHead == NULL)

{

pHead = _BuyNode(x);

}

else

{

SListNode* tmp = _BuyNode(x);

tmp->next = pHead;

pHead = tmp;

}

}

void PopFront(SListNode*& pHead) //头删

{

//

// 1.空

// 2.一个节点

// 3.一个以上的节点

//

if (pHead == NULL)

{

return;

}

else if (pHead->next == NULL)

{

free(pHead);

pHead = NULL;

}

else

{

SListNode* tmp = pHead;

pHead = pHead->next;

free(tmp);

}

}

void DestoryList(SListNode*& pHead) //清空整个链表

{

SListNode* cur = pHead;

while (cur)

{

SListNode* tmp = cur;

cur = cur->next;

free(tmp);

}

pHead = NULL;

}

int GetSize(SListNode* pHead) //获取链表长度

{

assert(pHead);

SListNode* cur = pHead;

int count = 0;

while (cur)

{

count++;

cur = cur->next;

}

return count;

}

SListNode* Find(SListNode* pHead, DataType x) //查找节点

{

SListNode* cur = pHead;

while (cur)

{

if (cur->data == x)

{

return cur;

}

cur = cur->next;

}

return NULL;

}

void Insert(SListNode* pos, DataType x) // 某位置后插入节点

{

assert(pos);

SListNode* tmp = _BuyNode(x);

tmp->next = pos->next;

pos->next = tmp;

}

void Erase(SListNode*& pHead, SListNode* pos) //删除某位置的节点

{

assert(pos);

assert(pHead);

//pos为头结点

if (pHead == pos)

{

pHead = pHead->next;

free(pos);

return;

}

////

SListNode* prev = pHead;

while (prev)

{

if (prev->next == pos)

{

prev->next = pos->next;

free(pos);

break;

}

prev = prev->next;

}

}

void DelNonTailNode(SListNode* pos) //// 删除一个无头单链表的非尾节点

{

assert(pos);

assert(pos->next);

SListNode* del = pos->next;

SListNode* dnext = del->next;

pos->data = del->data;

pos->next = dnext;

free(del);

}

void InsertFrontNode(SListNode* pos, DataType x) // 在无头单链表的一个非头节点前插入一个节点

{

assert(pos);

SListNode* tmp = _BuyNode(pos->data);

tmp->next = pos->next;

pos->next = tmp;

pos->data = x;

}

void Sort(SListNode* pHead) //冒泡排序

{

assert(pHead);

int size = GetSize(pHead);

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

{

SListNode* left = pHead;

SListNode* right = pHead->next;

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

{

if (left->data>right->data)

{

int tmp = left->data;

left->data = right->data;

right->data = tmp;

}

right = right->next;

left = left->next;

}

}

}

SListNode* FindMidNode(SListNode* pHead) //查找中间节点

{

SListNode* fast = pHead;

SListNode* slow = pHead;

while (fast&&fast->next)

{

slow = slow->next;

fast = fast->next->next;

}

return slow;

}

SListNode* FindKNode(SListNode* pHead, int k) //查找倒数第k个节点

{

SListNode* fast = pHead;

SListNode* slow = pHead;

while (fast && k--)

{

fast = fast->next;

}

if (k > 0)

{

return NULL;

}

while (fast)

{

slow = slow->next;

fast = fast->next;

}

return slow;

}

void PrintTailToHead(SListNode* pHead) //倒着打印单链表(递归)

{

if (pHead)

{

PrintTailToHead(pHead->next);

printf("%d ", pHead->data);

}

}

//SListNode* Reverse_(SListNode* pHead) //逆置单链表(需要接收返回值)原链表会面目全非

//{

// SListNode* cur = pHead;

// SListNode* newHead = NULL;

// while (cur)

// {

// SListNode* tmp = cur;

// cur = cur->next;

// tmp->next = newHead;

// newHead = tmp;

// }

// return newHead;

//}

void Reverse(SListNode*& pHead) //逆置单链表

{

SListNode* cur = pHead;

SListNode* newHead = NULL;

while (cur)

{

SListNode* tmp = cur;

cur = cur->next;

tmp->next = newHead;

newHead = tmp;

}

pHead = newHead;

//return newHead;

}

SListNode* Merge(SListNode* pHead1, SListNode* pHead2) //合并两个有序链表(合并后依然有序)递归

{

if (pHead1 == NULL)

return pHead2;

else if (pHead2 == NULL)

return pHead1;

SListNode* pMergedHead = NULL;

if (pHead1->data < pHead2->data)

{

pMergedHead = pHead1;

pMergedHead->next = Merge(pHead1->next, pHead2);

}

else

{

pMergedHead = pHead2;

pMergedHead->next = Merge(pHead1, pHead2->next);

}

return pMergedHead;

}

Test.cpp

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97
#include "SList.h"

#include<stdlib.h>

void Test1()

{

// 尾插 打印 尾删 头插 头删 清空链表

SListNode* list = NULL;

PushBack(list, 1);

PushBack(list, 2);

PushBack(list, 3);

PushBack(list, 4);

PrintSlist(list);

PopBack(list);

PrintSlist(list);

PushFront(list,0);

PrintSlist(list);

PopFront(list);

PrintSlist(list);

DestoryList(list);

PrintSlist(list);

}

void Test2()

{

// 查找节点 在某位置插入节点 删除某位置节点

SListNode* list = NULL;

PushBack(list, 1);

PushBack(list, 2);

PushBack(list, 3);

PushBack(list, 4);

PrintSlist(list);

SListNode* pos = Find(list, 2);

Insert(pos, 0);

PrintSlist(list);

Erase(list, Find(list, 0));

PrintSlist(list);

}

void Test3()

{

SListNode* list = NULL;

PushBack(list, 1);

PushBack(list, 2);

PushBack(list, 3);

PushBack(list, 4);

PushBack(list, 5);

PushBack(list, 6);

PrintSlist(list);

// 删除一个无头单链表的非尾节点

/*SListNode* pos = Find(list, 2);

DelNonTailNode(pos);

PrintSlist(list);*/

// 在无头单链表的一个非头节点前插入一个节点

/*SListNode* pos = Find(list, 2);

InsertFrontNode(pos, 0);

PrintSlist(list);*/

//查找中间节点

//PrintSlist(FindMidNode(list));

//查找倒数第k个节点

//SListNode* ret = FindKNode(list, 2);

//PrintSlist(ret);

//倒着打印单链表(递归)

//PrintTailToHead(list);

//逆置单链表

//SListNode* ret = Reverse(list);

//PrintSlist(ret);

//PrintSlist(Reverse_(list));

}

void Test4()

{ //合并两个有序链表(合并后依然有序)

SListNode* list = NULL;

PushBack(list, 4);

PushBack(list, 2);

PushBack(list, 1);

PushBack(list, 4);

PrintSlist(list);

Sort(list);

PrintSlist(list);

/*SListNode* list1 = NULL;

PushBack(list1, 2);

PushBack(list1, 3);

PushBack(list1, 3);

PushBack(list1, 0);

PrintSlist(list);

Sort(list1);

PrintSlist(list1);

SListNode* ret = Merge(list, list1);

PrintSlist(ret);

PrintSlist(list);

PrintSlist(list1);*/

}

int main()

{

//Test1();

//Test2();

//Test3();

Test4();

system("pause");

return 0;

}

以上内容是小编给大家介绍的C语言单链表的实现代码,希望对大家有所帮助!

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 C语言单链表的实现 https://www.kuaiidc.com/106216.html

相关文章

发表评论
暂无评论