C++ 链表,程序在调用链表中的下一个指针时卡住

C++ Linked list, program stuck when calling the next pointer in the list

我用c++做了一个链表。 我不知道当试图指向列表的下一个元素时,程序停止了。

我的节点Class如下:

class Node {
friend class List;
private :
Node* next;
public:
int value;
Node()
{
    value = 0;
    next = nullptr;
}

Node(int data)
{
    this->value = data;
    this->next = nullptr;
}
};

我的列表 class 有 next 和 delete 方法。每当调用节点 class 中的下一个属性时。程序卡住了。 对于我的列表 Class,我将它们制作如下:

class List {
private:
Node* head;
public:
    List ()
    {
        head = 0; // create an empty list
    }
    ~List ()
    {
        delete head; // clean up the list and all nodes
    }

    Node* first () const
    {
        return head;
    }

    Node* next(const Node* n) const{
        return n->next;
    }

    void append(int i)
    {
        Node* newNode = new Node(i);

        if (head == nullptr){
            head = newNode;
        }
        else
        {
            Node *ptr = head;
            // the loop sets ptr to last node of the linked list
            while (ptr->next != nullptr){
                ptr = ptr->next;
            }
            // ptr now points to the last node
            // store temp address in the next of ptr
            ptr->next = newNode;

            }
    }


    void insert(Node* n, int i)
    {
        Node *ptr = head;
        Node *newNode = new Node(i);
        newNode->next = n;

        if(n==head)
        {
            head = newNode;
        }
        else
        {
            while(ptr->next != n)
            {
                ptr = ptr->next;
            }
            ptr->next = newNode;
        }
    }


    void erase( Node* n)
    {
        Node *ptr = head;
        Node *before ;

        if (n->next == nullptr)
            {
                free(n);
                return ;
            }

        if(head == n)
        {
            head = n->next;
            free(n);
            return ;
        }
        else
            {

            while(ptr!= n)
                {
                    before = ptr;
                    ptr = ptr->next ;
                }
                before->next = ptr;
                free(ptr);
                free(n);
                return ;
            }
    }

    void printLst()
    {
        while(head != nullptr)
        {
            std::cout<<head->value<<" ";
            head = head->next;
        }
    }
};

并全面了解计划。我把主要功能做得很简单:

int main()
{
List list;

list.append(55);
list.append(50);
list.append(20);
list.append(30);


list.insert(list.first(), 22);
list.insert(list.first(), 87);
list.printLst();

list.erase(list.first());
list.printLst();
}

有什么建议吗?

在擦除中,您永远不会为 'before'

赋值
      Node* before;

那你就做

     before->next = ptr;

Error C4703 potentially uninitialized local pointer variable 'before' used ConsoleApplication1 C:\work\ConsoleApplication1\ConsoleApplication1.cpp 124

另外 - 更重要的是,您的 printLst 函数将 head 设置为 null

void printLst()
{
    while (head != nullptr)
    {
        std::cout << head->value << " ";
        head = head->next; <<========
    }
}

你的打印函数不应该改变 head

所以 list.first 然后 returns null 所以这个

    list.erase(list.first());

用 null 调用擦除

Exception thrown: read access violation. n was nullptr.