#include <stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct DNode
{
int data;
struct DNode *prior;
struct DNode *next;
}DNode,*DLinkedList;
bool InitList(DLinkedList *L)
{
(*L)=(DNode*)malloc(sizeof(DNode));
if((*L)==NULL)
return false;
(*L)->next=NULL;
(*L)->prior=NULL;
return true;
}
bool ListInsert(DLinkedList *L,int i ,int e)
{
if(i<1)
return false;
int j=0;
DNode *p = *L, *s;
while(p!=NULL&&j<i-1)
{
p=p->next;
j++;
}
if(p==NULL)
return false;
s=(DNode*)malloc(sizeof(DNode));
s->data=e;
s->next=p->next;
if(p->next!=NULL)
p->next->prior=s;
s->prior=p;
p->next=s;
return true;
}
bool InsertNextNode(DNode* p,int e)
{
if(p==NULL)
return false;
DNode *s=(DNode*)malloc(sizeof(DNode));
s->data=e;
s->next=p->next;
s->prior=p;
if(p->next!=NULL)
p->next->prior=s;
p->next=s;
return true;
}
bool ListDelete(DNode *p)
{
if(p==NULL)
return false;
if(p->next==NULL)
return false;
DNode* s = p->next;
if(s->next!=NULL)
s->next->prior=p;
p->next=s->next;
free(s);
return true;
}
bool DestoryList(DLinkedList *L)
{
while((*L)->next!=NULL)
ListDelete(*L);
free(*L);
*L=NULL;
return true;
}
void PrintList(DLinkedList L)
{
if(L->next==NULL)
printf("链表已空");
DNode *p=L->next;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
DNode* GetNode(DLinkedList L,int i)
{
if(i<1)
return false;
int j=0;
DNode *p=L;
while(p!=NULL&&j<i)
{
p=p->next;
j++;
}
return p;
}
int main ()
{
DLinkedList L;
InitList(&L);
ListInsert(&L,1,1);
InsertNextNode(L,10);
ListInsert(&L,2,2);
ListInsert(&L,3,3);
PrintList(L);
printf("%d \n",GetNode(L,3)->data);
ListDelete(GetNode(L,3));
PrintList(L);
DestoryList(&L);
PrintList(L);
}