协助单链接列表(C ++)程序

时间:2014-06-29 15:31:24

标签: c++ list linked-list

我开始在C ++中学习链接列表,并且我已经编写了一个程序来创建和显示列表。编译很好,但执行失败。

以下是代码:

#include <iostream>
using namespace std;

class node
{
    public:
    int data;                       // information contained by the node
    node *next;                     // a pointer pointing to an instance of the class node.
};

class list
{
public:
    node *head;
    node *temp, *tp;
    void createlist();
    void displaylist();
};

void list :: createlist()
{
    int size;
    cout<<"Enter the size of the list you want to create.\n";
    cin>>size;
    temp=head;
    for(int k=1;k<=size;k++)
    {
    if(head == NULL)
    {
        head = new node[1];
        cout<<"Enter the data for node number 1\n";
        cin>>head->data;
        head->next=NULL;
    }
    else
    {

            temp->next=new node[1];
            temp=temp->next;
            cout<<"Enter the data for node number \n"<<k;
            cin>>temp->data;
            temp->next=NULL;

    }
    }
}


void list::displaylist()
{
    if (head==NULL)
    {
        cout<<"List is empty.";
    }
    else
    {
        while(tp!=NULL)
        {
        tp = head;
        cout<<tp->data;
        tp = tp->next;
        }
    }
}

int main()
{
        list obj;
    cout<<"Creating list...\n";
    obj.createlist();
    cout<<"Displaying list...\n";
    obj.displaylist();
    return 0;
}


#include <iostream>
using namespace std;

class node
{
    public:
    int data;                       // information contained by the node
    node *next;                     // a pointer pointing to an instance of the class node.
};

class list
{
public:
    node *head;
    node *temp, *tp;
    void createlist();
    void displaylist();
};

void list::createlist()
{
    int size;
    cout<<"Enter the size of the list you want to create.\n";
    cin>>size;
    temp=head;
    for(int k=1;k<=size;k++)
    {
    if(head == NULL)
    {
        head = new node[1];
        cout<<"Enter the data for node number 1\n";
        cin>>head->data;
        head->next=NULL;
    }
    else
    {

            temp->next=new node[1];
            temp=temp->next;
            cout<<"Enter the data for node number \n"<<k;
            cin>>temp->data;
            temp->next=NULL;

    }
    }
}


void list::displaylist()
{
    if (head==NULL)
    {
        cout<<"List is empty.";
    }
    else
    {
        while(tp!=NULL)
        {
        tp = head;
        cout<<tp->data;
        tp = tp->next;
        }
    }
}

int main()
{
        list obj;
    cout<<"Creating list...\n";
    obj.createlist();
    cout<<"Displaying list...\n";
    obj.displaylist();
    return 0;
}

以下是输出:
Creating list... Enter the size of the list you want to create. 3 Enter the data for node number 1 1

因此,第二个节点的数据不被接受。我是链接列表的新手,想尝试以这种方式解决问题,而且我花了太多时间在这上面。我哪里出错了?

1 个答案:

答案 0 :(得分:1)

创建对象obj时,数据成员head和tp未初始化,并且具有任意值。所以程序有不确定的行为。将临时变量作为类的tp或临时数据成员也是一个坏主意。它们必须是使用它们的成员函数的局部变量。

没有任何意义可以像你一样分配节点数组。

head = new node[1];
/...
temp->next=new node[1];

简单地写

head = new node();
/...
temp->next=new node();

最好将类节点定义为类列表的内部类。

并且不要忘记编写用于释放已分配内存的析构函数。