Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

I have made a linked list with c++.
I have no idea when trying to point to the next element of the list, the program stops.

My Node Class as follows:

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;
}
};

My List class has next and delete methods. Whenever there is calling for the next attributes in the Node class. The program stucks.
For my List Class I made them as follows:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
        }
    }
};

and to get a full vision of program. I made the main function very simple:

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();
}

Any suggestions ?

>Solution :

in erase you never assign a value to ‘before’

      Node* before;

then you do

     before->next = ptr;

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

also – more importantly your printLst function sets head to null

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

your print function should not change head

so list.first then returns null so this

    list.erase(list.first());

calls erase with null

Exception thrown: read access violation.
n was nullptr.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading