服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - 剑指offer之C++语言实现链表(两种删除节点方式)

剑指offer之C++语言实现链表(两种删除节点方式)

2021-07-21 15:14chenyu_insist C/C++

今天小编就为大家分享一篇关于剑指offer之C++语言实现链表(两种删除节点方式),小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

1 问题

用C++语言实现链表

2 代码实现

?
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
#include <iostream>
#include <stdlib.h>
using namespace std;
class List
{
public:
 List();
 ~List();
 List* createNode(int value);//创建节点
 bool insertNode(List *node);//插入节点
 void printList();//打印节点
 bool deleteNode(List *node);//删除节点不移动头节点
 bool deleteNode1(List *node);//删除节点移动头节点
 int listSize();//长度
 void printNode();//打印但前的value
 void freeList();//释放链表
private:
 int value;
 List *head;
 List *next;
};
bool List::deleteNode(List *node)
{
 if (node == NULL)
 {
 std:cout << "node is NULL" << std::endl;
 return false;
 }
 if (head == NULL)
 {
 std::cout << "head is NULL" << std::endl;
 return false;
 }
 //如果node等于head
 if (head == node)
 {
 head = head->next;
 }
 List *p = head;
 while (p->next != NULL)
 {
 if (p->next == node)
 {
  p->next = p->next->next;
  return true;
 }
 p = p->next;
 }
 return false;
}
bool List::deleteNode1(List *node)
{
 if (node == NULL)
 {
 std:cout << "node is NULL" << std::endl;
 return false;
 }
 if (head == NULL)
 {
 std::cout << "head is NULL" << std::endl;
 return false;
 }
 //如果node等于head
 if (head == node)
 {
 head = head->next;
 }
 List *p = head;
 while (head->next != NULL)
 {
 if (head->next == node)
 {
  head->next = head->next->next;
  std::cout << "delete node success head->value" << head->value << std::endl;
  //这里要记得把头节点的指针移动最后还原,这里的头节点是保存在这个类里面,改变了就是改变了
  //如果这里是把head作为参数传递,最后head会被销毁那么不需要移动头指针
  head = p;
  return true;
 }
 //注意,这里由于head是成员变量,改变了就是改变了,所以需要最后重新指定
 head = head->next;
 }
 std::cout << "delete node fail head->value" << head->value << std::endl;
 //这里要记得把头节点的指针移动最后还原,这里的头节点是保存在这个类里面,改变了就是改变了
 //如果这里是把head作为参数传递,最后head会被销毁那么不需要移动头指针
 head = p;
 return false;
}
List::List()
{
 value = 0;
 head = NULL;
 next = NULL;
}
List::~List()
{
 delete head;
 delete next;
}
List* List::createNode(int value)
{
 List *list = NULL;
 list = new List();
 if (list)
 {
 list->value = value;
 return list;
 }
 return NULL;
}
bool List::insertNode(List *node)
{
 node->next = head;
 head = node;
 return true;
}
void List::printList()
{ if (head == NULL)
 {
 std::cout << "head is NULL" << std::endl;
 return;
 }
 List *p = head;
 while (p != NULL)
 {
 std::cout << p->value << std::endl;
 p = p->next;
 }
 return;
}
void List::printNode()
{
 std::cout << value << std::endl;
}
int List::listSize()
{
 if (head == NULL)
 {
 std::cout << "head is NULL" << std::endl;
 return 0;
 }
 int len = 0;
 List *p = head;
 while (p != NULL)
 {
 p = p->next;
 ++len;
 }
 return len;
}
void List::freeList()
{
 if (head == NULL)
 {
 std::cout << "head is NULL" << std::endl;
 return;
 }
 List *p;
 while (head != NULL)
 {
 p = head;
 head = head->next;
 free(p);
 }
}
int main()
{
 List list;
 List *list1 = list.createNode(5);
 list.insertNode(list1);
 List *list2 = list.createNode(6);
 list.insertNode(list2);
 List *list3 = list.createNode(1);
 list.insertNode(list3);
 List *list4 = list.createNode(3);
 list.insertNode(list4);
 List *list5 = list.createNode(2);
 list.insertNode(list5);
 list.printList();
 std::cout << "list size is " << list.listSize() << std::endl;
 std::cout << "-----------开始删除节点值为3的节点" << std::endl;
 list.deleteNode1(list4);
 list.printList();
 std::cout << "list size is " << list.listSize() << std::endl;
 list.freeList();
 list.printList();
 return 0;
}

3 运行结果

2
3
1
6
5
list size is 5
-----------开始删除节点值为3的节点
delete node success head->value2
2
1
6
5
list size is 4
head is NULL

4 小结

很明显用C语言实现,我们习惯在外面搞个头结点,然后用C++实现,我们直接在类的里面放一个head指针,然后我们在增加节点的时候我们会把head进行移动,放在最前面,所以后面的 便利和删除操作等最好是不要动head的位置了,因为head动了,下次便利就有问题,如果删除函数移动了head,我们最后需要复原head

比如我们的头指针尽量不要移动,我们可以用一个指针变量来保存这个head指针,然后我们移动保存的指针变量,同时把保存的指针变量在一些情况下改变下一个指向的指针,那么我们下次便利head也是生效的,这样保证了头指针不被污染

比如下面的例子

?
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
#include <stdio.h>
void change(char *a)
{
 *(a + 1) = 's';
}
int main()
{
 char value[10] = "chenyu";
 change(value);
 printf("value is %s\n", value);
 return 0;
}
#include <stdio.h>
void change(char *a)
{
 char *p = a;
 *(p + 1) = 's';
}
int main()
{
 char value[10] = "chenyu";
 change(value);
 printf("value is %s\n", value);
 return 0;
}

其实最后的结果都是一样,csenyu

头指针是指向这块内存的地址,如果我们用指针变量保存了,然后这个指针变量也指向了这里,用指针变量去操作后,然后头指针也是指向这里,后面数据的指向也会改变

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对服务器之家的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/u011068702/article/details/86516967

延伸 · 阅读

精彩推荐
  • C/C++C语言实现双人五子棋游戏

    C语言实现双人五子棋游戏

    这篇文章主要为大家详细介绍了C语言实现双人五子棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    两片空白7312021-11-12
  • C/C++C语言main函数的三种形式实例详解

    C语言main函数的三种形式实例详解

    这篇文章主要介绍了 C语言main函数的三种形式实例详解的相关资料,需要的朋友可以参考下...

    ieearth6912021-05-16
  • C/C++c/c++实现获取域名的IP地址

    c/c++实现获取域名的IP地址

    本文给大家汇总介绍了使用c/c++实现获取域名的IP地址的几种方法以及这些方法的核心函数gethostbyname的详细用法,非常的实用,有需要的小伙伴可以参考下...

    C++教程网10262021-03-16
  • C/C++深入C++拷贝构造函数的总结详解

    深入C++拷贝构造函数的总结详解

    本篇文章是对C++中拷贝构造函数进行了总结与介绍。需要的朋友参考下...

    C++教程网5182020-11-30
  • C/C++c/c++内存分配大小实例讲解

    c/c++内存分配大小实例讲解

    在本篇文章里小编给大家整理了一篇关于c/c++内存分配大小实例讲解内容,有需要的朋友们可以跟着学习参考下。...

    jihite5172022-02-22
  • C/C++使用C++制作简单的web服务器(续)

    使用C++制作简单的web服务器(续)

    本文承接上文《使用C++制作简单的web服务器》,把web服务器做的功能稍微强大些,主要增加的功能是从文件中读取网页并返回给客户端,而不是把网页代码...

    C++教程网5492021-02-22
  • C/C++关于C语言中E-R图的详解

    关于C语言中E-R图的详解

    今天小编就为大家分享一篇关于关于C语言中E-R图的详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看...

    Struggler095962021-07-12
  • C/C++OpenCV实现拼接图像的简单方法

    OpenCV实现拼接图像的简单方法

    这篇文章主要为大家详细介绍了OpenCV实现拼接图像的简单方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    iteye_183805102021-07-29