简体   繁体   English

单链列表,为什么列表为空(head == NULL)?

[英]Singly linked list, why is the list empty (head == NULL)?

I created a singly linked list: 我创建了一个单链表:

#include <iostream>

using namespace std;

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

bool isEmpty(Node *head){
    if (head == NULL){
        return true;
    }
    else{
        return false;
    }
}

void append(Node *head, Node *last, int data){
    Node *newNode = new Node;
    newNode->data = data;
    newNode->next = NULL;
    if (isEmpty(head)){
        head = newNode;
        last= newNode;
    }
    else{
        last->next = newNode;
        last= newNode;
    }

}

void printList(Node *current){
    if (isEmpty(current)){
        cout << "List is empty." << endl;
    }
    else{
        int i = 1;
        while (current != NULL){
            cout << i << ". Node: " << endl;
            cout << current->data << endl;
            current = current->next;
            i++;
        }
    }
}

void main(){
    Node *head = NULL;
    Node *last = NULL;
    append(head, last, 53);
    append(head, last, 5512);
    append(head, last, 13);
    append(head, last, 522);
    append(head, last, 55);
    printList(head);
}

When I compile it, the output is this: 当我编译它时,输出是这样的:

List is empty. 清单为空。

But I don't know why. 但是我不知道为什么。 "head" gets an address, so "head" shouldn't be NULL. “ head”获取地址,因此“ head”不应为NULL。 But apparently it is NULL. 但显然是NULL。 I don't know how to solve this problem. 我不知道如何解决这个问题。

You have to remember that arguments to functions are by default passed by value, which means that the arguments value is copied and the function only works on the copy and not the original. 您必须记住,默认情况下,函数的参数是按值传递的,这意味着将复制参数值,并且该函数仅适用于副本,而不适用于原始副本。

Now take the append function, when you pass the head argument the pointer is copied into the argument, and any changes made to head inside the function is only made to that copy. 现在使用append函数,当您传递head参数时,指针将被复制到该参数中,并且对该函数内部head所做的任何更改仅会对该副本进行。

To make it work you need to pass the arguments you change by reference , like 为了使其正常工作,您需要传递通过引用更改的参数 ,例如

void append(Node *&head, Node *&last, int data)

Now head and last are references to the original pointer variables, and changes to the variable are made to the original variables. 现在headlast是对原始指针变量的引用,并且对该变量所做的更改是对原始变量的。

The parameter head and last are passed by value, so the change of them inside the function append has nothing to do with the outside variables, they are independent. 参数headlast是通过值传递的,因此它们在函数append的更改与外部变量无关,它们是独立的。

You need to pass it by reference or pointer of pointer, such as: 您需要通过引用或指针指针传递它,例如:

void append(Node *&head, Node *&last, int data) {

In the function : append(Node *head, Node *tail, int data) 在函数中: append(Node *head, Node *tail, int data)

you should pass head and tail by reference. 您应该通过参考传递headtail In your approach when you are assigning head = newNode then it is being assigned to a local copy of head and not the head that you passed. 在您的方法中,当您分配head = newNode会将其分配给head的本地副本,而不是您传递的head

Pass the address of the head and last variable and it sholud work. 传递head和last变量的地址,它将有效。

append(&head, &last, 53);

and changes in the append function as below 以及添加功能的变化如下

void append(Node **head, Node **last, int data)

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

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