#include "stdio.h"
#include "stdlib.h"
typedef struct node
{
int data;
struct node *next;
struct node *pre;
}Node;
Node *head=NULL;
Node* create()
{
Node *p,*tail;
p=(Node*)malloc(sizeof(Node));//申请一个节点
scanf("%d",&p->data);
head=p;
head->pre=NULL;
if(p->data==0)//若节点为0;则创建失败
{
free(p);
head=NULL;
head->pre=NULL;
tail=NULL;
return head;
}
while(p->data!=0)//头结点创建成功后,接着申请节点
{
tail=p;
p=(Node*)malloc(sizeof(Node));
scanf("%d",&p->data);
if(p->data!=0)
{
p->pre=tail;
tail->next=p;
}
}
free(p);//节点为0,则尾节点的next指针为null
tail->next=NULL;
return head;
}
void insert(Node *head,int i)//插入
{
for (Node *find=head;find;find=find->next)
{
if(i--==1)
{
Node *p=(Node*)malloc(sizeof(Node));
scanf("%d",&p->data);
if(find->pre)
{
find->pre->next=p;
p->pre=find->pre;
p->next=find;
find->pre=p;
}
else
{
find->pre=p;
p->next=find;
::head=p;
}
return;
}
}
printf("超出下标\n");
}
int main()
{
Node *p=create();
Node *q=p;
while(p)
{
printf("%d\n",p->data);
p=p->next;
}
printf("------------------------------------------\n");
p=q;
insert(p,1);
printf("------------------------------------------\n");
while(head)
{
printf("%d\n",head->data);
head=head->next;
}
return 0;
}