C语言双向链表插入不进去?是什么情况??
#include "stdio.h"#include "malloc.h"
typedef struct DNode
{
int data;
struct DNode *prior,*next;
}DNode,*LinkList;
/*建立有n个结点的双链表*/
LinkList create_link(LinkList head,int n)
{
LinkList p, q;
int i,x;
head=(LinkList)malloc(sizeof(DNode));
head->prior=NULL;
head->next=NULL;
p=head;
for(i=1;i<=n;i++)
{
q=(LinkList)malloc(sizeof(DNode));
printf("请输入结点值:");
fflush(stdin); /*清除键盘缓冲区*/
scanf("%d",&x);
q->data=x;
p->next=q;
q->prior=p;
q->next=NULL;
p=q;
}
return(head);
}
/*双链表各个结点值的输出:*/
void ListPint_L(LinkList head)
{
LinkList p;
p=head->next;
while(p!=NULL)
{
printf("%3d\n",p->data);
p=p->next;
}
}
/*在双链表中插入一个数据*/
int insert_link(LinkList head,int e,int i)
{
LinkList p,s;
int j;
head=(LinkList)malloc(sizeof(DNode));
head->prior=NULL;
head->next=NULL;
p=head->next;
while(j<i&&p->next!=NULL)
{
j++;
p=p->next;
}
if(j>i)
return 0;
s=(LinkList)malloc(sizeof(DNode));
s->data=e;
s->next=p;
s->prior=p->prior;
p->prior->next=s;
p->prior=s;
return 1;
}
void main( )
{
int i,e;
LinkList head;
head=(LinkList)malloc(sizeof(DNode));
head->prior=NULL;
head->next=NULL;
head=create_link(head,5);
printf("输出结果是:\n");
ListPint_L(head);
printf("请输入你要插入的数据:");
fflush(stdin);
scanf("%d",&e);
printf("请输入你要插入的位置:");
scanf("%d",&i);
insert_link(head,e,i);
ListPint_L(head);
}
(运行结果)