#include<stdio.h>
typedef struct Node {
int data;
struct Node* next,* last;}DCLL;// 初始化DCLL*InitList(DCLL*L){L=(DCLL*)malloc(sizeof(DCLL));L->next =L;L->last =L;DCLL* p,* q;
q =L;
int dat;printf("请输入你的数据:");scanf("%d",&dat);while(dat){
p =(DCLL*)malloc(sizeof(DCLL));
p->data = dat;
p->next = q->next;
q->next = p;
p->last = q;
q = p;printf("请输入你的数据:");scanf("%d",&dat);}L->last = q;returnL;}// 插入操作
int InsertList(DCLL*L, int i, int e){if(i <1|| i >LengthList(L)+1){printf("插入的位置有误!");return;}DCLL* p,* q;
q =L;for(int l =1; l < i; l++){
q = q->next;}
p =(DCLL*)malloc(sizeof(DCLL));
p->data = e;
p->next = q->next;
q->next = p;
p->last = q;}// 删除操作
int DeleteList(DCLL*L, int i){if(EmptyList(L)){printf("双链表为空!");return;}if(i <1|| i >LengthList(L)+1){printf("插入的位置有误!");return;}DCLL* p;
p =L;for(int l =1; l < i; l++){
p = p->next;}
p->next = p->next->next;
p->next->last = p;}// 判断是否为空
int EmptyList(DCLL*L){return LengthList ==0;}// 返回双循环链表的长度
int LengthList(DCLL*L){DCLL* p;
p =L->next;
int count =0;while(p !=L){
count++;
p = p->next;}return count;}// 遍历双循环链表
int TraverseList(DCLL*L){DCLL* p;
p =L->next;while(p !=L){printf("%d ", p->data);
p = p->next;}putchar('\n');}
int main(){DCLL*L=NULL;L=InitList(L);
int select, i, e;while(1){printf("1、插入元素 2、删除元素 3、遍历输出 4、退出程序\n");printf("请输入你要进行的操作:");scanf("%d",&select);if(select ==1){printf("请输入你要插入的位置:");scanf("%d",&i);printf("请输入你要插入的元素:");scanf("%d",&e);InsertList(L, i, e);}elseif(select ==2){printf("请输入你要删除的位置:");scanf("%d",&i);DeleteList(L, i);}elseif(select ==3){TraverseList(L);}else{break;}}}