#include<stdio.h>
#include<stdlib.h>
typedef struct DNode{
int data;
struct DNode *prior,*next;//定义前指针与后继指针
}DNode,*DLinkList;
//初始化
void Init(DLinkList &L)
{
L=(DNode *)malloc(sizeof(DNode));
L->prior=NULL;
L->next=NULL;
}
//双链表的插入
bool Insert(DNode *p,DNode *s)
{
if(p==NULL||s==NULL)
return false;
s->next=p->next;
if(p->next!=NULL)
p->next->prior=s;
s->prior=p;
p->next=s;
return true;
}
//双链表的删除
bool Delete(DNode *p)
{
if(p==NULL)
return false;
DNode *s=p->next;
if(s==NULL)
return false;
p->next=s->next;
if(s->next!=NULL)
s->next->prior=p;
free(s);
return true;
}
//销毁双链表
void Destory(DLinkList &L)
{
while(L->next!=NULL)
{
Delete(L);
}
free(L);
L=NULL;
}
//头插法
DLinkList Head(DLinkList &L)
{
int x;
DNode *s;
scanf("%d",&x);
while(x!=9999)
{
s=(DNode *)malloc(sizeof(DNode));
s->data=x;
if(L->next==NULL)
{
s->next=NULL;
s->prior=L;
L->next=s;
}
else
{
s->next=L->next;
L->next->prior=s;
s->prior=L;
L->next=s;
}
scanf("%d",&x);
}
return L;
}
//尾插法
DLinkList Tail(DLinkList &L)
{
int x;
DNode *r,*s;
r=L;
scanf("%d",&x);
while(x!=9999)
{
s=(DNode *)malloc(sizeof(DNode));
s->data=x;
s->next=NULL;
s->prior=r;
r->next=s;
r=s;
scanf("%d",&x);
}
r->next=NULL;
return L;
}
//打印
void print(DLinkList L)
{
DNode *p;
p=L->next;
while(p!=NULL)
{
printf("%d\n",p->data);
p=p->next;
}
}
int main()
{
DLinkList L;
Init(L);
//Tail(L);
Head(L);
print(L);
return 0;
}