C++ 单链表的基本操作(详解)

2025-05-27 0 20

链表一直是面试的高频题,今天先总结一下单链表的使用,下节再总结双向链表的。本文主要有单链表的创建、插入、删除节点等。

1、概念

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

链表中的数据是以结点来表示的,每个结点的构成:元素 + 指针,元素就是存储数据的存储单元,指针就是连接每个结点的地址数据。如下图:

C++ 单链表的基本操作(详解)

2、链表的基本操作

SingleList.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
#include "stdafx.h"

#include "SingleList.h"

#include <cstdlib>

#include <iostream>

#include <string.h>

#include <conio.h>

#include <stdio.h>

/*c++实现简单的单链表操作*/

using namespace std;

SingleList::SingleList()

{

int num;

char name[128];

// 创建链表

node *stuList = CreatNode();

PrintList(stuList);

// 插入节点

printf("\\n请输入要插入的学生学号和姓名,输入0 0表示结束.");

scanf_s("%d%s", &num, name, 100);

stuList = InsertNode(stuList, num, name);

PrintList(stuList);

// 删除节点

printf("\\n请输入要删除的学生学号:");

scanf_s("%d", &num, 100);

stuList = DeleteNode(stuList, num);

PrintList(stuList);

// 逆序

printf("\\n逆序后的链表为:\\n");

stuList = ReverseList(stuList);

PrintList(stuList);

system("PAUSE");

}

SingleList::~SingleList()

{

}

//建立单链表

node *SingleList::CreatNode()

{

node *head, *p, *s;

int num = 0;

char name[128];

int cycle = 1;

head = (node *)malloc(sizeof(node)); // 为头结点分配内存空间

head->next = nullptr;

p = head; // p指向头节点

while (cycle)

{

printf("\\n请输入学生的学号和姓名:");

scanf_s("%d%s", &num, name, 100);

if (num != 0)

{

s = (node *)malloc(sizeof(node));

s->num = num;

memcpy(s->name, name, 128);

printf("%d%s", s->num, s->name);

p->next = s; // 指向新插入的节点

p = s; // p指向当前节点

}

else

{

cycle = 0;

}

}

head = head->next;

p->next = NULL;

printf("头节点学生信息为: %d%s\\n", head->num, head->name);

return head;

}

//单链表插入

node *SingleList::InsertNode(node *head, int num, char* name)

{

node *s, *p1, *p2 = NULL;

p1 = head;

s = (node *)malloc(sizeof(node));

s->num = num;

strcpy_s(s->name, name);

while ((s->num > p1->num) && p1->next != NULL)

{

p2 = p1;

p1 = p1->next;

}

if (s->num <= p1->num)

{

if (head == p1)

{

// 插入首节点

s->next = p1;

head = s;

}

else

{

// 插入中间节点

p2->next = s;

s->next = p1;

}

}

else

{

// 插入尾节点

p1->next = s;

s->next = NULL;

}

return head;

}

// 计算单链表长度

int SingleList::GetLength(node *head)

{

int length = 0;

node *p;

p = head;

while (p != NULL)

{

p = p->next;

length++;

}

return length;

}

//单链表删除某个元素

node *SingleList::DeleteNode(node *head, int num)

{

node *p1, *p2 = nullptr;

p1 = head;

while (num != p1->num && p1->next != NULL)

{

p2 = p1;

p1 = p1->next;

}

if (num == p1->num)

{

if (p1 == head)

{

head = p1->next;

}

else

{

p2->next = p1->next;

}

free(p1);

}

else

{

printf("找不到学号为%d 的学生!\\n", num);

}

return head;

}

//单链表逆序

node *SingleList::ReverseList(node *head)

{

// A->B->C->D

node *old_head; // 原来链表的头

node *new_head; // 新链表的头

node *cur_head; // 获得原来链表的头

if (head == NULL || head->next == NULL)

return head;

new_head = head; // A

cur_head = head->next; // B

while (cur_head)

{

old_head = cur_head->next; // 将原来链表的头取出,并将第二个节点作为头节点

cur_head->next = new_head; // 将取出的头设为新链表的头

new_head = cur_head; // 新链表的头就是目前新链表的头

cur_head = old_head; // 接着处理

}

head->next = NULL;

head = new_head;

return head;

}

//打印单链表

void SingleList::PrintList(node *head)

{

node *p;

int n;

n = GetLength(head);

printf("\\n打印出 %d 个学生的信息:\\n", n);

p = head;

while (p != NULL)

{

printf("学号: %d ,姓名: %s\\n", p->num, p->name);

p = p->next;

}

}

SingleList.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
#pragma once

typedef struct student

{

int num; // 学号

char name[128]; // 姓名

struct student *next;

}node;

class SingleList

{

public:

SingleList();

~SingleList();

//建立单链表

node *CreatNode();

//单链表插入

node *InsertNode(node *head, int num, char* name);

// 计算单链表长度

int GetLength(node *head);

//单链表删除某个元素

node *DeleteNode(node *head, int num);

//单链表逆序

node *ReverseList(node *head);

//打印单链表

void PrintList(node *head);

};

关于逆序逻辑,研究了一下:

1、主要思路:

假设有单链表A->B->C->D,首先取出首节点A作为新逆序出来的链表

这样,原链表就为:B->C->D,逆序后的新链表为:A

2. 按照上述方法,依次取出B、C、D放入新链表

2、图形表示:

  原始的单链表:

  C++ 单链表的基本操作(详解)
<!–[endif]–>

初始状态时,单链表如上图所示,head指向头节点A。

1. 取出原始链表的第一个节点A,然后将该节点作为新链表的头节点

原始链表:

  C++ 单链表的基本操作(详解)
<!–[endif]–>

  新链表:

<!–[if !vml]–>  C++ 单链表的基本操作(详解)<!–[endif]–>

<!–[if !supportLists]–> 2.然后同上处理:

 原始链表:

<!–[if !vml]–> C++ 单链表的基本操作(详解)<!–[endif]–>

  新链表:

<!–[if !vml]–> C++ 单链表的基本操作(详解)<!–[endif]–>

以上这篇C++ 单链表的基本操作(详解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。

收藏 (0) 打赏

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

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

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

快网idc优惠网 建站教程 C++ 单链表的基本操作(详解) https://www.kuaiidc.com/74686.html

相关文章

发表评论
暂无评论