这是一个试图制作链表的程序。

#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 = temp1temp2当前指向的节点之后链接新分配的节点。只需在末尾添加新节点即可。
  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节点,则通过依次跟踪下一个字段,我们可以访问每个节点。 temp1temp2是完成遍历的指针,它们充当临时指针,temp1用于保存新分配的节点,temp2用于通过跟随next链接直到最后一个(最后一个链接)来遍历列表找到(通过下一个字段中的NULL指针检测到)后,此最后一个节点的NULL链接将替换为temp1拥有的新分配的节点。由于现在temp1拥有的节点已链接/添加到列表的末尾,因此temp1被重新使用以容纳另一个新节点。

关于c++ - temp2-> next包含什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7260452/

10-10 19:41