下面代码运行时第一个员工的信息不能输出,求解答!
#include<iostream>
#include<string.h>
using namespace std;
typedef struct EmpNode
{
string category;
string name;
string sex;
int age;
int phone_number;
struct EmpNode *next;
}Employee, *EmpPtr;
class EmpListLink
{
private:
EmpPtr head;
EmpPtr tail;
int count;
public:
EmpListLink(){};
void CreateEmpList();
void PrintEmpList();
};
void EmpListLink::CreateEmpList()
{
EmpPtr p;
cout<<"请输入员工的总数"<<endl;
cin>>count;
if(count<0)
{
cout<<"错误,总数应不小于0."<<endl;
return ;
}
head=new Employee;
head->age=0;
head->category="0";
head->name="0";
head->phone_number=0;
head->sex="0";
head->next=NULL;
tail=head;
for(int i=0;i<count;i++)
{
p=new Employee;
cout<<"请输入员工的姓名:";
cin>>p->name;
cout<<"请输入员工的性别:";
cin>>p->sex;
cout<<"请输入员工的年龄:";
cin>>p->age;
cout<<"请输入员工的电话号码:";
cin>>p->phone_number;
cout<<"请输入员工的类别:";
cin>>p->category;
p->next=NULL;
tail->next=p;
tail=p;
}
EmpPtr temp;
temp=head->next;
if(temp==NULL)
{
cout<<"学生链表为空!"<<endl;
return ;
}
while(temp!=NULL)
{
cout<<p->name<<" "<<p->sex<<" "<<p->age<<" "<<p->phone_number<<" "<<p->category<<endl;
temp=temp->next;
}
}
int main()
{
EmpListLink emplistlink;
emplistlink. CreateEmpList();
return 0;
}
temp=head->next;
你直接从第二个节点数出了啊
while(temp!=NULL)
{
cout<<p->name<<" "<<p->sex<<" "<<p->age<<" "<<p->phone_number<<" "<<p->category<<endl;
temp=temp->next;
}
你用temp循环,为啥cout输出的是p的值呢?把p改成temp啊