#include <stdio.h>
#include <stdlib.h>
#define OVERFLOW -2
#define OK 1
#define ERROR 0
//此双向循环链表无头结点
typedef int ElemType;
typedef struct DulNode {
ElemType data;
struct DulNode *prior;
struct DulNode *next;
}DulNode,*DulLinkList;
DulLinkList InitList_Dul(DulLinkList L);
DulLinkList CreateElemP_Dul(DulLinkList L);
void PrintList_Dul(DulLinkList L);
int GetElemP1_Dul(DulLinkList L,int i,ElemType *e);
DulLinkList ListInsert_Dul(DulLinkList L,int i,ElemType e);
int ListLength_Dul(DulLinkList L);
DulLinkList GetElemP2_Dul(DulLinkList L,int i);
DulLinkList ListInsert_Dul(DulLinkList L,int i,ElemType e);
DulLinkList ListDelete_Dul(DulLinkList L,int i,ElemType *e);
int GetLocate_Dul(DulLinkList L,ElemType e);
int main()
{
DulLinkList dul;
int len,loc;
ElemType e;
dul = InitList_Dul(dul);
dul = CreateElemP_Dul(dul);
len = ListLength_Dul(dul);
printf("该双向链表的长度为:%d
",len);
PrintList_Dul(dul);
GetElemP1_Dul(dul,3,&e);//3为元素所在的位序
printf("第i个位置的元素为:%d
",e);
printf("向链表中插入一个元素后:");
dul = ListInsert_Dul(dul,1,0);//1为插入元素的位置
PrintList_Dul(dul);
printf("从链表删除一个元素后:");
dul = ListDelete_Dul(dul,2,&e);//2为删除元素的位序
PrintList_Dul(dul);
printf("删除的元素是:%d
",e);
loc = GetLocate_Dul(dul,0);//0为元素的值
printf("该双向循环链表元素i的位序为:%d
",loc);
return 0;
}
//初始化一个双向循环链表
DulLinkList InitList_Dul(DulLinkList L) {
L = (DulLinkList)malloc(sizeof(DulNode));
if(L != NULL) {
L->next = L->prior = L;
} else {
exit(OVERFLOW);
}
return L;
}
//创建一个双向循环链表
DulLinkList CreateElemP_Dul(DulLinkList L) {
int n,i;
DulLinkList p,s;
s = L;
printf("请输入双向循环链表的元素个数:");
scanf("%d",&n);
printf("请输入双向循环链表中元素的值:");
for(i=0; i<n; i++) {
p = (DulLinkList)malloc(sizeof(DulNode));
scanf("%d",&p->data);
p->prior = s->prior;
s->prior->next = p;
p->next = s;
s->prior = p;
}
return L;
}
//输出该双向循环链表
void PrintList_Dul(DulLinkList L) {
int i;
DulLinkList p;
p = L;
printf("该循环链表中的元素为:");
while(p->next != L) {
p = p->next;
printf("%d ",p->data);
}
printf("
");
}
//返回双向循环链表的长度
int ListLength_Dul(DulLinkList L) {
DulLinkList p;
int i;
p = L->next;
i = 0;
while((p!=L) && (p!=NULL)) {
++i;
p = p->next;
}
return i;
}
//取得双向循环链表上第i个位置上的元素的值
int GetElemP1_Dul(DulLinkList L,int i,ElemType *e) {
DulLinkList p;
int j;
p = L;
j = 0;
while(p->next && j<i) {
++j;
p = p->next;
}
if(!p || j>i) return ERROR;
*e = p->data;
return OK;
}
//返回双向循环链表上第i个位置上的指针
DulLinkList GetElemP2_Dul(DulLinkList L,int i) {
DulLinkList p;
int j;
p = L;
j = 0;
while(p->next && j<i) {
++j;
p = p->next;
}
if(!p || j>i) return ERROR;
return p;
}
//向该双向循环链表的第i个位置之前插入元素e
DulLinkList ListInsert_Dul(DulLinkList L,int i,ElemType e) {
DulLinkList p,s;
int m;
if(!(p=GetElemP2_Dul(L,i))) {
return ERROR;
}
if(!(s = (DulLinkList)malloc(sizeof(DulNode)))) {
return ERROR;
}
s->data = e;
s->prior = p->prior;
p->prior->next = s;
s->next = p;
p->prior = s;
return L;
}
//删除双向循环链表上第i个位置的元素,并返回该元素的值
DulLinkList ListDelete_Dul(DulLinkList L,int i,ElemType *e) {
DulLinkList p;
if(!(p=GetElemP2_Dul(L,i))) {
return ERROR;
}
*e = p->data;
p->prior->next = p->next;
p->next->prior = p->prior;
return L;
}
//返回双向循环链表上元素e的位序
int GetLocate_Dul(DulLinkList L,ElemType e) {
DulLinkList p;
int j;
p = L->next;
j = 0;
while((p != L) && (p->data!=e)) {
j++;
}
return j;
}