为什么输出一个空指针链表时if(pHead==NULL){cout《“”;}这句不能执行

#include
using namespace std;

typedef struct Node
{
int age;
struct Node* next;
}LIST;

LIST* CreatList()
{
int data=0;
LIST* Phead=NULL;
LIST* Pm=NULL;
LIST* Pcur=NULL;
Phead=(LIST*)malloc(sizeof(LIST));
Phead->age=0;
Phead->next=NULL;

cout<<"Enter your data of node (-1 quit):";
scanf("%d", &data);
Pcur=Phead;
while (data!=-1)
{

    Pm=(LIST*)malloc(sizeof(LIST));
    Pm->age=data;
    Pm->next=NULL;
    Pcur->next=Pm;
    Pcur=Pcur->next;
    cout<<"Enter your data of node (-1 quit):";
    scanf("%d", &data);
}
return Phead;

}

void ListOut(LIST* Phead)
{
LIST* p=NULL;

if(Phead==NULL)   //如果改为if(Phead->next==NULL)则cout执行,why?
{
    cout<<"List is NULL\n";
}
else
{
    p=Phead->next;
    while(p!=NULL)
    {
        cout<<p->age<<endl;
        p=p->next;
    }
}

}

void main()
{
LIST* p1=CreatList();
ListOut(p1);
system("pause");
}

在创建链表的时候,看你怎么输入,只要输入了非-1的数,则head不为空,如果只输入了一次-1,则head是空的。

CreatList()里面无论是否输入数据都会为phead分配内存,实际数据是从phead的下一个节点开始存储的,phead中无数据只作为一个链表头用。

图片说明

当输入-1后,head指针存储的是节点的地址,当然不是空的,但是head->next是NULL,