#include<stdio.h>
typedef struct DuLNode{
int data;
struct DuLNode *prior;
struct DuLNode *next;
}DuLNode,*DuLinkList;
// it return a pointer of the ith element in the double linked list
DuLinkList GetElemP_DuL(DuLinkList L,int i){
DuLinkList p;int j;
p=L;
for(j=1;j<=i;j++){
p=p->next;
}
return p;
}
int main(){
//first,we construct a double linked list, it value is 1 2 3
DuLinkList L,A,B,C,p;
L=(DuLinkList)malloc(sizeof(DuLNode));
A=(DuLinkList)malloc(sizeof(DuLNode));
B=(DuLinkList)malloc(sizeof(DuLNode));
C=(DuLinkList)malloc(sizeof(DuLNode));
L->next=A;L->prior=C; A->data=1;A->prior=L;A->next=B; B->data=2;B->prior=A;B->next=C; C->data=3;C->prior=B;C->next=L;
p=L->next;//p point to A
ListInsert_DuL(L,2,5);
int e;
ListDelete_DuL(L,3,e);
while(p && p!=L){
printf("%d\n",p->data);
p=p->next;
}
return 1;
}
//insert one number before ith number
int ListInsert_DuL(DuLinkList L,int i, int e){
DuLinkList s;
DuLinkList p;
p=GetElemP_DuL(L,i);
if(!(s=(DuLinkList)malloc(sizeof(DuLNode)))) return 0;
s->data=e;
s->prior=p->prior;p->prior->next=s;
s->next=p;p->prior=s;
return 1;
}
//delete the i-th number in the double linked list
int ListDelete_DuL(DuLinkList L,int i,int e){
DuLinkList p;
if(!(p=GetElemP_DuL(L,i))) return 0;
e=p->data;
p->prior->next=p->next;
p->next->prior=p->prior;
free(p);
return 1;
}
typedef struct DuLNode{
int data;
struct DuLNode *prior;
struct DuLNode *next;
}DuLNode,*DuLinkList;
// it return a pointer of the ith element in the double linked list
DuLinkList GetElemP_DuL(DuLinkList L,int i){
DuLinkList p;int j;
p=L;
for(j=1;j<=i;j++){
p=p->next;
}
return p;
}
int main(){
//first,we construct a double linked list, it value is 1 2 3
DuLinkList L,A,B,C,p;
L=(DuLinkList)malloc(sizeof(DuLNode));
A=(DuLinkList)malloc(sizeof(DuLNode));
B=(DuLinkList)malloc(sizeof(DuLNode));
C=(DuLinkList)malloc(sizeof(DuLNode));
L->next=A;L->prior=C; A->data=1;A->prior=L;A->next=B; B->data=2;B->prior=A;B->next=C; C->data=3;C->prior=B;C->next=L;
p=L->next;//p point to A
ListInsert_DuL(L,2,5);
int e;
ListDelete_DuL(L,3,e);
while(p && p!=L){
printf("%d\n",p->data);
p=p->next;
}
return 1;
}
//insert one number before ith number
int ListInsert_DuL(DuLinkList L,int i, int e){
DuLinkList s;
DuLinkList p;
p=GetElemP_DuL(L,i);
if(!(s=(DuLinkList)malloc(sizeof(DuLNode)))) return 0;
s->data=e;
s->prior=p->prior;p->prior->next=s;
s->next=p;p->prior=s;
return 1;
}
//delete the i-th number in the double linked list
int ListDelete_DuL(DuLinkList L,int i,int e){
DuLinkList p;
if(!(p=GetElemP_DuL(L,i))) return 0;
e=p->data;
p->prior->next=p->next;
p->next->prior=p->prior;
free(p);
return 1;
}