Home > Back-end >  C Linked list, program stuck when calling the next pointer in the list
C Linked list, program stuck when calling the next pointer in the list

Time:05-26

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:

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 ?

CodePudding user response:

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.

  •  Tags:  
  • c
  • Related