liuyawen44 于 2015.05.25 23:54 提问

C++简单链表创建出了问题，求大神解答

#include
#include
using namespace std;
typedef struct NODE
{
int number;
string name;
string sex;
int age;
struct NODE *pNext;
}*PNODE,node;

{

``````    PNODE p=pHead->pNext;
while(NULL!=p)
{
cout<<p->number<<" ";
cout<<p->name<<" ";
cout<<p->sex<<" ";
cout<<p->age<<" ";
p=p->pNext;
}
cout<<endl;
``````

}
PNODE create_list(void)
{

``````PNODE pTail=pHead;
pTail->pNext=NULL;

for(int i=0; i<=1; i++)
{
int num;
string nm;
string sx;
int ag;
cin>>num;
getline(cin,nm);
getline(cin,sx);
cin>>ag;
PNODE pNEW=new node;
pNEW->number=num;
pNEW->name=nm;
pNEW->sex=sx;
pNEW->age=ag;
pTail->pNext=pNEW;
pNEW->pNext=NULL;
pTail=pNEW;
}
``````

}
int main()
{

``````PNODE pHead=NULL;

``````

}

2个回答

a1193561652   2015.05.26 19:06

`````` using namespace std;
typedef struct NODE
{
int number;
string name;
string sex;
int age;
struct NODE *pNext;
}*PNODE,node;

{

while(NULL!=p)
{
cout<<p->number<<" ";
cout<<p->name<<" ";
cout<<p->sex<<" ";
cout<<p->age<<" ";
p=p->pNext;
}
cout<<endl;

}
PNODE create_list(void)
{

pTail->pNext=NULL;

for(int i=0; i<=1; i++)
{
int num;
string nm;
string sx;
int ag;
cout<<"num:";
cin>>num;
cin.get();
cout<<"Name:";
getline(cin,nm);
cout<<"Sex:";
getline(cin,sx);
cout<<"Age:";
cin>>ag;
PNODE pNEW=new node;
pNEW->number=num;
pNEW->name=nm;
pNEW->sex=sx;
pNEW->age=ag;
pTail->pNext=pNEW;
pNEW->pNext=NULL;
pTail=pNEW;
}

}
int main()
{

}
``````

a1193561652   2015.05.26 00:36

cin>>num;后面加一个cin.get()。清空键盘缓冲区。

a1193561652 回复liuyawen44: create_list()里的吗？是两次呀，从0到1。
2 年多之前 回复
liuyawen44 不对啊，而且他为什么是三次循环赋值呢？