我正在尝试构造一个简单的链表,使用指向下一个插入位置的指针,并一个一个地添加一个节点。
Tnode* NULL_cp = 0;
struct Tnode{
string word;
Tnode* left;
Tnode* right;
};
int main(int argc, char** argv){
int n = 0;
Tnode head;
head.word = "the head";
head.left = NULL_cp;
head.right = NULL_cp;
Tnode* insertP = &head;
while(n<argc-1){
Tnode node;
node.word = argv[n+1];
node.left = insertP;
node.right = NULL_cp;
insertP->right = &node;
insertP = &node;
cout << "inside loop: " << insertP->word << endl;
n++;
}
cout << "outside loop: the word is " << insertP->word << endl;
}
输出是
inside loop: hello
outside loop: the word is
如果我输入a.out hello。让我感到困惑的部分是,在一个循环之后, insertP 应该指向新插入的具有单词hello的节点,但是即使在循环内它打印出hello,它也什么也没打印出来,知道为什么吗?非常感谢你