简体   繁体   English

如何将头部指针传递给 main.cpp 中的函数

[英]How to pass the pointer of a head to a function in main.cpp

When I try to call the DisplayLinkedList function in main, it prints out nothing.当我尝试在 main 中调用 DisplayLinkedList 函数时,它什么也没打印出来。 How do i correctly pass the head into the DisplayLinkedList function so that it properly prints out the entire linked list?我如何正确地将头部传递给 DisplayLinkedList 函数,以便它正确打印出整个链表?

void LinkedList::AppendInTheEnd(int numberToAdd)
{
     NodePtr newNode = new Node;
     newNode->nextNode = NULL;
     newNode->data = numberToAdd;

     if(headNode != NULL)
     {
        currentNode = headNode;
        while(currentNode->nextNode != NULL)
        {
            currentNode = currentNode->nextNode;
        }
        currentNode->nextNode = newNode;
     }
     else
     {
         headNode = newNode;
     }
}
void LinkedList::DisplayLinkedList(Node* head)
{

         Node* p;
         p = head;
         cout << "Displaying the list" << p << endl;
         while(p != NULL)
         {
              cout << "Node at " << p << endl;
              cout << " value " << p->data << endl;
              cout << " next: " << p->data << endl;
           }
            p = p->nextNode;
}
int main()
{
   LinkedList::Node* head = NULL;


  LinkedList list;

  list.AppendInTheEnd(9);
  list.AppendInTheEnd(10);

  list.DisplayLinkedList(head);
}

I think you have a design issue here (in addition to what Jabberwocky pointed out).我认为您在这里有一个设计问题(除了 Jabberwocky 指出的问题)。 LinkedList::DisplayLinkedList is a member function and the name implies, that it prints the contents of the list. LinkedList::DisplayLinkedList是一个成员函数,顾名思义,它打印列表的内容。 Eg例如

list.DisplayLinkedList();

would imply, that it prints the content of list .将暗示,它打印list的内容。

Then why does it take any parameter?那它为什么要带任何参数呢? It's a member function, which means it's called on an instance of LinkedList and you can access the members of this instance using this-> .它是一个成员函数,这意味着它是在LinkedList的实例上调用的,您可以使用this->访问该实例的成员。 Passing some parameter to print any other list than the current instance just makes not much sense.传递一些参数来打印当前实例以外的任何其他列表并没有多大意义。 I suggest the following approach:我建议采用以下方法:

void LinkedList::DisplayLinkedList()
{
     Node *p = this->headNode;
     cout << "Displaying the list" << p << endl;
     while(p != NULL)
     {
         cout << "Node at " << p << "\n"; // prints the address
         cout << " value " << p->data << "\n";
         cout << " next: " << p->data << "\n"; // p->nextNode maybe?

         p = p->nextNode;
     }
}

For good practice don't spam endl ;)为了良好的实践,不要发送垃圾邮件endl ;)

There is at least a problem here:这里至少有一个问题:

     while(p != NULL)
     {
          cout << "Node at " << p << endl;
          cout << " value " << p->data << endl;
          cout << " next: " << p->data << endl;
          p = p->nextNode;   // <------------------+
     }                                             |
                                                   |
     // p = p->nextNode;   // this should be here -|

The p = p->nextNode must be inside the loop. p = p->nextNode必须在循环内。

The other problem I can see is that head is actually never updated.我可以看到的另一个问题是head实际上从未更新过。 It is initialized once to NULL .它被初始化为NULL一次。 So when you call DisplayLinkedList(head) , head is NULL and obviously nothing will be printed.因此,当您调用DisplayLinkedList(head) , head 为 NULL 并且显然不会打印任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM