简体   繁体   English

C ++创建链接列表并打印

[英]C++ create linked list and print

I created a linked list and wanted to print the items. 我创建了一个链接列表,并希望打印这些项目。

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;
}

If I write my code as this, it works fine (print 0123456789). 如果我以此方式编写代码,则可以正常工作(打印0123456789)。 But after making a slight change after the for loop: 但是在for循环之后进行一些细微的更改后:

constructor->item = 9;
constructor->next = new node;
constructor = constructor->next;
constructor = nullptr;

I assumed this would work the same way. 我认为这将以相同的方式工作。 but the output is 01234567890 with a one more 0 added. 但输出为01234567890,并添加了一个0。 Can anyone tell me why? 谁能告诉我为什么?

Thank you so much for help! 非常感谢您的帮助!

You've added a new node after the 9 entry but never defined the item value. 您在9条目之后添加了一个新节点,但从未定义item值。
The value happened to default to zero. 该值默认为零。


As to the difference between 至于两者之间的区别

// Creates a new node...
constructor->next = new node;

// Then ignores it by making the current position the end of the list
constructor->next = nullptr;

and

// 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;

the comments should help explain the difference. 评论应有助于解释差异。

They both create a new node, but in the second block, the constructor = constructor->next; 它们都创建了一个新节点,但是在第二个块中, constructor = constructor->next; moves to the new node before marking the end of the list. 在标记列表结尾之前移至新节点。 The end result is that the second block of code has one more node in the list than the first block. 最终结果是第二个代码块在列表中比第一个代码块多一个节点。

In the first case, you make the constructor->next point to the new node created, and then to the nullptr. 在第一种情况下,您使Constructor-> next指向创建的新节点,然后指向nullptr。 This is where the new node is lost. 这是新节点丢失的地方。 That is , the node that is pointed by the constructor currently,which is 9 in this case, its next will point to the new node first, and in the next line, its reference is changed to the nullptr. 也就是说,构造函数当前指向的节点(在这种情况下为9),其下一个将首先指向新节点,在下一行中,其引用将更改为nullptr。 In the second case after creating the new node, you move you pointer constructor to the node to the next of 9. So when you say next of constructor now, it implies the next of the newly created node, to which the constructor pointer is pointing. 在第二种情况下,创建新节点后,将指针构造函数移动到该节点的9的下一个节点。因此,当您现在说下构造函数的下一个时,它意味着新创建的节点的下一个,构造函数指针指向该节点。 The value zero would be initialised by default when you create a new node. 创建新节点时,默认情况下将初始化零值。 So the newly created node is not lost in the second case. 因此,在第二种情况下,新创建的节点不会丢失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM