【发布时间】:2011-08-31 16:43:15
【问题描述】:
这是一个试图制作链表的程序。
#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函数后的图为真,那么while( temp2->next != NULL )语句中的temp2->next包含什么?
【问题讨论】:
-
有一个作业标签你知道;-)
-
@Suhail Gupta startPTR 不是指向 temp1,而是指向 temp1 指向的起始节点。因此,您的图表需要更正。
-
@EAGER_STUDENT 那么
startPTR = temp1是什么意思呢? -
@Suhail Gupta startPTR 是一个指针,你的图中 temp2 是如何指向 startPTR 的?
-
@ Nicolas Grebille 对于某些情况,我想介绍一个
self satisfaction标签
标签: c++ list visual-c++ pointers linked-list