这是一个试图制作链表的程序。
#include <iostream>
using namespace std;
struct node {
char name[20];
int age;
int height;
node* next; // Pointer to the next node
};
node* startPTR = NULL; // Start Pointer (root)
// This pointer permanantly points to the start of the list
// Initially there are no nodes
void addNode_AT_END(); // prototype for the function that 'adds nodes at the end'
int main() {
do {
addNode_AT_END();
cout << "Add more ?";
char ch;
cin >> ch;
} while( ch == 'y');
}
void addNode_AT_END() {
node *temp1;
node *temp2;
temp1 = new node; // We declare space for a pointer item and assign a temporary pointer to it
//*temp1 is the node that it points to
cout << "Enter the name : ";
cin >> temp1->name;
cout << endl << "Enter the age : ";
cin >> temp1->age;
cout << endl << "Enter height : ";
cin >> temp1->height;
temp1->next = NULL; // indicates that this node when inserted in the list will be the last node
if( startPTR == NULL) {
startPTR = temp1; // In the empty list last node will be the first node
} else {
temp2 = startPTR;
while( temp2->next != NULL )
temp2 = temp2->next;
temp2->next = temp1;
}
}从这个尚未完成的程序中,这就是我的理解:
如果第二次调用函数
addNode_AT_END
后的数字为true,那么temp2->next
语句中的while( temp2->next != NULL )
包含什么? 最佳答案
您的图不正确。 start = temp2确实意味着start和temp2指针都指向同一节点。您的图表显示temp2指针的下一个字段包含start的地址。同样,在执行start->next = temp1
之后,并不意味着如果在temp1
中获得了新的节点值(在下一个函数调用中),那么start->next
仍将继续指向刚在temp1
中分配的新值。它会保留您用新值覆盖之前的旧值。 start->next = temp1
只是将值复制到temp1
即。由start指向的节点的下一个组成部分(start->next
)的变量(指针变量)的地址。之后,start和temp1
之间没有任何联系。
在链表上下文中,“temp1 ----> temp2”表示其地址存储在next
中的节点的temp1
字段,该节点的地址具有temp2
保留或保留的地址。现在,更改指针变量的值后,temp2
不会更改保存在next
中存储的地址处的节点的temp1
字段。 temp1->next
仍然包含它之前存储的值。
下一个链接没有指向某个变量名,也就是说,start->next = temp
不会使start
节点的下一个节点始终指向temp1
包含的任何节点,但是start->next
将包含分配时temp1
所存储的地址。
请注意,说“开始指向temp1”表示该地址
while (temp2->next != NULL)
temp2 = temp2->next;
在
temp2->next = NULL
时将中断,这意味着temp2
指向列表的最后一个节点。此时temp2->next = temp1
在temp2
当前指向的节点之后链接新分配的节点。只需在末尾添加新节点即可。 At the end of the above while loop
temp2
|
V
(start) ----> (n1) ----> (n2) ----> (n3) . . . (n(n-1)) ----> (nn) ----> NULL
temp2->next = temp1 makes
temp2
|
V
(start) ----> (n1) ----> (n2) ----> (n3) . . . (n(n-1)) ----> (nn) ----> (temp1)--->NULL
because temp2 holds the address of (nn) therefore linking the new node to the next node of the last node.
更新
第一次:
start = NULL
a new address is allocated and the address stored into temp1 pointer. Also temp->next = NULL
if condition becomes true and temp1 is assigned to start
start = temp1
List state
start = addr1;
|
V
(addr1) ----> (NULL)
第二次:
a new node is allocated and the address of the new node is stored into `temp1`. Let this address be `addr2`. Now `temp1` contains the value `addr2`
start is NOT NULL, as start has addr1 in it from the last call.So the else part is true and we get the address of the start node `addr1` into temp2.
temp2 = start;
which means temp2 now points to `addr1`
while loop is encountered. The first iteration the condition `temp2->next != NULL` is FALSE. This is because `temp2` points to `addr1` and the next pointer field of `addr1` has NULL from the last time the function is called. Therefore the while loop terminates.
The next statement does `temp2->next = temp1` . `temp2` points to `addr1` address, and the address of the newly allocated node `addr2` contained in `temp1` is assigned into the next field of the node whose address is stored into `temp2`. Which actually assigns the address `addr2` to the next field of the node identified by the address `addr1`.
temp1 = addr2 after allocation
start = addr1;
|
V
(addr1) ----> (NULL) at begining
^
|
temp2
after temp2->next = temp1
start = addr1;
|
V
(addr1) ----> (addr2) ----> (NULL) at end
^
|
temp2
第三次:
temp1 = addr3 new node address allocated
start = addr1;
|
V
(addr1) ----> (addr2) ----> (NULL) at start
^
|
temp2
start = addr1;
|
V
(addr1) ----> (addr2) ----> (NULL) next iteration after temp2=temp2->next
^
|
temp2
we can see temp2->next = NULL and while condition is false. Note that temp2 contains itself the address addr2, and thus temp2->next is NOT addr2, it is NULL.
start = addr1;
|
V
(addr1) ----> (addr2) ----> (NULL) next iteration after temp2=temp2->next
^
|
temp2
After linking: temp2->next = temp1;
start = addr1; temp1 the address addr3 (new node)
| | is stored in temp1. this address is assigned
V V to the next node of temp2, replacing NULL
(addr1) ----> (addr2) ----> (addr3) ----> (NULL)
^
|
temp2
指针是在列表中移动/遍历的方式。列表的起始地址保存在指针
start
中。当每个节点的下一个字段指向下一个节点时,如果我们获得start
节点,则通过依次跟踪下一个字段,我们可以访问每个节点。 temp1
和temp2
是完成遍历的指针,它们充当临时指针,temp1
用于保存新分配的节点,temp2
用于通过跟随next
链接直到最后一个(最后一个链接)来遍历列表找到(通过下一个字段中的NULL指针检测到)后,此最后一个节点的NULL链接将替换为temp1
拥有的新分配的节点。由于现在temp1
拥有的节点已链接/添加到列表的末尾,因此temp1
被重新使用以容纳另一个新节点。关于c++ - temp2-> next包含什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7260452/