2 liuyawen44 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;

void travel_List(PNODE pHead)
{

    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 pHead=new node;

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;
}
return pHead;

}
int main()
{

PNODE pHead=NULL;
pHead=create_list();



travel_List(pHead);

}

2个回答

a1193561652
a1193561652   Rxr 2015.05.26 19:06
已采纳
 using namespace std;
typedef struct NODE
{
    int number;
    string name;
    string sex;
    int age;
    struct NODE *pNext;
}*PNODE,node;

void travel_List(PNODE pHead)
{

    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 pHead=new node;

    PNODE pTail=pHead;
    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;
    }
    return pHead;

}
int main()
{

    PNODE pHead=NULL;
    pHead=create_list();
    travel_List(pHead);

}

你试试这段代码。

a1193561652
a1193561652   Rxr 2015.05.26 00:36

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

a1193561652
a1193561652 回复liuyawen44: create_list()里的吗?是两次呀,从0到1。
2 年多之前 回复
liuyawen44
liuyawen44 不对啊,而且他为什么是三次循环赋值呢?
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片