# 线性表之双向循环链表

//exp2-5.c
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define OK 1
#define ERROR 0

//========================循环双向链表     结构说明=============
typedef int ElemType;
typedef struct LNode{
ElemType data;
struct LNode *next;
struct LNode *prior;
}CDListNode;

//内置申请节点函数
if(p != NULL){
p->next = p->prior = NULL;
}
return p;
}
//初始化
if(*L == NULL){
fprintf(stderr, "初始化失败！\n");
exit(EXIT_FAILURE);
}
(*L)->next = (*L)->prior = *L;
}

//销毁表
CDLinkList p = L, q = p->next;
while(q != L){
free(p);
p = q;
q = p->next;
}
free(p);
}

//判断表是否为空
return L->next == L;
}

//求表长
int i = 0;
while(p->next != L){
i++;
p = p->next;
}
return i;
}

//输出表
while(p != L){
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
//求i位置的元素
int GetElem(CDLinkList L, int i, ElemType *e){
int j;
while(j < i && p->next != L){
i++;
p = p->next;
}
if(p == L){
return ERROR;
}
else{
*e = p->data;
return OK;
}
}

//求元素的位置
int i = 1;
while(p != L && p->data != e){
i++;
p = p->next;
}
if(p == L){
return ERROR;
}
else{
return i;
}
}

//插入元素
int InsertList(CDLinkList L, int i, ElemType e){
int j = 0;

//查找位置
if(p->next == L){                      //若为空表
pNew = newNode();
pNew->data = e;
p->next = pNew;
pNew->next = p;
p->prior = pNew;
pNew->prior = p;
return OK;
}
else if(j == 1){                     //表不为空，但插入第一个位置
pNew = newNode();
pNew->data = e;
pNew->next = p->next;
pNew->next->prior = pNew;
p->next = pNew;
pNew->prior = p;
return OK;
}
else{
p = L->next;
while(j < i - 2 && p != L){            //查找前驱
j++;
p = p->next;
}
if(p == L)
return ERROR;
else{
pNew = newNode();
pNew->data = e;
pNew->next = p->next;
if(pNew->next != NULL)              //非尾节点
pNew->next->prior = pNew;
p->next = pNew;
pNew->prior = p;
return OK;
}
}
}

//删除
int DeleteList(CDLinkList L, int i, ElemType *e){
int j = 0;

if(p->next != L){
if(i == 1){
q = p->next;
*e = q->data;
p->next = q->next;
q->next->prior = p;
return OK;
}
else{
p = L->next;
while(j < i - 2 && p != L){
j++;
p = p->next;
}
if(p == L)
return ERROR;
else{
q = p->next;           //要被删除的节点
if(q == L)            //不存在q
return ERROR;
*e = q->data;
p->next = q->next;
if(q->next != NULL)       //非尾节点
q->next->prior = p;
free(q);
return OK;
}//else
}//else
}//if
else                 //空表
return ERROR;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120