# 数据结构——C++中实现双链表（含完整代码）

11 篇文章 1 订阅

1、创建双链表(-1结束)
2、遍历双链表
3、插入双链表元素
4、双链表删除元素
5、判断双链表是否为空
6、双链表的长度
7、查找双链表元素
0、退出程序

#include <iostream>
using namespace std;

typedef struct DNode {
int data;
struct DNode* prior, * next;

}DNode;

DNode* n = (DNode*)malloc(sizeof(DNode));

if (n == NULL) {
cout << "内存不足，分配失败" << endl;
return;
}

#include <iostream>
using namespace std;

typedef struct DNode {
int data;
struct DNode* prior, * next;

//双链表删除元素(按值删除)
void DeleteDNode_Value(DLinkList& L, int a) {

if (LengthList(L) == 0) {
cout << "双链表为空" << endl;
return;
}

DNode* p = L->next;
DNode* q = L;

while (p != NULL) {
if (p->data == a) {
if (p->next == NULL) {
q->next = p->next;
free(p);
}
else {
q->next = p->next;
p->next->prior = q;
free(p);
}
return;
}
p = p->next;
q = q->next;
}
cout << "没找到该数值" << endl;
}

#include <iostream>
using namespace std;

typedef struct DNode {
int data;
struct DNode* prior, * next;

//初始化双链表

L = (DNode*)malloc(sizeof(DNode));

if (L == NULL)//判断L内存开辟是否成功
{
cout << "内存不足，分配失败" << endl;
return;
}

L->prior = NULL;
L->next = NULL;
}

//查找长度
int LengthList(DLinkList L) {
int length = 0;
DNode* m = L->next;
while (m != NULL) {
length++;
m = m->next;
}
return length;
}

//创建一个双链表

DNode* m = L;
int temp;
cout << "请输入要添加的数值." << endl;
cin >> temp;

while (temp != -1) {
DNode* n = (DNode*)malloc(sizeof(DNode));

if (n == NULL) {
cout << "内存不足，分配失败" << endl;
return;
}

n->next = m->next;
n->prior = m;
n->data = temp;
m->next = n;
m = m->next;
cin >> temp;
}

cout << "添加完成" << endl;
}

//遍历打印
DNode* n = L->next;

if (LengthList(L) == 0) {
cout << "双链表为空" << endl;
return;
}

cout << "遍历结果：" << endl;
while (n != NULL) {
cout << n->data << " ";
n = n->next;
}
cout << endl;
}

//插入双链表元素(在a位置插入b)
void InsertDNode(DLinkList& L, int a, int b) {
DNode* temp = (DNode*)malloc(sizeof(DNode));
temp->next = NULL;
temp->prior = NULL;
temp->data = b;

if (temp == NULL) {
cout << "内存不足，分配失败" << endl;
return;
}

DNode* n = L;

while (a != 1) {
n = n->next;
a--;
}

temp->prior = n;
temp->next = n->next;
n->next = temp;
}

//双链表删除元素(按值删除)
void DeleteDNode_Value(DLinkList& L, int a) {

if (LengthList(L) == 0) {
cout << "双链表为空" << endl;
return;
}

DNode* p = L->next;
DNode* q = L;

while (p != NULL) {
if (p->data == a) {
if (p->next == NULL) {
q->next = p->next;
free(p);
}
else {
q->next = p->next;
p->next->prior = q;
free(p);
}
return;
}
p = p->next;
q = q->next;
}
cout << "没找到该数值" << endl;
}

//双链表删除元素(按位删除)
void DeleteDNode_Pos(DLinkList& L, int a) {

if (LengthList(L) == 0) {
cout << "双链表为空" << endl;
return;
}

if (a > LengthList(L))
{
cout << "位序过大" << endl;
return;
}

DNode* p = L->next;
DNode* q = L;
int pos = 1;

while (pos < a)
{
p = p->next;
q = q->next;
pos++;
}

if (pos == a) {
q->next = p->next;
free(p);
}
else {
q->next = p->next;
p->next->prior = q;
free(p);
}
}

//判断双链表是否为空
if (L->next == NULL)
return true;
return false;
}

//查找双链表的元素
void Lookup_DLinnkList(DLinkList L, int i) {
DNode* temp = L->next;
int pos = 1;

while (pos <= LengthList(L)) {
if (temp->data == i) {
cout << "该值的位序为： " << pos << endl;
return;
}
pos++;
temp = temp->next;
}

cout << "在该双链表中没有找到该值" << endl;
}

int main() {

DNode* n = (DNode*)malloc(sizeof(DNode));
if (n == NULL) {
cout << "内存不足，分配失败" << endl;
return 0;
}
n = L;

int select;

while (true) {
cout << "=========================================" << endl;
cout << "=======|  1、创建双链表(-1结束)  |=======" << endl;
cout << "=======|  2、遍历双链表          |=======" << endl;
cout << "=======|  3、插入双链表元素      |=======" << endl;
cout << "=======|  4、双链表删除元素      |=======" << endl;
cout << "=======|  5、判断双链表是否为空  |=======" << endl;
cout << "=======|  6、双链表的长度        |=======" << endl;
cout << "=======|  7、查找双链表元素      |=======" << endl;
cout << "=======|  0、退出程序            |=======" << endl;
cout << "=========================================" << endl;
cout << "你要实现的功能：" << endl;
cout << "================" << endl;
cin >> select;

switch (select) {

case 1://创建双链表
system("pause");
system("cls");
break;

case 2://遍历双链表
system("pause");
system("cls");
break;

case 3://插入双链表元素
int i, j;
cout << "请输入要插入的位置: " << endl;
cin >> i;
cout << "请输入要插入的值: " << endl;
cin >> j;
InsertDNode(L, i, j);
system("pause");
system("cls");
break;

case 4://双链表删除元素
int select;
int temp;
cout << "按什么方式删除元素：" << endl;
cout << "1 --  按值删除" << endl;
cout << "2 --  按位删除" << endl;
cin >> select;
if (select == 1) {
cout << "要删除的值为：" << endl;
cin >> temp;
DeleteDNode_Value(L, temp);
}
else if (select == 2) {
cout << "要删除的位数为：" << endl;
cin >> temp;
DeleteDNode_Pos(L, temp);
}
system("pause");
system("cls");
break;

case 5://判断双链表是否为空
cout << "双链表为空" << endl;
}
else {
cout << "双链表不为空" << endl;
}
system("pause");
system("cls");
break;

case 6://双链表的长度
cout << "双链表长度：" << LengthList(L) << endl;
system("pause");
system("cls");
break;

case 7://查找双链表元素
int val;
cout << "你要查找的值为：" << endl;
cin >> val;
Lookup_DLinnkList(L, val);
system("pause");
system("cls");
break;

case 0://退出程序
cout << "已退出" << endl;
exit(0);
break;

default://其他情况
cout << "输入错误，请重新输入" << endl;
system("pause");
system("cls");
break;
}
}
return 0;
}

• 5
点赞
• 11
收藏
• 打赏
• 0
评论
07-01 929
09-16 332
09-22 634
08-14 1836
10-21 6672

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

©️2022 CSDN 皮肤主题：游动-白 设计师：我叫白小胖

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。