简体   繁体   English

单链表,插入到最后

[英]Singly Linked List, insert at end

Due to reasons beyond my control, the function declaration must not change.由于我无法控制的原因,function 声明不得更改。 I'm not very good with pointers, so the NODE** declaration really throws me off.我对指针不是很好,所以 NODE** 声明真的让我失望。

I know the basic gist is more or less correct.我知道基本要点或多或少是正确的。 I just keep getting errors due to the pointers and don't know how to solve it.由于指针,我不断收到错误,不知道如何解决。

There is currently an error in this line of the else statement: else 语句的这一行当前有一个错误:

NODE* last = data;

//a value of type "NODE **" cannot be used to initialize an entity of type "NODE *"

Yet I don't know if I've been handling the pointers correctly.但是我不知道我是否一直在正确处理指针。

class NODE {
  public:
    string firstname;
    string lastname;
    string email;
    NODE* next;
};

void add_node_tail(NODE** data, string firstname,string lastname, string email) {
    NODE* temp = new NODE;
    temp->firstname = firstname;
    temp->lastname = lastname;
    temp->email = email;
    temp->next = NULL;

    if(!data) { // empty list becomes the new node
        data = &temp;
        return;
    } else { // find last and link the new node
        NODE* last = data;
        while(last->next) {
            last=last->next;
            last->next = temp;
        }
    }
}

If you could give me a rundown of how the pointers would work in the correct implementation, that would be the perfect answer.如果你能给我一个关于指针在正确实现中如何工作的概要,那将是一个完美的答案。

NODE** data is a pointer to NODE* , so you should dereference the pointer like *data to get the value of NODE* . NODE** data是指向NODE*的指针,因此您应该像*data这样取消引用指针来获取NODE*的值。

void add_node_tail(NODE** data, string firstname,string lastname, string email) {
    NODE* temp = new NODE;
    temp->firstname = firstname;
    temp->lastname = lastname;
    temp->email = email;
    temp->next = NULL;

    // *** dereference data to get the value ***
    if(!*data) { // empty list becomes the new node
        // *** dereference data to set the value ***
        // also remove the extra & not to save a pointer to local variable
        *data = temp;
        return;
    } else { // find last and link the new node
        // *** dereference data to get the value ***
        NODE* last = *data;
        while(last->next) {
            last=last->next;
            last->next = temp;
        }
    }
}

A slightly different way to look at the problem.看待问题的方式略有不同。

data is (or better be because the asker's code and all of the answers won't work if it is not) pointer to a next pointer. data是(或者更好的是,因为提问者的代码和所有答案都不起作用)指向下一个指针。 The head pointer is a next pointer by another name;头指针是另一个名称的下一个指针; think of it as the first next pointer.将其视为第一个下一个指针。 There's nothing special about it, so if you abstract away the different name by taking a pointer to it, you can write much simpler code.它没有什么特别之处,所以如果你通过一个指向它的指针抽象出不同的名字,你可以编写更简单的代码。 data has already done this first step. data已经完成了这第一步。

This means all you need to do is find the first null next pointer and point it at the new node.这意味着您需要做的就是找到第一个 null 下一个指针并将其指向新节点。

void add_node_tail(NODE** data, string firstname,string lastname, string email) {
    while (*data) { // if data points at a non-null next
        data = &(*data)->next; // point at the pointer to the next node
    }
    *data = new NODE{ firstname, lastname, email, NULL }; // insert at last next
}

Note: new NODE{ firstname, lastname, email, NULL };注意: new NODE{ firstname, lastname, email, NULL }; is making use of aggregate initialization to make the initialization easier to write.正在使用聚合初始化来使初始化更容易编写。

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

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