程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> new-C++簡單鏈表創建出了問題,求大神解答

new-C++簡單鏈表創建出了問題,求大神解答

編輯:編程綜合問答
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);

}

最佳回答:


 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);

}

你試試這段代碼。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved