在头部添加新节点
adding new nodes at head
赦免我。我是C 的新手。我试图将第三个节点添加为head节点并初始化数据,但看来它使其命令是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;
}
此行将 first 节点的 next
设置为 NULL
,而不是最后一个:
//end of linked list
n->next=NULL;
此外,您确实将n
的next
分配给了:
head = n;
n->next = head;
您应该在重新分配head
之前设置n
的next
。
如果要设置最后一个节点的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 = n;
然后分配了节点本身地址旁边的数据成员,因为头已经等于n。
n->next = head;
之后,您重新签名n-> next
n->next=NULL;
因此,现在节点头的数据成员接下来等于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;
}
相关文章:
- 节点 *temp; 和节点 *tmp = 新节点之间的差异
- 为什么我的节点在我设置后被设置为 nullptr = 新节点?
- 指向二叉树中新节点的指针
- 如果我们不创建一个新节点并使用指针插入数据并建立链接(在链表中)怎么办?
- C++ 如何使用动态计算的新节点实现 A*?
- 如何在使用 ItemType 的模板类时将新节点插入二叉树 c++.(我是 c++ 的新手)
- 在不创建新节点的情况下实现带有映射的trie
- 添加新节点后,链接列表的负责人总是无效
- 推送功能(将新节点添加到列表顶部)C
- 二叉树不接受新节点
- 使用括号会在声明新节点时会产生错误
- C++链表将新节点添加到列表的开头
- C++为什么创建一个新节点来删除堆栈上的节点是错误的
- 如何在双链接列表中创建新节点?(C++)
- 了解如何在链接列表末端添加新节点
- 创建新节点的困难
- C++ 在链表中间插入新节点时出现问题
- 将新节点添加到列表中并动态命名
- C++:尝试将新节点添加到链表会产生"线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x0)"错误
- 在头部添加新节点