怎么改进这段C语言程序

如何改进这段C语言程序?
我用Microsoft   Visual   C++   6.0调试,虽然没有错误,但运行中会产生非法操作,请问如何修改这段程序?
#include <stdio.h>
#include <malloc.h>
struct   snode{
int   data;
struct   snode   *next,*pre;
};
struct   snode   *head,*tail;

init_line()
{
head=NULL;
}

creat_line()
{
int   e;
struct   snode   *p;
p=(struct   snode   *)malloc(sizeof(struct   snode));
printf( "Input   number: ");
scanf( "%d ",&e);
while(e)
{
p-> data=e;
if(head==NULL)
head=p;
else
{
tail-> next=p;
p-> pre=tail;
}
tail=p;
tail-> next=NULL;
p=(struct   snode   *)malloc(sizeof(struct   snode));
printf( "Input   number: ");
scanf( "%d ",&e);
}
}
print()
{
struct   snode   *p,*t;
p=tail;
while(p)
{
printf( "%d ",p-> data);
t=p;
p=p-> pre;
free(t);
p-> next=NULL;
}
}

main()
{
init_line();
creat_line();
print();
}

------解决方案--------------------
#include <stdio.h>
#include <malloc.h>

struct snode
{
int data;
struct snode *next,*pre;
};

struct snode *head,*tail;

void init_line() //说实话一点用没有, 全局变量编译器会自动初始化
{
head=NULL;
tail=NULL;
}

void creat_line()
{
int e;
struct snode *p;
p=(struct snode *)malloc(sizeof(struct snode));
printf( "Input number: ");
scanf( "%d ",&e);
while(e)
{
p-> data=e;
if(head==NULL)
{
head=p;
head-> pre = NULL; //此处应该这样
}
else
{
tail-> next=p;
p-> pre=tail;
}
tail=p;
tail-> next=NULL;
p=(struct snode *)malloc(sizeof(struct snode));
printf( "Input number: ");
scanf( "%d ",&e);
}
}
void print()
{
struct snode *p,*t;
p=tail;
while(p)
{
printf( "%d ",p-> data);
t=p;
p=p-> pre;
free(t);
if(p)
p-> next=NULL; //当p为NULL,不再执行该语句
}
}

int main()
{
init_line();
creat_line();
print();
return 0;
}