C++创建链表并打印
C++ create linked list and print
我创建了一个链表并想打印这些项目。
struct node{
int item;
node *next;
}; typedef node* nodeptr;
void insertAt(nodeptr headnode, size_t index, int item);
int main(int argc, const char * argv[]) {
nodeptr head;
head = new node;
nodeptr constructor = new node;
head->item = 0;
head->next = constructor;
for(int n = 0; n<8; n++){
constructor->item = n+1;
constructor->next = new node;
constructor = constructor->next;
}
constructor->item = 9;
constructor->next = new node;
constructor->next = nullptr;
for(nodeptr begin = head; begin != nullptr; begin = begin->next){
cout << begin->item << endl;
}
return 0;
}
如果我这样写我的代码,它工作正常(打印 0123456789)。但是在 for 循环之后稍作改动后:
constructor->item = 9;
constructor->next = new node;
constructor = constructor->next;
constructor = nullptr;
我以为这会以同样的方式工作。但输出是 01234567890 加上一个 0。谁能告诉我为什么?
非常感谢您的帮助!
您在 9
条目后添加了一个新节点,但从未定义 item
值。
该值碰巧默认为零。
至于
的区别
// Creates a new node...
constructor->next = new node;
// Then ignores it by making the current position the end of the list
constructor->next = nullptr;
和
// Creates a new node...
constructor->next = new node;
// Makes the new node the current node
constructor = constructor->next;
// Marks the current position as the end of the list
// The list is now one item longer than the previous example
constructor = nullptr;
评论应该有助于解释差异。
它们都创建了一个新节点,但在第二个块中,constructor = constructor->next;
在标记列表末尾之前移动到新节点。最终结果是第二个代码块在列表中比第一个块多了一个节点。
在第一种情况下,你让constructor->next 指向创建的新节点,然后指向nullptr。这是新节点丢失的地方。也就是说,构造函数当前指向的节点,在本例中为 9,其 next 将首先指向新节点,并在下一行中将其引用更改为 nullptr。
在创建新节点后的第二种情况下,您将构造函数指针移动到 9 中的下一个节点。因此,当您现在说构造函数的下一个时,它意味着构造函数指针指向的新创建节点的下一个.当您创建新节点时,默认情况下会初始化值零。所以第二种情况下新创建的节点并没有丢失。
我创建了一个链表并想打印这些项目。
struct node{
int item;
node *next;
}; typedef node* nodeptr;
void insertAt(nodeptr headnode, size_t index, int item);
int main(int argc, const char * argv[]) {
nodeptr head;
head = new node;
nodeptr constructor = new node;
head->item = 0;
head->next = constructor;
for(int n = 0; n<8; n++){
constructor->item = n+1;
constructor->next = new node;
constructor = constructor->next;
}
constructor->item = 9;
constructor->next = new node;
constructor->next = nullptr;
for(nodeptr begin = head; begin != nullptr; begin = begin->next){
cout << begin->item << endl;
}
return 0;
}
如果我这样写我的代码,它工作正常(打印 0123456789)。但是在 for 循环之后稍作改动后:
constructor->item = 9;
constructor->next = new node;
constructor = constructor->next;
constructor = nullptr;
我以为这会以同样的方式工作。但输出是 01234567890 加上一个 0。谁能告诉我为什么?
非常感谢您的帮助!
您在 9
条目后添加了一个新节点,但从未定义 item
值。
该值碰巧默认为零。
至于
的区别// Creates a new node...
constructor->next = new node;
// Then ignores it by making the current position the end of the list
constructor->next = nullptr;
和
// Creates a new node...
constructor->next = new node;
// Makes the new node the current node
constructor = constructor->next;
// Marks the current position as the end of the list
// The list is now one item longer than the previous example
constructor = nullptr;
评论应该有助于解释差异。
它们都创建了一个新节点,但在第二个块中,constructor = constructor->next;
在标记列表末尾之前移动到新节点。最终结果是第二个代码块在列表中比第一个块多了一个节点。
在第一种情况下,你让constructor->next 指向创建的新节点,然后指向nullptr。这是新节点丢失的地方。也就是说,构造函数当前指向的节点,在本例中为 9,其 next 将首先指向新节点,并在下一行中将其引用更改为 nullptr。 在创建新节点后的第二种情况下,您将构造函数指针移动到 9 中的下一个节点。因此,当您现在说构造函数的下一个时,它意味着构造函数指针指向的新创建节点的下一个.当您创建新节点时,默认情况下会初始化值零。所以第二种情况下新创建的节点并没有丢失。