mardi 29 juin 2021

Linked list in a class is not removing an element

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

struct Node
{
    int x;
    Node* next = nullptr;
};

typedef Node* nodeptr;

class L
{
    private: 
        nodeptr head = nullptr;
        int lengthCount = 0;
    public: 
        void add(const int data);
        void print();
        void find(const int data);
        void pop(const int data);
        void listSize();
};

void L:: listSize()
{
    cout << "The size of the link list is:  " << lengthCount << endl;
}

void L::add(const int data)
{
    lengthCount += 1;
    Node* newNode = new Node; 
    newNode->x = data;  
    
    if(head == nullptr)
    {
        head = newNode; 
    }
    else
    {
        nodeptr temp = head;
        while(temp->next != nullptr)
        {
            temp = temp->next;
        }
        temp->next = newNode;
    }
}

void L::pop(const int data)
{
    if(head == nullptr)
    {
        cout << "Empty List" << endl;
    }   
    else if(head->x == data)
    {
        head = head->next;
    }
    else
    {
        nodeptr temp = head->next; 

        while(temp != nullptr)
        {
            if(temp-> x != data)
            {
                temp = temp->next;
            }
            else
            { 
                if(temp->next != nullptr)
                {   
                    temp = temp->next;
                }
                else
                {
                    temp->next = nullptr;
                }
                break;
            }
        }
    }
}

void L::find(const int data)
{
    if(head == nullptr)
    {
        cout << "Empty List" << endl;
    }
    else 
    {
        nodeptr temp = head;
        
        for(temp; temp != nullptr; temp = temp->next)
        {
            if(temp->x == data)
            {
                cout << "Found" << endl;
                break;
            }
            if(temp->next == nullptr)
                cout << "Not Found" << endl;    
        }
    }
}

void L::print()
{
    nodeptr temp = head;
    string line(20,'-');
    cout << "Print list" << endl;
    cout << line << endl;
    while(temp != nullptr)
    {
        cout << temp->x << endl;
        temp = temp->next;
    }
    cout << line << endl;
}

int main()
{
    vector <int> val;
    for(int i = 0; i < 10; i++)
        val.push_back(5*i);
    cout << "Printing list" << endl;
    for(auto i : val)
            cout << i << " ";
    cout << endl;
    L listObj;
    cout << "Adding list" << endl;
    for(auto i : val)
        listObj.add(i);
    listObj.print();
    listObj.listSize();
    listObj.find(15);
    listObj.print();
    cout << "popping 10" << endl;
    listObj.pop(10);
    listObj.print();
}

The problem that I am having is, I am not able to modify the actually memory of a linked list while using a class.

Im not sure what did I do wrong.

If adding works, i would say the idea of removing a value should work as well.

the remove function is called pop. the pop function is not removing the value 10, so i am not sure why.

If it is a function that is not in a class, i would say i need to pass a head pointer with & operator then i can actually modify the value.

Please let me know where did I do wrong.

Thanks

Aucun commentaire:

Enregistrer un commentaire