#include<stdio.h>
typedef struct Node {
int data;
struct Node* next,* last;}DLL;// 初始化DLL*InitList(DLL*L){L=(DLL*)malloc(sizeof(DLL));L->next =NULL;L->last =NULL;DLL* p,* q;
q =L;
int dat;printf("请输入你的数据:");scanf("%d",&dat);while(dat){
p =(DLL*)malloc(sizeof(DLL));
p->data = dat;
p->next = q->next;
q->next = p;
p->last = q;
q = p;printf("请输入你的数据:");scanf("%d",&dat);}returnL;}// 插入操作
int InsertList(DLL*L, int i, int e){if(i <1|| i >LengthList(L)+1){printf("插入的位置有误!");return;}DLL* p,* q;
q =L;for(int l =1; l < i; l++){
q = q->next;}
p =(DLL*)malloc(sizeof(DLL));
p->data = e;
p->next = q->next;
q->next = p;
p->last = q;}// 删除操作
int DeleteList(DLL*L, int i){if(EmptyList(L)){printf("双链表为空!");return;}if(i <1|| i >LengthList(L)+1){printf("插入的位置有误!");return;}DLL* p;
p =L;for(int l =1; l < i; l++){
p = p->next;}
p->next = p->next->next;
p->next->last = p;}// 判断是否为空
int EmptyList(DLL*L){return LengthList ==0;}// 返回双链表的长度
int LengthList(DLL*L){DLL* p;
p =L->next;
int count =0;while(p !=NULL){
count++;
p = p->next;}return count;}// 遍历双链表
int TraverseList(DLL*L){DLL* p;
p =L->next;while(p !=NULL){printf("%d ", p->data);
p = p->next;}putchar('\n');}
int main(){DLL*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;}}}