#include<stdio.h>
#include <stdlib.h>
typedef int ElemType;
typedef struct DNode {
ElemType data;
struct DNode *prior, *next;
} DNode;
DNode* DList_TailInsert(DNode *L) {
int x;
L = (DNode*) malloc(sizeof(DNode));
DNode *s;
DNode *r = L;
scanf("%d", &x);
while (x != 999) {
s = (DNode*) malloc(sizeof(DNode));
s->data = x;
s->prior = r;
r->next = s;
r = s;
scanf("%d", &x);
}
r->next = NULL;
return L;
}
DNode* GetElem(DNode *L, int i) {
int j = 1;
DNode *p;
p = L->next;
if (i == 0)
return L;
if (i < 1)
return NULL;
while (p != NULL && j < i) {
p = p->next;
j++;
}
return p;
}
DNode* DListInsert(DNode *L, int i, int e) {
DNode *p;
p = GetElem(L, i);
DNode *s;
s = (DNode*) malloc(sizeof(DNode));
s->data = e;
s->prior = p->prior;
s->next = p;
p->prior->next = s;
return L;
}
DNode* DListDelete(DNode *L, int i) {
DNode *p;
p = GetElem(L, i);
p->prior->next = p->next;
p->next->prior = p->prior->next;
free(p);
return L;
}
void PrintDList(DNode *L) {
DNode *p;
p = L->next;
while (p->next != NULL) {
printf("%d ", p->data);
p = p->next;
if (p->next == NULL) {
printf("%d ", p->data);
}
}
}
int main() {
DNode L;
DNode *L1 = DList_TailInsert(&L);
PrintDList(L1);
printf("\n");
DNode *L2 = DListInsert(L1, 1, 0);
PrintDList(L2);
printf("\n");
DNode *L3 = DListDelete(L2,1);
PrintDList(L3);
}