#include<iostream>
#include<malloc.h>
using namespace std;
typedef int ElemType;
typedef struct DNode
{
ElemType data;
struct DNode *prior;
struct DNode *next;
}Dlinknode;
void CreateList(Dlinknode *&L,ElemType a[],int n)
{
Dlinknode *s;
s=(Dlinknode *)malloc(sizeof(Dlinknode));
L->prior=L->next=NULL;
for(int i=0;i<n;i++)
{
s->data=a[i];
s->next=L->next;
if(L->next!=NULL)
L->next->prior=s;
s->prior=L;
L->next=s;
}
}
bool ListInsert(Dlinknode *&L,int i,ElemType e)
{
int j=0;
Dlinknode *p=L,*s;
if(i<=0)
return false;
while(j<i-1&&p!=NULL)
{
j++;
p=p->next;
}
if(p==NULL)
return false;
else{
s=(Dlinknode *)malloc(sizeof(Dlinknode));
s->data=e;
s->next=p->next;
if(p->next!=NULL)
p->next->prior=s;
s->prior=p;
p->next=s;
return true;
}
}
bool ListDelete(Dlinknode *&L,int i,ElemType &e)
{
int j=0;
Dlinknode *p=L,*q;
if(i<0)
return false;
while(j<i-1&&p!=NULL)
{
j++;
p=p->next;
}
if(p==NULL)
return false;
else{
q=p->next;
if(q==NULL)
return false;
e=q->data;
if(q->next!=NULL)
q->next->prior=p;
p->next=q->next;
free(q);
return true;
}
}