双链表相关代码。
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;
}
在进行相关操作时,记得考虑临界状态,确保代码的正确运行。
以下代码考虑了删除元素为最后一位数值的情况使用if语句进行单独操作。
#include <iostream>
using namespace std;
typedef struct DNode {
int data;
struct DNode* prior, * next;
}DNode, * DLinkList;
//双链表删除元素(按值删除)
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;
}DNode, * DLinkList;
//初始化双链表
void InisDLinkList(DLinkList& L) {
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;
}
//创建一个双链表
void InsertDLinkList(DLinkList& L) {
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;
}
//遍历打印
void PrintDLinkList(DLinkList& L) {
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);
}
}
//判断双链表是否为空
bool Empty_DLinkList(DLinkList L) {
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() {
DLinkList L;
InisDLinkList(L);
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://创建双链表
InsertDLinkList(L);
system("pause");
system("cls");
break;
case 2://遍历双链表
PrintDLinkList(L);
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://判断双链表是否为空
if (Empty_DLinkList(L)) {
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;
}
部分运行结果如下: