C++实现双向链表的相关操作代码
#include<iostream>
#include<cstdlib>
using namespace std;
#define OK 1
#define ERROR 0
#define MAXSIZE 100
typedef int Elemtype;
typedef int Status;
typedef struct DuLNode//双向链表的存储结构
{
Elemtype data;//数据域
struct DuLNode* prior;//地址域
struct DuLNode* next;//地址域
}DuLNode, * DuLinklist;
Status InitDuLinklist(DuLinklist& L);//初始双向链表
Status CreateDuLinklist(DuLinklist& L, int n);//创建双向链表
Status GetDuLinklist(DuLinklist L, int i, Elemtype& e);//获取双向链表
Status LocateDuLinklist(DuLinklist L, Elemtype e);//双向链表中查找元素
Status InsertDuLinklist(DuLinklist& L, int i, Elemtype& e);//在双向链表中插入元素
Status DeleteDuLinklist(DuLinklist& L, int i, Elemtype& e);//在双向链表中删除元素
Status PrintDuLinklist(DuLinklist L);//双向链表打印
int main(void)
{
DuLinklist L;
L = NULL;
int k = 0;
int len = 0;
int e = 0;
int i = 0;
do {
cout << "*****双向链表的相关操作*****";
cout << "\n1.初始化双向链表";
cout << "\n2.创建双向链表";
cout << "\n3.获取双向链表";
cout << "\n4.双向链表中查找元素";
cout << "\n5.在双向链表中插入元素";
cout << "\n6.在双向链表中删除元素";
cout << "\n7.双向链表打印";
cout << "\n0.结束程序";
cout << "\n1.初始化双向链表";
cout << "\n输入想要进行的操作数k:";
cin >> k;
switch (k)
{
case 1:
if (InitDuLinklist(L))
cout << "初始化成功。" << endl;
else
cout << "初始化失败。" << endl;
break;
case 2:
cout << "输入想要创建双向链表元素的len的值为:";
cin >> len;
if (CreateDuLinklist(L, len))
cout << "创建成功。" << endl;
else
cout << "创建失败。" << endl;
break;
case 3:
cout << "\n输入想要插入的元素e和想要插入的位置i分别为:";
cin >> e >> i;
if (InsertDuLinklist(L, i, e))
cout << "插入成功。" << endl;
else
cout << "插入失败。" << endl;
break;
case 4:
cout << "\n想要删去元素的位置i:";
cin >> i;
if (DeleteDuLinklist(L, i, e))
cout << "删去成功。" << endl;
else
cout << "删除失败。" << endl;
break;
case 5:
cout << "\n想要查找元素e为:";
cin >> e;
if (LocateDuLinklist(L, e))
cout << "查找成功。" << endl;
else
cout << "查找失败。" << endl;
break;
case 6:
cout << "\n想要获取的元素位置:";
cin >> i;
if (GetDuLinklist(L, i, e))
cout << "获取成功。" << endl;
else
cout << "获取失败。" << endl;
break;
case 7:
if (PrintDuLinklist(L))
cout << "打印成功。" << endl;
else
cout << "打印失败。" << endl;
break;
}
} while (k != 0);
return 0;
}
Status InitDuLinklist(DuLinklist& L)//初始双向链表
{
L = new DuLNode;
L->next = NULL;
L->prior = NULL;
return OK;
}
Status CreateDuLinklist(DuLinklist& L, int n)//创建双向链表
{
DuLNode* p;
int e = 0;
cout << "输入创建双向链表的数值:";
for (int i = 0; i < n; i++)
{
p = new DuLNode;
cin >> e;
p->prior = L;
p->data = e;
p->next = L->next;//将节点*p插入到头节点之后
L->next = p;
}
return OK;
}
Status GetDuLinklist(DuLinklist L, int i, Elemtype& e)//获取双向链表
{
DuLNode* p;
p = L->next;//*p节点是首元结点
int j = 1;
while (p && j != i)
{
p = p->next;
j++;
}
if (!p || j > i) return ERROR;//确定指针p
e = p->data;
cout << "获取的元素是" << e << endl;
return OK;
}
Status LocateDuLinklist(DuLinklist L, Elemtype e)//双向链表中查找元素
{
DuLNode* p;
p = L->next;
int j = 1;
while (p&&p->data!=e)
{
p = p->next;
j++;
}
cout << "元素" << e << "所在的位置是第" << j << "个节点";
return OK;
}
Status InsertDuLinklist(DuLinklist& L, int i, Elemtype& e)//在双向链表中插入元素
{
DuLNode* p, * s;
p = L->next;
int j = 1;
while (p && j != i)
{
p = p->next;
j++;
}
if (!p || j > i) return ERROR;//确定指针p
s = new DuLNode;
s->data = e;
s->prior = p->prior;//将节点*s插入到双向链表
p->prior->next = s;
s->next = p;//将*p节点接到*s节点之后
p->prior = s;
return OK;
}
Status DeleteDuLinklist(DuLinklist& L, int i, Elemtype& e)//在双向链表中删除元素
{
DuLNode* p;
p = L->next;
int j = 1;
while (p && j != i)
{
p = p->next;
j++;
}
if (!p || j > i) return ERROR;//确定指针*p
p->prior->next = p->next;//被删节点*p的前驱结点的后继为*p的后一个
p->next->prior = p->prior;//被删结点*p的后继结点的前驱为*p的前一个
delete p;
return OK;
}
Status PrintDuLinklist(DuLinklist L)//双向链表打印
{
DuLNode* p;
p = L->next;
cout << "双向链表的元素为:";
while (p)
{
cout << p->data << " ";
p = p->next;
}
return OK;
}