首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >next 2->next包含什么?

next 2->next包含什么?
EN

Stack Overflow用户
提问于 2011-08-31 16:43:15
回答 2查看 714关注 0票数 1

这是一个试图建立链接列表的程序。

代码语言:javascript
复制
#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->nextwhile( temp2->next != NULL )语句中包含什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-08-31 16:47:51

你的图表不正确。start = temp2确实意味着start和temp2指针都指向同一个节点。您的图表显示了temp2指针的下一个字段保存着开始的地址。而且,在执行start->next = temp1之后,并不意味着如果您在temp1中获得了一些新的节点值(在下一个函数调用中),start->next仍然会继续指向刚刚在temp1中分配的新值。它将保存旧值,在您用新的值覆盖它之前。start->next = temp1只是在temp1 ie中复制值。指向变量(指针变量)的地址,该变量是由start指向的节点的下一个组件,即start->next。之后,start和temp1之间就没有任何联系了。

在链接列表上下文中,"temp1 -> temp2“是指存储在temp1中的节点的next字段,该节点的地址由temp2持有或持有。现在,在更改指针变量的值之后,temp2不会更改存储在temp1中的地址处节点的next字段。temp1->next仍然包含它以前存储的值。

下一个链接不指向某个变量名称,即,start->next = temp start 将不会使start节点的下一个节点始终指向节点 temp1 包含的任何节点,但是 start->next 将包含在分配时存储的E 236 temp1 <代码>E 140的地址。

请注意,通过说"start is指向temp1“意味着地址

代码语言:javascript
复制
while (temp2->next != NULL)
  temp2 = temp2->next;

temp2->next = NULL中断时,这意味着temp2指向列表的最后一个节点。此时,temp2->next = temp1将新分配的节点链接到temp2当前指向的节点之后。这就是简单地在末尾添加新节点。

代码语言:javascript
复制
  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.

更新

第一次:

代码语言:javascript
复制
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)

第二次:

代码语言:javascript
复制
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

第三次:

代码语言:javascript
复制
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链接遍历列表,直到最后一个链接(在下一个字段中由空指针检测到)时,最后一个节点的空链接被temp1持有的新分配节点替换。由于现在temp1保存的节点被链接/添加到列表的末尾,所以temp1被重用来容纳另一个新节点。

票数 1
EN

Stack Overflow用户

发布于 2011-08-31 16:47:49

它包含NULL,这是因为这行:

代码语言:javascript
复制
temp1->next = NULL; 

每个新节点都有next指针,通过执行上面的步骤使之成为NULL,并且新节点被追加到列表的末尾,结果,列表的末尾总是NULLwhile循环遍历到列表的末尾,while(temp2->next != NULL)设置条件,直到next of temp2变成NULL,do temp2 = temp2->next

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7260452

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档