为什么输出一个空指针链表时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");
}

查看全部
qq_28107929
不死奇迹
2015/09/04 10:07
  • 链表
  • 指针
  • 点赞
  • 收藏
  • 回答
    私信

4个回复