链表是否为空的问题,求帮助!!
怎么节点无论输入是几时,总打印出 此链表为空....
希望大家帮忙看一下。。
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct node
{
int date;
struct node * pnext;
}NODE,*PNODE;
PNODE creat_list (void);
void traverse_list (PNODE);
bool empty (PNODE);
int main (void)
{
PNODE phead = NULL;
phead = creat_list ();
if (empty (phead))
{
printf("此链表为空!\n");
}
else
{
printf("此链表不为空!\n");
}
traverse_list (phead);
return 0;
}
PNODE creat_list (void)
{
int i,val,len;
PNODE phead = (PNODE)malloc(sizeof(NODE));
if(NULL == phead)
{
printf("分配失败!!\n");
exit(-1);
}
PNODE ptail = phead;
ptail->pnext = NULL;
printf("请输入节点的个数:len = ");
scanf("%d",&len);
for (i=0; i<len; i++)
{
printf("请输入第%d个节点的个数:",i+1);
scanf("%d",&val);
PNODE pnew = (PNODE)malloc(sizeof(NODE));
if (NULL == pnew)
{
printf("分配失败!!");
exit(-1);
}
pnew->date = val;
ptail->pnext = pnew;
pnew->pnext = NULL;
ptail = pnew;
}
return phead;
}
void traverse_list (PNODE phead)
{
PNODE p = phead->pnext;
while (p != NULL)
{
printf("%3d",p->date);
p = p->pnext;
}
printf("\n");
return ;
}
bool empty (PNODE phead)
{
if ( NULL== phead)
{
return true;
}
else
return false;
}