Home > front end >  Problem when deleting specifics nodes in linked list
Problem when deleting specifics nodes in linked list

Time:10-26

I am trying to delete specifics nodes by a give data in my pop method, so my linked list code is:

#include <iostream>
#include <string>
using namespace std;

class Node
{
private:
public:
    int n1;
    int n2;
    Node *next;

    Node(int n1, int n2)
    {
        this->n1 = n1;
        this->n2 = n2;
        this->next = NULL;
    }
};

class LinkedList
{
private:
public:
    Node *start;
    int size;
    LinkedList()
    {
        this->start = NULL;
        this->size = 0;
    }

    void append(int n1, int n2)
    {
        Node *node = new Node(n1, n2);
        if (this->start == NULL)
        {
            this->start = node;
        }
        else
        {
            Node *temp = this->start;
            while (temp->next != NULL)
            {
                temp = temp->next;
            }
            temp->next = node;
        }
        this->size  ;
    }

    void print()
    {
        Node *temp = this->start;

        while (temp != NULL)
        {
            cout << "n1: "   to_string(temp->n1)   ", n2: "   to_string(temp->n2)   "\n";
            temp = temp->next;
        }
    }

    void pop(int n1, int n2)
    {
        Node *temp = this->start;
        Node *prev = NULL;

        if (temp != NULL && temp->n1 == n1 && temp->n2 == n2)
        {
            this->start = temp->next;
            delete temp;
            this->size--;
            return;
        }
        else
        {
            while (temp != NULL && temp->n1 != n1 && temp->n2 != n2)
            {
                prev = temp;
                temp = temp->next;
            }
            if (temp == NULL)
            {
                return;
            }
            prev->next = temp->next;
            this->size--;
            delete temp;
        }
    }
};

And then I was testing with different code in main method, so for example with the next main code I am tryng to delete the node that has (0, 0) data:

int main(int argc, char const *argv[])
{
    LinkedList lk;
    lk.append(0, 0);
    lk.append(0, 1);
    lk.append(0, 2);
    lk.append(0, 3);
    lk.append(0, 5);
    lk.append(0, 6);

    lk.pop(0, 0);

    lk.print();

    return 0;
}

And with the above code my output is successful because (0, 0) is not in the linked list:

n1: 0, n2: 1
n1: 0, n2: 2
n1: 0, n2: 3
n1: 0, n2: 5
n1: 0, n2: 6

But with the next main code, I am tryng to delete intermediate nodes, last node, first node:

int main(int argc, char const *argv[])
{
    LinkedList lk;
    lk.append(0, 0);
    lk.append(0, 1);
    lk.append(0, 2);
    lk.append(0, 3);
    lk.append(0, 5);
    lk.append(0, 6);

    lk.pop(0, 5);
    lk.pop(0, 1);
    lk.pop(0, 0);
    lk.pop(0, 6);

    lk.print();

    return 0;
}

And with the above main code my output is nothing. It is only a message error:

[Done] exited with code=3221225477 in 1.173 seconds

Meanwhile my expected output should be:

n1: 0, n2: 2
n2: 0, n2: 3

Testing with different main codes, I notice that my problem is in pop method, but I don´t really know how to fix it. Thanks.

CodePudding user response:

This line:

while (temp != NULL && temp->n1 != n1 && temp->n2 != n2)

Should be

while (temp != NULL && (temp->n1 != n1 || temp->n2 != n2))

Otherwise, it will delete first element that matches either n1 or n2, but not necessarily both.

While we are here, let's simplify your pop() function:

    void pop(int n1, int n2)
    {
        Node *temp = start;
        Node *prev = NULL;

        while ( temp != NULL && ((temp->n1 != n1) || (temp->n2 != n2)) )
        {
            prev = temp;
            temp = temp->next;
        }

        if (temp != NULL)
        {
            if (prev == NULL)
            {
               start = temp->next;
            }
            else
            {
                prev->next = temp->next;
            }
            size--;
            delete temp;
        }
    }
  • Related