# 双向循环链表----C语言

#include <stdio.h>
#include <stdlib.h>

typedef int Elemtype;
typedef int Status;

typedef struct node
{
Elemtype data;
struct node *next, *prior;
}ListNode;

//双向循环链表初始化
{
L->prior = L;
L->next = L;
}

//判断双向循环链表是否为空
{
if(L->next == L)
return 1;
else
return 0;
}

//销毁双向循环链表
{
ListNode *p = L->next, *r;
while(p != NULL)
{
r = p->next;
free(p);
p = r;
}
L->next = L;
}

//双向循环链表的长度
{
ListNode *p = L->next;
int j = 1;
if(ListEmpty(L))
return 0;
while(p->next != L)
{
p = p->next;
j++;
}
return j;
}

//创建双向循环链表(头插法)
{
ListNode *p = L, *s, *r;
int i;
printf("Input:");
for(i = 1; i <= n; i++)
{
s = (ListNode *)malloc(sizeof(ListNode));
scanf("%d", &s->data);
s->next = p;
p->prior = s;
p = s;
}
L->next = p;
p->prior = L;
}

//创建双向循环链表(尾插法)
{
int i;
ListNode *p = L, *s;
for(i = 1; i <= n; i++)
{
s = (ListNode *)malloc(sizeof(ListNode));
scanf("%d", &s->data);
p->next = s;
s->prior = p;
p = s;
}
L->prior = p;
p->next = L;
}*/

//遍历双向循环链表
{
ListNode *p = L->next;
while(p->next != L)
{
printf("%d ", p->data);
p = p->next;
}
if(p->next == L)
printf("%d", p->data);
printf("\n");
}

//返回第i个位置的元素
Status GetElem(LinkList L, int i, Elemtype *e)
{
int j = 1;
ListNode *p = L->next;
if(ListEmpty(L)|| i < 1 || i > ListLength(L))
return 0;
while(p != L && j < i)
{
p = p->next;
j++;
}
*e = p->data;
}

//查找元素e的位置
{
int i = 1;
ListNode *p = L->next;
if(ListEmpty(L))
return 0;
while(p->next != L && p->data != e)
{
p = p->next;
i++;
}
if(p->next == L && p->data != e)
return 0;
return i;
}

//第i个位置插入元素e
Status ListInsert(LinkList L, int i, Elemtype e)
{
int j = 1;
ListNode *p = L->next, *s;
if(ListEmpty(L) || i < 1 || i > ListLength(L) + 1)
return 0;
if(i == ListLength(L) + 1)
while(p != L && j < i)
{
p = p->next;
j++;
}
s = (ListNode *)malloc(sizeof(ListNode));
s->data = e;
if(p == L && j == i)
{
s->next = p->prior->next;
p->prior->next = s;
s->prior = p->prior;
L->prior = s;
}
else
{
s->next = p;
p->prior->next = s;
s->prior = p->prior;
p->next->prior = s;
}
return 1;
}

//删除第i个位置的元素
Status ListDelete(LinkList L, int i, Elemtype *e)
{
int j = 1;
ListNode *p = L->next;
if(ListEmpty(L) || i < 1 || i > ListLength(L))
return 0;
while(p->next != L && j < i)
{
p = p->next;
j++;
}
if(p->next == L && j == i)
{
L->prior = p->prior;
p->prior->next = L;
}
else
{
p->prior->next = p->next;
p->next->prior = p->prior;
}
*e = p->data;
free(p);
return 1;
}

//在值为x的结点之前插入一个值为y的结点
void DinsertB(LinkList L, Elemtype x, Elemtype y)
{
ListNode *p = L->next, *s;
s = (ListNode *)malloc(sizeof(ListNode));
s->data = y;
while(p != L && p->data != x)
p = p->next;
if(p == L)
printf("error\n");
else
{
s->next = p;
s->prior = p->prior;
p->prior->next = s;
p->prior = s;
}
}

int main()
{
int n, i;
Elemtype e, f;
InitList(L);
printf("Input ListCreate n:");
scanf("%d", &n);
ListCreate(L, n);
if(!ListEmpty(L))
{
printf("ListLength L:%d\n", ListLength(L));
printf("L:");
ListTra(L);
printf("Input GetElem i:");
scanf("%d", &i);
if(GetElem(L, i, &e))
printf("GetElem e:%d\n", e);
else
printf("error\n");
printf("Input LocateElem e:");
scanf("%d", &e);
printf("LocateElem i:%d\n", LocateElem(L, e));
printf("Input ListInsert i,e:");
scanf("%d %d", &i, &e);
if(ListInsert(L, i, e))
ListTra(L);
else
printf("error\n");
printf("Input ListDelete i:");
scanf("%d", &i);
if(ListDelete(L, i, &e))
{
ListTra(L);
printf("ListDelete e:%d\n", e);
}
else
printf("error\n");
printf("Input DinsertB e, f:");
scanf("%d %d", &e, &f);
DinsertB(L, e, f);
ListTra(L);
}
else
printf("ListEmpty\n");
return 0;
}