如何指向链表中的下一个节点并打印值
how to point to the next node in linked list and print the value
我试图将值存储在 C++ 的链表中并打印它们。但我不知道我是否正在编写正确的代码。当我创建一个新的节点插入值打印时,这有效但是。但是当我最后使用临时打印时,所有值都不起作用
#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->next;
使 temp
指向与 temp->next
指向的同一内存。这是一个空指针。
然后
temp = new node();
覆盖temp
的先前值,并使其指向新分配的node
结构。
以上两个语句没有link一个新的节点进入列表。
改为尝试
// 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
...
temp
是一个单独的变量,它占用一个单独的内存范围。
所以在这些声明之后
temp = temp->next;
temp = new node();
变量已更改temp
。但是之前指向的节点的数据成员temp->next
并没有改变,因为temp->next
占用了不同的内存范围。
因此您没有链表。您有未链接的单独节点。
您可以使用 node **
.
类型的变量 temp
通过以下方式实现您想要的效果
#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
这有效..
#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;
}
我试图将值存储在 C++ 的链表中并打印它们。但我不知道我是否正在编写正确的代码。当我创建一个新的节点插入值打印时,这有效但是。但是当我最后使用临时打印时,所有值都不起作用
#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->next;
使 temp
指向与 temp->next
指向的同一内存。这是一个空指针。
然后
temp = new node();
覆盖temp
的先前值,并使其指向新分配的node
结构。
以上两个语句没有link一个新的节点进入列表。
改为尝试
// 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
...
temp
是一个单独的变量,它占用一个单独的内存范围。
所以在这些声明之后
temp = temp->next;
temp = new node();
变量已更改temp
。但是之前指向的节点的数据成员temp->next
并没有改变,因为temp->next
占用了不同的内存范围。
因此您没有链表。您有未链接的单独节点。
您可以使用 node **
.
temp
通过以下方式实现您想要的效果
#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
这有效..
#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;
}