简体   繁体   English

C++ 以更简洁的方式将项目添加到链表

[英]C++ Add items to linked list in the cleaner possible way

I'm trying to add items to the front of a list.我正在尝试将项目添加到列表的前面。 Basically, what I'm trying to do here is:基本上,我在这里要做的是:

  • Start with a null list;从一个空列表开始;

  • Read a number;读取一个数字;

  • Call function where a new node is created to store the number, and next pointer points to null;调用函数,创建一个新节点来存储数字,next指针指向空;

  • If the list is empty, then this new node is the beginning of the list (and only element)如果列表为空,则这个新节点是列表的开头(也是唯一的元素)

  • If there are more elements, then this new node points to the head of the list and becomes the new head.如果有更多元素,那么这个新节点指向链表的头部,成为新的头部。

My function does what I want (at least I can see that in the debugger) but after it returns my list is empty and the head is null again.我的函数做了我想要的(至少我可以在调试器中看到),但在它返回后我的列表为空,头部再次为空。

struct node{
    int data;
    node *next;
};

void insert_front(node *list, int num){
    node * newnode = new (node);
    newnode->data = num;
    newnode->next = nullptr;

    if (list == nullptr)
        list = newnode;
    else{
        newnode->next = list;
        list = newnode;
    }
}

int main()
{
    int n;
    node *head = nullptr;

    cout << "Input numbers to store (0 finishes input): ";
    cin >> n;
    while (n != 0){
        insert_front(head, n);
        cin >> n;
    }
    return 0;
}

Also tried this but it doesn't even compile:也试过这个,但它甚至没有编译:

void insert_front(node &lst, int num){
    node *newnode = new node();
    newnode->data=num;
    newnode->next=lst;
    lst=newnode;
}

I intentionally avoided using OOP, templates, typedef, etc. as much as possible to get a "cleaner" code so I can understand how everything works.我故意尽可能避免使用 OOP、模板、typedef 等,以获得“更干净”的代码,这样我就可以了解一切是如何工作的。

You need a reference to a pointer varibable: node* &list您需要对指针变量的引用:node* &list

void insert_front(node* &lst, int num){
    node *newnode = new node();
    newnode->data=num;
    newnode->next=lst;
    lst=newnode;
}

If you don't use a reference you'll be modifying a copy of your "lst" pointer, so the list will keep pointing to the old front after leaving this function.如果您不使用引用,您将修改“lst”指针的副本,因此在离开此函数后,列表将继续指向旧的前端。 A reference parameter in c++ is prefixed with "&" symbol. C++ 中的引用参数以“&”符号为前缀。 In single old C (not your case) you'd need a pointer to a pointer instead.在单个旧 C(不是您的情况)中,您需要一个指向指针的指针。

Don't pass by reference since you can't assign to it.不要通过引用传递,因为您无法分配给它。

node* insert_front(node* list, int val)
{
    node* n = new node();
    n->data = val;
    n->next= list;
    return n;  // return the new head of the list
}

Then when inserting:然后插入时:

while (n != 0){
    head = insert_front(head, n);  // head will always change every time you add to the front
    cin >> n;
}

Or alternatively, you can have your insert function also update list to reflect the new head, but you'd have to pass a pointer to the header pointer itself:或者,您可以让插入函数也更新list以反映新的头部,但您必须将指针传递给头部指针本身:

void insert_front(node** pList, int val)
{
    node* n = new node();
    n->data = val;
    n->next= *pList;
    *pList= n;
}


while (n != 0){
    insert_front(&head, n);  // head will always change every time you add to the front
    cin >> n;
}

You are passing the list by value.您正在按值传递列表。

See this for analogy:看这个类比:

int x;
void modify_x_where_x_is_passed_by_reference( int & x_ref);
void modify_x_where_x_is_passed_by_a_pointer( int * x_ptr);
// Usage
modify_x_where_x_is_passed_by_reference( x );
modify_x_where_x_is_passed_by_a_pointer( &x ); // the address of x

// But when your variable is a pointer!
int * y;
void modify_y_where_y_is_passed_by_reference( int* & y_ref);
void modify_y_where_y_is_passed_by_a_pointer( int* * y_ptr);
// Usage
modify_y_where_y_is_passed_by_reference( y );
modify_y_where_y_is_passed_by_a_pointer( &y ); // the address of y

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

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