这是一个试图制作链表的程序。
#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
后的数字为真,那么temp2->next
语句中的while( temp2->next != NULL )
包含什么?
答案 0 :(得分:2)
它包含NULL
,这是因为这一行:
temp1->next = NULL;
通过执行上述步骤,每个新节点都有next
指针,并且新节点会附加在列表的末尾,结果是列表总是NULL
。 NULL
循环遍历到列表末尾,while
设置条件,直到while(temp2->next != NULL)
next
变为temp2
,NULL
}}
答案 1 :(得分:1)
您的图表不正确。 start = temp2确实表示start和temp2指针都指向同一个节点。您的图表显示temp2指针的下一个字段包含start的地址。在执行start->next = temp1
之后,并不意味着如果在temp1
中获得一些新节点值(在下一个函数调用中),start->next
仍然会指向刚刚分配的新值temp1
。它将保留旧的值,然后用新的值覆盖它。 start->next = temp1
只是复制temp1
中的值即。变量的地址(指针变量)start指向的节点的下一个组件start->next
。之后,start和temp1
之间没有任何关联。
在链表上下文中“temp1 ----&gt; temp2”表示地址存储在next
中的节点的temp1
字段,保存带有地址的节点的地址由temp2
举行或举行。现在,在更改指针变量temp2
的值后,不会更改next
中存储的地址所保存的节点的temp1
字段。 temp1->next
仍然包含之前存储的值。
下一个链接没有指向某个变量名,也就是说,start->next = temp
不会使start
节点的下一个节点始终指向temp1
包含的任何节点,但是它start->next
将包含temp1
在分配时存储的地址。
请注意,通过说“start指向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
被重用来保存另一个新节点。