#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
typedef int ElemType, Status;
typedef struct LNode //双向循环链表节点
{
ElemType data; //数据
struct LNode* next; //指向下一个节点指针
struct LNode* prev; //指向前一个节点指针
}LNode, *DuCircList;
typedef struct DuCircListInfo //双向循环链表信息
{
DuCircList head; //头指针
LNode* pCurNode; //当前节点指针位置
int length; //双向循环链表长度
}DuCircListInfo;
Status InitList(DuCircListInfo& L) //双向循环链表初始化
{
L.head = (LNode*)malloc(sizeof(LNode));
if (L.head == 0) exit(0);
L.head->next = NULL;
L.head->prev = NULL;
L.length = 0;
L.pCurNode = L.head;
return OK;
}
Status DestroyList(DuCircListInfo& L) //双向循环链表销毁
{
if (L.head == NULL) return OK;
LNode* p = L.head;
LNode* q = p;
while (p)
{
p = p->next;
free(q);
q = p;
}
L.head = NULL;
L.length = 0;
L.pCurNode = NULL;
return OK;
}
Status ClearList(DuCircListInfo& L) //双向循环链表清空
{
if (L.length == 0) return OK;
LNode* p = L.head->next;
LNode* q = p;
while (p->next != L.head)
{
p = p->next;
free(q);
q = p;
}
free(p);
L.head->next = NULL;
L.head->prev = NULL;
L.length = 0;
L.pCurNode = NULL;
return OK;
}
Status ListEmpty(DuCircListInfo L) //双向循环链表判空
{
if (L.length == 0) return OK;
return ERROR;
}
Status GetElem(DuCircListInfo L, int i, ElemType& e) //双向循环链表元素获取
{
if (L.length == 0) return ERROR;
if (i < 1 || i > L.length) return ERROR;
LNode* p = L.head;
int j = 0;
while (j < i)
{
p = p->next;
j++;
}
e = p->data;
return OK;
}
Status LocateElem(DuCircListInfo L, ElemType e) //双向循环链表元素查找
{
if (L.length == 0) return ERROR;
LNode* p = L.head->next;
int i = 1;
while (i <= L.length)
{
if (p->data == e) return i;
p = p->next;
i++;
}
return ERROR;
}
Status PriorElem(DuCircListInfo L, ElemType cur_e, ElemType& pre_e) //双向循环链表元素前驱
{
if (L.length == 0) return ERROR;
LNode* p = L.head->next;
int i = 1;
while (i <= L.length)
{
if (p->data == cur_e)
{
if (i == 1)
{
pre_e = p->prev->prev->data;
return OK;
}
pre_e = p->prev->data;
return OK;
}
p = p->next;
i++;
}
return ERROR;
}
Status NextElem(DuCircListInfo L, ElemType cur_e, ElemType& next_e) //双向循环链表元素后继
{
if (L.length == 0) return ERROR;
LNode* p = L.head->next;
int i = 1;
while (i <= L.length)
{
if (p->data == cur_e)
{
if (i == L.length)
{
next_e = p->next->next->data;
return OK;
}
next_e = p->next->data;
return OK;
}
p = p->next;
i++;
}
return ERROR;
}
Status ListTraverse(DuCircListInfo L) //双向循环链表遍历
{
if (L.length == 0) return ERROR;
LNode* p = L.head->next;
while (p != L.head)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
return OK;
}
Status SetElem(DuCircListInfo& L, int i, ElemType& e) //双向循环链表元素替换
{
if (L.length == 0) return ERROR;
if (i < 1 || i > L.length) return ERROR;
LNode* p = L.head->next;
int j = 1;
while (j < i)
{
p = p->next;
j++;
}
ElemType temp = e;
e = p->data;
p->data = temp;
return OK;
}
Status InsertElem(DuCircListInfo& L, int i, ElemType e) //双向循环链表元素插入
{
if (i < 1 || i > L.length + 1) return ERROR;
LNode* p = (LNode*)malloc(sizeof(LNode));
if (p == NULL) exit(0);
p->data = e;
LNode* q = L.head;
int j = 0;
while (j < i - 1)
{
q = q->next;
j++;
}
p->next = q->next;
p->prev = q;
q->next = p;
p->next->prev = p;
L.length = L.length + 1;
return OK;
}
Status DeleteElem(DuCircListInfo& L, int i, ElemType& e) //双向循环链表元素删除
{
if (L.length == 0) return ERROR;
if (i < 1 || i > L.length) return ERROR;
LNode* p = L.head->next;
int j = 1;
while (j < i)
{
p = p->next;
j++;
}
e = p->data;
p->prev->next = p->next;
p->next->prev = p->prev;
L.length = L.length - 1;
free(p);
p = NULL;
return OK;
}
Status CreateList(DuCircListInfo& L) //双向循环链表创建
{
L.head = (LNode*)malloc(sizeof(LNode));
if (L.head == NULL) exit(0);
L.head->next = NULL;
L.head->prev = NULL;
L.length = 0;
L.pCurNode = L.head;
int n;
printf("双向循环链表长度:");
scanf_s("%d", &n);
LNode* p = L.head;
int i = 0;
printf("链表元素:");
while (i < n)
{
ElemType e;
scanf_s("%d", &e);
LNode* q = (LNode*)malloc(sizeof(LNode));
if (q == NULL) exit(0);
q->data = e;
q->next = L.head;
q->prev = L.pCurNode;
L.pCurNode->next = q;
L.pCurNode = q;
L.length = L.length + 1;
i++;
}
L.head->prev = L.pCurNode;
return OK;
}
int main()
{
DuCircListInfo L;
CreateList(L);
ListTraverse(L);
InsertElem(L, 1, 0);
ListTraverse(L);
InsertElem(L, 7, 6);
ListTraverse(L);
ElemType e;
DeleteElem(L, 7, e);
ListTraverse(L);
PriorElem(L, 1, e);
printf("1的前驱元素:%d\n", e);
NextElem(L, 1, e);
printf("1的后继元素:%d\n", e);
e = 0;
SetElem(L, 2, e);
ListTraverse(L);
return 0;
}
运行结果如下: