如何打印链接列表的数据部分

时间:2014-05-17 02:05:15

标签: c++ visual-c++

基本上我想打印Linked列表的数据部分,它基本上是一个Integer指针,我在创建时为它分配一个数组,我想打印它的所有值怎么做? 谢谢。 这是我的代码     使用namespace std;

struct Node{
    Node *next;
    int *data;
};
class DataLine{
    private:
      Node *first;
    public:
      DataLine(){
          first=NULL;
  }
    void create_list(){
        Node *temp=new Node;
        int i=2;
        int dat[5]={12,13,14,13,16};
        temp->data=dat;
        temp->next=NULL;
        if(first==NULL){
            //cout<<"hello 1"<<endl;
            first=temp;

        }
        else{
            Node *curr=first;    //We are now doing trevercing so we are assigning the first to the node because we donot want to move the first bacuse link break if we move the first
            while(curr->next!=NULL) //searching end of list
            {   
                curr=curr->next; //Moving to the next node
            }
            curr->next=temp; //insert node

            temp=NULL;        //Making the temp enpty to use it for the new purpose
            //delete temp;
        }
    }
    void print_list()
        {
            Node *prnt=first;  //We are now again want trevercing so we agin run the node by the new node
            while(prnt!=NULL)  //Checking the loop will run till it get the null in the node means its address part and data part both are nUll
            {
                for(int i=0;i<5;i++)
                cout<<"         ***** The "<<" node is "<<*(prnt->data+i)<<endl; //Printing the data
                prnt=prnt->next;           //Moving to the next node
            }

        }
};
int main(){
DataLine dl;
dl.create_list();
    dl.print_list();
_getch();
return 0;
}   

1 个答案:

答案 0 :(得分:0)

你的void print_list(void)的想法是正确的,但你可以使它更清洁,但请注意我改变你的输出每行打印一个节点(如果你想要改变它)。对我来说,for循环的结构对于链表来说是完美的,并且保持链表代码是循环体的代码。

void print_list(void) const
{
    for (Node* p = first; p != NULL; p = p->next)
    {
        for (int i = 0; i < Node::unLength; ++i) std::cout << p->data[i] << ", ";
        std::cout << std::endl;
    }
}

但是,正如一些评论中所指出的,您的创建列表代码中还存在其他问题。我建议修复这些(对于这个程序)的方法是重新定义你的结构,以便始终保持固定数量的整数。

struct Node
{
    enum { unLength = 5 };
    Node* next;
    int data[unLength];
};

我还在这里添加了一个数组长度的常量,因为在你的代码中包含幻数的错误做法,如果你错误输入其中一个,会发生什么?

现在在void create_list()你可以去:

void create_list()
{
    Node* temp = new Node;

    // Set the next node of temp
    temp->next = NULL;

    // Add some data to temp (we can't just assign the data pointer in C/C++)
    int data[Node::unLength] = {0, 1, 2, 3, 4};
    for (int i = 0; i < Node::unLength; ++i) temp->data[i] = data[i];

    Node *p = first;
    while (p != NULL) p = p->next;
    p->next = temp;
}

没有必要将temp设置为NULL,因为在函数返回后会立即删除temp。在之前的代码中,您将指针设置为Nodedatatemp->data=dat;,这不起作用,因为只要函数返回就删除dat,您需要改为分配内存并复制dat中的值,这是上面代码中的for循环。

对于你的类构造函数(和析构函数),我建议:

class DataLine
{
private:
    Node* first;

public:
    DataLine(void) : first(NULL) {}
    ~DataLine(void)
    {
        while (first != NULL)
        {
            Node *temp = first->next;
            delete first;
            first = temp;
        }
    }

你有正确的想法,但有一些关于C / C ++的微妙内容在高级语言中并不明显,例如复制数组和变量范围。

如果您正在使用C ++,我建议不要担心链接列表,只需创建一个std::vector,在C ++ 11中,类似下面的内容可能会起作用(未经测试):

#include <vector>
#include <array>

int main(int argc, char** argv)
{
    std::vector< std::array<int, 5> > myData;
    myData.push_back({0, 1, 2, 3, 4});
    myData.push_back({0, 1, 2, 3, 4});
    myData.push_back({0, 1, 2, 3, 4});

    for (const auto& i : myData)
    {
        for (int j : i) std::cout << j << ", ";
        std::cout << std::endl;
    }

    return 0;
}