如何指向链表中的下一个节点并打印该值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何指向链表中的下一个节点并打印该值相关的知识,希望对你有一定的参考价值。
我试图在c ++中将值存储在链表中并打印它们。但我不知道我是否正在编写正确的代码。当我创建一个新的节点插入值 - 打印时,这可行。但是当我在最后使用temp打印所有的值都不起作用
#include <iostream>
using namespace std;
struct node
{
int val;
node *next;
};
int main()
{
node *temp = new node(); //creating the first node
node *head, *tail;
temp->val= 1; //assigning value to the first node
head = temp; //head contains the address of 1st node
cout<< "head value" <<head << endl;
cout << "head value" << temp << endl;
cout<< "1st value" << temp->val << endl;
cout << "=================================================" << endl;
//============================================second node
temp = temp->next;
temp = new node();
temp->val = 2;
cout << "head value" << head << endl;
cout << "head value" << temp << endl;
cout << "2rd value" << temp->val << endl;
cout << "=================================================" << endl;
//============================================third node
temp = temp->next;
temp = new node();
temp->val = 3;
cout << "head value" << head << endl;
cout << "head value" << temp << endl;
cout << "3rd value" << temp->val << endl;
tail = temp;
temp->next = NULL;
cout<< "=================================================" << endl;
cout<< "value in head" << head->val << endl;
cout << "=================================================" << endl;
cout<< "the value temp is reset to head which is the location of first node" << endl;
cout << "=================================================" << endl;
//temp = NULL;
temp = head; //add of first node is stored in temp
cout<< "the value of head " << head << endl;
cout<< "the value of temp " << temp << endl;
//Problem from this ............................................................
//temp->next = head->next;
cout << "the value of head " << head->next << endl;
cout << "the value of temp " << temp->next << endl;
cout<< "value in head + 1 " << temp->val << endl;
system("pause");
return 0;
}
输出显示temp->next
的地址不工作output photo
答案
temp
是一个单独的变量,它占用一个单独的内存范围。
所以在这些陈述之后
temp = temp->next;
temp = new node();
改变了变量temp
。但是之前指向的节点的数据成员temp->next
没有改变,因为temp->next
占用不同的内存范围。
因此,您没有链接列表。您有单独的未链接的节点。
您可以使用temp
类型的变量node **
以下列方式实现您想要的效果。
#include <iostream>
#include <cstdlib>
using namespace std;
struct node
{
int val;
node *next;
};
int main()
{
node *head, *tail;
node **temp = &head;
*temp = new node(); //creating the first node
( *temp )->val= 1; //assigning value to the first node
cout<< "head value" <<head << endl;
cout << "head value" << *temp << endl;
cout<< "1st value" << ( *temp )->val << endl;
cout << "=================================================" << endl;
//============================================second node
temp = &( *temp )->next;
*temp = new node();
( *temp )->val = 2;
cout << "head value" << head << endl;
cout << "head value" << *temp << endl;
cout << "2rd value" << ( *temp )->val << endl;
cout << "=================================================" << endl;
//============================================third node
temp = &( *temp )->next;
*temp = new node();
( *temp )->val = 3;
cout << "head value" << head << endl;
cout << "head value" << *temp << endl;
cout << "3rd value" << ( *temp )->val << endl;
tail = *temp;
( *temp )->next = NULL;
cout<< "=================================================" << endl;
cout<< "value in head" << head->val << endl;
cout << "=================================================" << endl;
cout<< "the value temp is reset to head which is the location of first node" << endl;
cout << "=================================================" << endl;
//temp = NULL;
temp = &head; //add of first node is stored in temp
cout<< "the value of head " << head << endl;
cout<< "the value of temp " << *temp << endl;
//Problem from this ............................................................
//temp->next = head->next;
cout << "the value of head " << head->next << endl;
cout << "the value of temp " << ( *temp )->next << endl;
cout<< "value in head + 1 " << ( *temp )->next->val << endl;
system("pause");
return 0;
}
程序输出可能看起来像
head value0x55ab880e8c20
head value0x55ab880e8c20
1st value1
=================================================
head value0x55ab880e8c20
head value0x55ab880e9c50
2rd value2
=================================================
head value0x55ab880e8c20
head value0x55ab880e9c70
3rd value3
=================================================
value in head1
=================================================
the value temp is reset to head which is the location of first node
=================================================
the value of head 0x55ab880e8c20
the value of temp 0x55ab880e8c20
the value of head 0x55ab880e9c50
the value of temp 0x55ab880e9c50
value in head + 1 2
另一答案
该声明
temp = temp->next;
使temp
指向与temp->next
指向的相同的记忆。这是一个空指针。
然后
temp = new node();
覆盖temp
的先前值,并使其指向新分配的node
结构。
以上两个语句不会将新节点链接到列表中。
而是尝试类似的东西
// At this point `temp` is pointing to the last node in the list
// Create a new node and link it into the list
temp->next = new node; // Allocate a new `node` and link it into the list
temp = temp->next; // Make `temp` point to the new node
temp->val = ...; // Set the new value
...
另一答案
这工作..
#include <iostream>
using namespace std;
struct node
{
int val;
node *next;
};
int main()
{
node *temp, *head, *tail;
temp = new node(); //creation of first node structure
temp->val = 1; //value of 1st node
head = temp; //assigning the temp address to head
cout<< "the add of head "<<head << endl;
cout<< "the add of temp "<< temp << endl;
cout<< "the value in 1st node is "<< temp->val << endl;
cout<< "=================================================" << endl;
temp->next = new node(); //creation of second node structure
temp = temp->next; //over writing the first address of temp with the
//address of second node
temp->val = 2;
cout << "the add of head " << head << endl;
cout << "the add of temp " << temp << endl;
cout << "the value in 2nd node is " << temp->val << endl;
cout << "=================================================" << endl;
temp->next = new node(); //creation of third node structure
temp = temp->next; //over writing the second address of temp with the
//address of third node
temp->val = 3;
cout << "the add of head " << head << endl;
cout << "the add of temp " << temp << endl;
cout << "the value in 3nd node is " << temp->val << endl;
cout << "=================================================" << endl;
temp->next = NULL; //the last node pointer pointers to null
temp = head; //temp is assigned the value of head which is
//the address of 1st node
cout << "the add of head " << head << endl;
cout << "the add of temp " << temp << endl;
cout << "the value in 1st node is " << temp->val << endl;
cout << "=================================================" << endl;
temp = temp->next; //the address of second node is assigned to temp
cout << "the add of head " << head << endl;
cout << "the add of temp " << temp << endl;
cout << "the value in 2nd node is " << temp->val << endl;
cout << "=================================================" << endl;
temp = temp->next; //the address of third node is assigned to temp
cout << "the add of head " << head << endl;
cout << "the add of temp " << temp << endl;
cout << "the value in 3nd node is " << temp->val << endl;
cout << "=================================================" << endl;
system("pause");
return 0;
}
以上是关于如何指向链表中的下一个节点并打印该值的主要内容,如果未能解决你的问题,请参考以下文章