文章目录
一、部分实现
1.双向循环链式存储结构

代码如下:
/*链式存储结构*/
typedef struct DuLNode
{
ElemType data;
struct DuLNode* prior;
struct DuLNode* next;
}DuLNode,*DuLinkList;
2.初始化双向循环链表


代码如下:
/*初始化双向循环链表*/
ElemType InitDuList(DuLinkList&L)
{
L = new DuLNode; //头指针指向头结点
if (L == NULL)
{
return ERROR;
}
L->next = L; //后指针域指向自己
L->prior = L; //前指针域指向自己
return OK;
}
3.使用尾插法插入结点
代码如下:
/*使用尾插法插入结点*/
ElemType InitInsertDuList(DuLinkList&L,int number)
{
DuLinkList p = L;
while (number--)
{
DuLinkList q = new DuLNode; //创建插入的结点,头指针P指向该结点
if (p==NULL)
{
return ERROR;
}
cin >> q->data; //输入插入结点的元素值
/*尾插法*/
p->next = q;
q->prior = p;
q->next = L;
L->prior = q;
p = q;
}
}
4.插入第i个结点(在第i个结点之前插入)

代码如下:
/*插入第i结点(在第i个结点之前插入)后面插入的操作*/
ElemType InsertDuList(DuLinkList&L, int i, ElemType elem)
{
DuLinkList pre = L;
int j = 0;
while (j<i)
{
pre = pre->next; //移动到了第i个结点的位置,pre指向第i个结点
j++;
if ((pre == L)) return ERROR;
}
DuLinkList s = new DuLNode;
s->data = elem;
s->next = pre; //插入操作
s->prior = pre->prior;
pre->prior->next = s;
pre->prior = s;
}
5.删除第i个结点

代码如下:
/*删除第i个结点操作,并将删除的结点元素赋值给e*/
ElemType DeleDuList(DuLinkList&L, int i, ElemType&e)
{
DuLinkList pre = L;
int j = 0;
while (j < i)
{
pre = pre->next;
j++;
if ((pre==L)) return ERROR;
}
e = pre->data;
pre->prior->next = pre->next; //删除操作
pre->next->prior = pre->prior;
delete pre;
return OK;
}
6.遍历双向循环链表
代码如下:
void PrintDuList(DuLinkList L)
{
DuLinkList p = L->next;
while (p!=L)
{
cout << p->data << endl;
p = p->next;
}
cout << endl;
}
7.主函数
代码如下:
int main()
{
DuLinkList DuList;
ElemType elem;
/*初始化操作*/
if (!(InitDuList(DuList))) cout << "初始化失败" << endl;
/*初始化插入5个结点操作*/
InitInsertDuList(DuList, 5);
cout << "*****************" << endl;;
/*删除操作*/
if(DeleDuList(DuList, 3,elem)==ERROR) cout << "删除操作失败" << endl;
/*遍历输出操作*/
PrintDuList(DuList);
/*插入操作*/
if (InsertDuList(DuList, 3, 3) == ERROR) cout << "插入操作失败" << endl;
/*遍历输出操作*/
PrintDuList(DuList);
return 0;
}
二、全部代码
代码如下:
#include<iostream>
#include<stdio.h>
using namespace std;
typedef int ElemType;
#define ERROR -1
#define OK 1
/*链式存储结构*/
typedef struct DuLNode
{
ElemType data;
struct DuLNode* prior;
struct DuLNode* next;
}DuLNode,*DuLinkList;
/*初始化双向循环链表*/
ElemType InitDuList(DuLinkList&L)
{
L = new DuLNode; //头指针指向头结点
if (L == NULL)
{
return ERROR;
}
L->next = L; //后指针域指向自己
L->prior = L; //前指针域指向自己
return OK;
}
/*使用尾插法插入结点*/
ElemType InitInsertDuList(DuLinkList&L,int number)
{
DuLinkList p = L;
while (number--)
{
DuLinkList q = new DuLNode; //创建插入的结点,头指针P指向该结点
if (p==NULL)
{
return ERROR;
}
cin >> q->data; //输入插入结点的元素值
/*尾插法*/
p->next = q;
q->prior = p;
q->next = L;
L->prior = q;
p = q;
}
}
/*插入第i结点(在第i个结点之前插入)后面插入的操作*/
ElemType InsertDuList(DuLinkList&L, int i, ElemType elem)
{
DuLinkList pre = L;
int j = 0;
while (j<i)
{
pre = pre->next; //移动到了第i个结点的位置,pre指向第i个结点
j++;
if ((pre == L)) return ERROR;
}
DuLinkList s = new DuLNode;
s->data = elem;
s->next = pre; //插入操作
s->prior = pre->prior;
pre->prior->next = s;
pre->prior = s;
}
/*删除第i个结点操作,并将删除的结点元素赋值给e*/
ElemType DeleDuList(DuLinkList&L, int i, ElemType&e)
{
DuLinkList pre = L;
int j = 0;
while (j < i)
{
pre = pre->next;
j++;
if ((pre==L)) return ERROR;
}
e = pre->data;
pre->prior->next = pre->next; //删除操作
pre->next->prior = pre->prior;
delete pre;
return OK;
}
/*遍历双向循环链表*/
void PrintDuList(DuLinkList L)
{
DuLinkList p = L->next;
while (p!=L)
{
cout << p->data << endl;
p = p->next;
}
cout << endl;
}
int main()
{
DuLinkList DuList;
ElemType elem;
/*初始化操作*/
if (!(InitDuList(DuList))) cout << "初始化失败" << endl;
/*初始化插入5个结点操作*/
InitInsertDuList(DuList, 5);
cout << "*****************" << endl;;
/*删除操作*/
if(DeleDuList(DuList, 3,elem)==ERROR) cout << "删除操作失败" << endl;
/*遍历输出操作*/
PrintDuList(DuList);
/*插入操作*/
if (InsertDuList(DuList, 3, 3) == ERROR) cout << "插入操作失败" << endl;
/*遍历输出操作*/
PrintDuList(DuList);
return 0;
}
三、运行结果


5807

被折叠的 条评论
为什么被折叠?



