为什么我的链表打印地址而不是值
Why my linked list is printing address instead of printing values?
这是用于合并两个排序链表(按排序顺序(的代码。
我的代码是打印地址,而不是打印排序列表。我不知道问题出在哪里。
这是我的代码:
#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函数后,它将打印地址(heaxadecimal(值,而不是打印新排序的链表。
似乎只需要将cout << NewNode << endl;
替换为:display(NewNode);
我刚刚将cout << NewNode << endl;
替换为cout << display(NewNode) << endl;
而且,它正在发挥作用。
代码很好。注意那些愚蠢的错误。一切顺利。
代码cout << NewNode << endl;
的倒数第二行将打印地址,因为NewNode是一个指针,而不是用于打印所有节点的调用display(NewNode)
。
相关文章:
- 如何使用 Boost Asio 在 Android 上获取我的本地 udp IP 地址?
- 为什么我得到以下代码地址清理器:未知地址错误的SEGV
- 为什么我的迭代器和指针不提供相同的地址
- 使用Boost库中的PC中获取我的以太网设备IP地址的列表
- C 在我的计算机上工作正常,但在Leetcode上获得地址消毒器堆越流行错误
- 如何让我的程序手动找到地址
- 如何禁用调试控制台在我的代码中显示文件地址
- getPeername()在客户端在虚拟机上运行时返回我的本地主机地址
- 如何阻止我的数组打印地址
- 为什么我的函数打印地址而不是数组的内容
- 如何获取我的计算机外部IP地址
- 为什么我的复制构造函数不适用于新调用并且具有相同的内存地址
- 为什么我不能为我的 QTcpServer 设置特定地址?
- GDB:显示函数将我的 {type} 的地址打印为整数——想改成十六进制
- C ++如何将我的命令行参数(MAC地址)转换为无符号字符
- 我的代码打印出地址而不是数组值
- 如何找出WSD打印机的IP地址?我想使用 WSD API,但无法使用 IWSDDiscoveredDevice
- 我的c++程序正在打印一个地址而不是一个值
- 获取我的IP地址并使用c++将其转换为字节
- 我的对象在矢量中的地址改变了