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

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

服务器之家 - 编程语言 - C/C++ - C++ 双链表的基本操作(详解)

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

2021-04-22 15:00C++教程网 C/C++

下面小编就为大家带来一篇C++ 双链表的基本操作(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

1.概念

双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。一般我们都构造双向循环链表。

结构图如下所示:

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

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

2.基本操作实例

DoubleList.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
#include "stdafx.h"
#include "DoubleList.h"
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
DoubleList::DoubleList()
{
    pDoubleListNode pDouList = NULL;
    // 创建双链表
    CreateDouList(pDouList);
    PrintDouList(pDouList);
    // 打印逆序链表
    PrintDouReverseList(pDouList);
    // 节点后插入节点
    InsertNodeAfter(pDouList);
    PrintDouList(pDouList);
    // 节点前插入节点
    InsertNodeBefore(pDouList);
    PrintDouList(pDouList);
    // 删除节点
    DeleteNode(pDouList);
    PrintDouList(pDouList);
    // 删除链表
    DeleteDouList(pDouList);
    PrintDouList(pDouList);
    system("PAUSE");
}
DoubleList::~DoubleList()
{
}
//创建双向链表
void DoubleList::CreateDouList(pDoubleListNode &head)
{
  char x;     // 定义成char型是用于输入'q'时可以退出,其实定义成int也能退出
  pDoubleListNode p, s;
  head = (pDoubleListNode)malloc(sizeof(DoubleListNode));
  head->next = NULL;
  head->prior = NULL;    // 构造头结点p
  p = head;
  printf("\n输入双向链表的元素,每输入一个元素后按回车,输入q表示结束.\n");
  fflush(stdin);  //清空输入缓冲区
  x = getchar();
  while (x != 'q')
  {
    s = (pDoubleListNode)malloc(sizeof(DoubleListNode));
    s->data = x - '0'; // 得到的是输入字符的ASCII码,减去30H就变成想要的数字
    s->next = NULL;
    s->prior = p;
    p->next = s;
    p = s;
    fflush(stdin);
    x = getchar();
  }
  if (x == 'q')
  {
    printf("双向链表构造完毕!\n");
  }
}
//打印双向链表
void DoubleList::PrintDouList(pDoubleListNode &head)
{
  pDoubleListNode p;
  printf("\n打印出双向链表数据为:\n");
  if (!IsDouListEmpty(head))
  {
    p = head->next;
    while (p)
    {
      printf("%d\n", p->data);
      p = p->next;
    }
  }
}
//逆序打印双向链表
void DoubleList::PrintDouReverseList(pDoubleListNode &head)
{
  pDoubleListNode p;
  printf("\n打印出逆序双向链表数据为:\n");
  if (!IsDouListEmpty(head))
  {
    p = head->next;
    while (p->next)
    {
      p = p->next;
    }
    while (p->prior)
    {
      printf("%d \n", p->data);
      p = p->prior;
    }
  }
}
//求链表长度
int DoubleList::GetDouListLength(pDoubleListNode &head)
{
  int length = 0;
  if (head == NULL)
  {
    printf("链表不存在,请先初始化!\n");
  }
  else
  {
    pDoubleListNode p = head->next;
    while (p)
    {
      length++;
      p = p->next;
    }
  }
  return length;
}
//判断链表是否为空
bool DoubleList::IsDouListEmpty(pDoubleListNode &head)
{
  if (head == NULL)
  {
    printf("链表不存在,请先初始化!\n");
    return true;
  }
  else if (head->next == NULL)
  {
    printf("链表为空!\n");
    return true;
  }
  
  return false;
}
//把双向链表置空
void DoubleList::ClearDouList(pDoubleListNode &head)
{
  if (head == NULL)
  {
    printf("链表不存在,请先初始化!\n");
  }
  else
  {
    pDoubleListNode p, q;
    p = q = head->next;  //是p、q指向第一个元素
    head->next = NULL;
    while (p)     //逐个释放元素所占内存
    {
      p = p->next;
      free(q);
      q = p;
    }
  }
}
// 删除双向链表
void DoubleList::DeleteDouList(pDoubleListNode &head)
{
  printf("\n删除双向链表\n");
  ClearDouList(head);
  free(head);
  head = NULL;
}
// 在双向链表中第i个位置后面插入元素
void DoubleList::InsertNodeAfter(pDoubleListNode &head)
{
  int data, pos;
  pDoubleListNode p, s;
  p = head;
  int i = 0;
  printf("\n在双向链表中第i个位置后面插入元素\n");
  printf("请输入要插入的元素和位置:\n");
  scanf_s("%d%d", &data, &pos, 100);
  if (head == NULL)
  {
    printf("链表不存在,请先初始化!\n");
  }
  else if (head->next == NULL)
  {
    printf("链表为空,插入第一个元素!\n");
    s = (pDoubleListNode)malloc(sizeof(DoubleListNode));
    s->data = data;
    s->prior = NULL; 
    s->next = NULL;
    head->next = s;    // 将新结点插入head后
  }
  else if (pos<1 || pos>GetDouListLength(head) + 1)
  {
    printf("插入位置错误!\n");
  }
  else
  {
    while (i < pos)
    {
      p = p->next;
      i++;
    }
    if (i == GetDouListLength(head))   //如果在最后一个元素后面插入data
    {
      s = (pDoubleListNode)malloc(sizeof(DoubleListNode));
      s->data = data;
      s->next = NULL;
      s->prior = p;
      p->next = s;
    }
    else
    {
      s = (pDoubleListNode)malloc(sizeof(DoubleListNode));
      s->data = data;
      s->next = p->next;
      p->next->prior = s;
      p->next = s;
      s->prior = p;
    }
  }
}
// 在双向链表中第i个位置前面插入元素
void DoubleList::InsertNodeBefore(pDoubleListNode &head)
{
  int data, pos;
  pDoubleListNode p, s;
  p = head;
  int i = 0;
  printf("\n在双向链表中第i个位置前面插入元素\n");
  printf("请输入要插入的元素和位置:\n");
  scanf_s("%d%d", &data, &pos, 100);
  if (head == NULL)
  {
    printf("链表不存在,请先初始化!\n");
  }
  else if (head->next == NULL)
  {
    printf("链表为空,插入第一个元素!\n");
    s = (pDoubleListNode)malloc(sizeof(DoubleListNode));
    s->data = data;
    s->prior = NULL;
    s->next = NULL;
    head->next = s;    // 将新结点插入head后
  }
  else if (pos<1 || pos>GetDouListLength(head) + 1)
  {
    printf("插入位置错误!\n");
  }
  else
  {
    while (i < pos)
    {
      p = p->next;
      i++;
    }
    if (i == 1)   // 如果在第一个元素前面插入data
    {
      s = (pDoubleListNode)malloc(sizeof(DoubleListNode));
      s->data = data;
      head->next = s;    // 将新结点插入head后
      s->prior = head;    // 新结点的前结点指向头结点
      s->next = p;            // 新结点的后结点指向原head的后结点
      p->prior = s ;          // 原第一个结点的前结点指向新结点
    }
    else
    {
      s = (pDoubleListNode)malloc(sizeof(DoubleListNode));
      s->data = data;
      s->prior = p->prior;
      s->next = p;
      p->prior->next = s;
      p->prior = s;
    }
  }
}
//删除双向链表中的第i个元素
void DoubleList::DeleteNode(pDoubleListNode &head)
{
  int pos;
  int i = 0;
  pDoubleListNode p = head;
  printf("\n在双向链表中删除第i个位置的元素\n");
  printf("请输入要删除的位置:");
  scanf_s("%d", &pos, 100);
  
  if (IsDouListEmpty(head))
  {
    return;
  }
  else if (pos<1 || pos>GetDouListLength(head))
  {
    printf("删除的位置不存在!\n");
  }
  else
  {
    while (i < pos)
    {
      p = p->next;
      i++;
    }
    if (i == GetDouListLength(head))
    {
      p->prior->next = NULL;
      free(p);
    }
    else
    {
      p->prior->next = p->next;
      p->next->prior = p->prior;
      free(p);
    }
  }
}

DoubleList.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
30
31
32
33
#pragma once
typedef struct DoubleListNode
{
  int data;       //数据
  struct DoubleListNode *prior; //前驱
  struct DoubleListNode *next; //后继
}DoubleListNode, *pDoubleListNode;
class DoubleList
{
public:
  DoubleList();
  ~DoubleList();
  //初始化双向链表
  void DoubleList::CreateDouList(pDoubleListNode &head);
  //打印双向链表
  void DoubleList::PrintDouList(pDoubleListNode &head);
  //逆序打印双向链表
  void DoubleList::PrintDouReverseList(pDoubleListNode &head);
  //求链表长度
  int DoubleList::GetDouListLength(pDoubleListNode &head);
  //判断链表是否为空
  bool DoubleList::IsDouListEmpty(pDoubleListNode &head);
  //把双向链表置空
  void DoubleList::ClearDouList(pDoubleListNode &head);
  //删除双向链表
  void DoubleList::DeleteDouList(pDoubleListNode &head);
  //在双向链表中第i个位置后面插入元素m
  void DoubleList::InsertNodeAfter(pDoubleListNode &head);
  // 在双向链表中第i个位置前面插入元素
  void DoubleList::InsertNodeBefore(pDoubleListNode &head);
  //删除双向链表中的第i个元素
  void DoubleList::DeleteNode(pDoubleListNode &head);
};

3.对链表插入节点的理解

例如在节点i前插入一个新的节点(即上面代码中的InsertNodeBefore函数):

链表结构体为:
?
1
2
3
4
5
6
typedef struct DoubleListNode
{
  int data;       // 数据
  struct DoubleListNode *prior; // 前驱
  struct DoubleListNode *next; // 后继
}DoubleListNode, *pDoubleListNode;

假设该链表由五个节点构成,分别为A,B,C,D,E

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

图中假设了A,B,C,D,E的地址分别为:addressA,addressB,addressC,addressD,addressE。

下面将分析链表的前插的例子:

双链表的前插,下面这是在节点"D"前插入一个新的节点"S"的代码和分析

s = (pDoubleListNode)malloc(sizeof(DoubleListNode));  // 申请一段内存空间,指针指向首地址为addressS
s->data = data;     // 给节点S的数据赋值data
s->prior = p->prior;  // p指向D节点, p->prior表示addressC,将它赋给s->prior,则s->prior里面的值是addressC,从而指向addressC这个地址即节点C,如下图S节点的蓝线
s->next = p;      // p是addressD,将它赋给s->next,s->next中的值为addressD,也即s->next指向了D,如下图S节点的红线
p->prior->next = s;  // p->prior 是addressC,即节点C,所以p->prior->next相当于没插入S之前的addressD,插入S后,将S的首地址即addressS赋给这个位置,所以此时,由CD的红线断裂,这个红线目标变成了S,如下图C节点的红线
p->prior = s;     // 同理,p->prior也指向了S,即p->prior中addressC变成了addressS, D指向C的蓝线断裂。变成如下图D节点指向S节点的蓝线.

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

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

延伸 · 阅读

精彩推荐
  • C/C++关于C语言中E-R图的详解

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

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

    Struggler095962021-07-12
  • C/C++c/c++实现获取域名的IP地址

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

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

    C++教程网10262021-03-16
  • C/C++OpenCV实现拼接图像的简单方法

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

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

    iteye_183805102021-07-29
  • 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语言main函数的三种形式实例详解

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

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

    ieearth6912021-05-16
  • C/C++深入C++拷贝构造函数的总结详解

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

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

    C++教程网5182020-11-30
  • C/C++C语言实现双人五子棋游戏

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

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

    两片空白7312021-11-12