【问题标题】:Why my linked list is printing address instead of printing values?为什么我的链表是打印地址而不是打印值?
【发布时间】:2021-05-18 05:14:39
【问题描述】:

这是合并两个排序链表(按排序顺序)的代码。

我的代码是打印地址而不是打印排序列表。 我无法理解问题出在哪里。

这是我的代码:

#include <iostream>
using namespace std;

class node
{
public:
    int data;
    node *next;

    node(int d)
    {
        data = d;
        next = NULL;
    }
};

node *Merge(node *head1, node *head2)
{
    if (head1 == NULL)
    {
        return head2;
    }

    if (head2 == NULL)
    {
        return head1;
    }

    node *c = NULL;
    if (head1->data < head2->data)
    {
        c = head1;
        c->next = Merge(head1->next, head2);
    }
    else
    {
        c = head2;
        c->next = Merge(head1, head2->next);
    }
    return c;
}

void InsertAtTail(node *&head, int data)
{
    if (head == NULL)
    {
        head = new node(data);
        return;
    }

    node *tail = head;
    while (tail->next != NULL)
    {
        tail = tail->next;
    }
    tail->next = new node(data);
    return;
}

void display(node *head)
{
    if (head == NULL)
    {
        cout << "The Linked List is empty !!";
        return;
    }

    while (head != NULL)
    {
        cout << head->data << " -> ";
        head = head->next;
    }
    cout << endl;
}

int main()
{
    node *head1 = NULL;
    node *head2 = NULL;
    int n1, n2, data;
    cout << "Creating 1st Linked List: " << endl;
    cout << "Enter the number of nodes you want to enter: ";
    cin >> n1;
    for (int i = 0; i < n1; i++)
    {
        cout << "Enter the data: ";
        cin >> data;
        InsertAtTail(head1, data);
    }

    cout << "Creating 2nd Linked List: " << endl;
    cout << "Enter the number of nodes you want to enter: ";
    cin >> n2;
    for (int i = 0; i < n2; i++)
    {
        cout << "Enter the data: ";
        cin >> data;
        InsertAtTail(head2, data);
    }

    display(head1);
    display(head2);

    node *NewNode = NULL;
    NewNode = Merge(head1, head2);
    cout << "The sorted list is: " << endl;
    cout << NewNode << endl;
    return 0;
}

这是我的输出截图: https://i.stack.imgur.com/dUv7A.png

所有其他功能都正常工作。但是在调用 Merge 函数之后,它会打印地址(十六进制)值,而不是打印新的排序链表。

【问题讨论】:

  • 因为 NewNode 是指针。你可能想要 display(NewNode);?
  • 因为这是你告诉它打印的,cout &lt;&lt; NewNode。如果你想要内容,你必须做更多,例如cout &lt;&lt; NewNode-&gt;data,或为node 提供operator&lt;&lt; 重载并执行cout &lt;&lt; *NewNode
  • 警惕递归的Merge() 函数,根据操作系统,在列表达到 100,000 个节点之前,您将溢出堆栈。

标签: c++ data-structures linked-list


【解决方案1】:

看来您只需要将cout &lt;&lt; NewNode &lt;&lt; endl; 替换为:display(NewNode);

【讨论】:

  • 但是我可以知道为什么 cout 不起作用吗?
  • @TanyaGupta 它正在工作。它正在做你告诉它做的事情:显示新节点的地址..
【解决方案2】:

我刚刚将cout &lt;&lt; NewNode &lt;&lt; endl; 替换为cout &lt;&lt; display(NewNode) &lt;&lt; endl;

而且,它正在工作。

代码很好。 注意愚蠢的错误。一切顺利。

【讨论】:

    【解决方案3】:

    代码的倒数第二行cout &lt;&lt; NewNode &lt;&lt; endl; 将打印地址,因为NewNode 是一个指针,而不是这个调用display(NewNode) 来打印所有节点。

    【讨论】:

      猜你喜欢
      • 2016-02-26
      • 1970-01-01
      • 2016-10-17
      • 1970-01-01
      • 2014-05-17
      • 1970-01-01
      • 2012-02-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多