对不起。我是 C++ 新手。我尝试将第三个节点添加为头节点并为数据初始化 5,但似乎它破坏了之前的 1->2 链接列表。
因此电流输出仅在输出端
5
我的预期输出是
5
1
2
我尝试过什么。
#include <iostream>
struct node {
int data;
node *next;
};
int main(int argc, const char * argv[])
{
node* n;
node * head;
node * tmp;
//create head node.
n = new node;
n->data=1;
tmp = n;
head = n;
//create a new node after head node and link it with head node
n = new node;
n->data=2;
tmp->next=n;
tmp=tmp->next;
//inserting before head node
n = new node;
head = n;
n->data=5;
n->next = head;
tmp = head;
//end of linked list
n->next=NULL;
//print
while ( head != NULL ) {
std::cout<< head->data << std::endl;
head = head->next;
}
return 0;
}
以下代码将解决您的问题,但这不是编写列表的好方法。
#include <iostream>
struct node {
int data;
node *next;
};
int main(int argc, const char * argv[])
{
node* n;
node * head;
node * tmp;
//create head node.
n = new node;
n->data=1;
tmp = n;
head = n;
//create a new node after head node and link it with head node
n = new node;
n->data=2;
tmp->next=n;
tmp=tmp->next;
//inserting before head node(the following i have changed!)
n = new node;
n->data=5;
n->next = head;
head = n;
//end of linked list
tmp=NULL;
//print
while ( head != NULL ) {
std::cout<< head->data << std::endl;
head = head->next;
}
return 0;
}
此行将第一个节点next
的设为,而不是最后一个:NULL
//end of linked list
n->next=NULL;
此外,您还可以将next
of分配n
给自身:
head = n;
n->next = head;
您应该在重新分配之前next
设置。n
head
如果要设置最后一个节点的next
,请使用类似以下内容的内容:
n->next->next->next = NULL;
但是,您最好使用构造函数来初始化数据并编写成员函数来操作数据,而不是手动执行。
您的代码没有创建链接列表。您在头部插入,在将现有列表链接到新节点之前覆盖头指针,从而破坏整个列表。
尝试一下:
struct node {
int x;
node *next;
};
int main()
{
node *root; // This won't change, or we would lose the list in memory
node *conductor; // This will point to each node as it traverses the list
root = new node; // Sets it to actually point to something
root->next = 0; // Otherwise it would not work well
root->x = 12;
conductor = root; // The conductor points to the first node
if ( conductor != 0 ) {
while ( conductor->next != 0)
conductor = conductor->next;
}
conductor->next = new node; // Creates a node at the end of the list
conductor = conductor->next; // Points to that node
conductor->next = 0; // Prevents it from going any further
conductor->x = 42;
}
这段代码片段
//inserting before head node
n = new node;
head = n;
n->data=5;
n->next = head;
tmp = head;
//end of linked list
n->next=NULL;
没有意义。
您将 n 分配给 head。
head = n;
然后将数据成员分配给节点本身的地址,因为 head 已经等于 n。
n->next = head;
之后你重新分配了 n->next
n->next=NULL;
因此,现在节点头的数据成员 next 等于 NULL,实际上您的列表仅包含头。
程序可以这样写
#include <iostream>
struct node {
int data;
node *next;
};
int main(int argc, const char * argv[])
{
node * n;
node * head = NULL;
node * tail = NULL;
//create head node.
n = new node;
n->data = 1;
n->next = NULL;
tail = n;
head = n;
//create a new node after head node and link it with head node
n = new node;
n->data = 2;
n->next = NULL;
tail->next = n;
tail = n;
//inserting before head node
n = new node;
n->data = 5;
n->next = head;
head = n;
//print
for ( n = head; n != NULL; n = n->next ) {
std::cout<< n->data << std::endl;
}
return 0;
}
创建一个InsertNode()函数,这样您就不必重复自己。
你的开场白是不真实的。OP 正在创建新节点并将它们正确插入到 tmp 处。OP 的 insert at head 代码会在将现有列表链接到新节点之前覆盖头指针,从而破坏整个列表。
快速解释为什么将 n 分配给 head,只是覆盖了列表中的入口节点并丢失了列表,将大大增强这个答案。