Hadi
Hadi

Reputation: 145

Remove Last end node from Linked List in C++

I want to remove the last end node from Linked List in C++.

My program has an exception. it is in a while loop.

How can I solve it? When the linked list has 1 node, program throw exception. the program works when we have more than 1 Node in the linked list.

Exception thrown: read access violation. Start->Next was nullptr. occurred

enter image description here

#include <iostream>
using namespace std;

struct Node {
    int Data;
    Node* Next;
};

class LinkedList {

private:
    Node* Head = NULL;
    Node* Tail = NULL;
    Node* temp = NULL;

public:

    void Setup(int Data)
    {
        temp = new Node;
        temp->Data = Data;
        temp->Next = NULL;
        if (Head == NULL)
        {
            Head = temp;
            Tail = temp;
        }
        else
        {
            Tail->Next = temp;
            Tail = Tail->Next;
        }
        cout << "Insert : " << Data << endl;
        //temp = NULL;
    }

    void Delete()
    {
        if (Head != NULL)
        {
            temp = Head;
            Head = Head->Next;
            cout << " Deleted Item is: " << temp->Data << endl;
            delete temp;
        }
        else
        {
            cout << " List is Empty \n";
        }
    }


    void DeleteFromEND()
    {
        Node* Start = Head;
        if (Start == NULL)
        {
            cout << " There is no item to delete!" << endl;
            return;
        }
        if (Start->Next != NULL)
        {
            while ((Start->Next)->Next != NULL)
            {
                Start = Start->Next;
            }
            temp = Start->Next;
            Start->Next = NULL;
        }
        else
        {
            temp = Start;
        }
        cout << " Deleted Item From END: " << temp->Data << endl;
        delete temp;

    }
};

int main()
{
    LinkedList ls;
    ls.Setup(15);
    ls.Setup(1);
    ls.Setup(5);
    ls.Setup(9);
    ls.DeleteFromEND();
    ls.DeleteFromEND(); ls.DeleteFromEND(); ls.DeleteFromEND();
    ls.DeleteFromEND();
    ls.DeleteFromEND();
    ls.Setup(4);
    ls.Setup(545);
    //ls.Delete();
    ls.Setup(0);
    ls.Setup(-19);
    ls.DeleteFromEND();
    ls.DeleteFromEND();
    ls.DeleteFromEND();
    ls.DeleteFromEND();
    ls.DeleteFromEND();
    ls.DeleteFromEND();
    ls.DeleteFromEND();
    ls.DeleteFromEND();

}

Upvotes: 0

Views: 870

Answers (3)

Iman Kianrostami
Iman Kianrostami

Reputation: 502

You can use a debugger like GDB to detect the part of code causing the exception. Also you can read more about your exception here: Access violation

With that being said, your exception is happening because of this line: while ((Start->Next)->Next != NULL). If you have only one item in your list, Start->Next would be NULL though you will get an exception when trying to access (Start->Next)->Next. To solve it just check if list has more than one item then start the while loop.

void DeleteFromEND()
{
    if(Head == NULL)
    {
        cout << " There is no item to delete!" << endl;
        return;
    }
    Node* Start = Head;
    if(Start->Next != NULL)
    {
        while ((Start->Next)->Next != NULL)
        {
            Start = Start->Next;
        }
        temp = Start->Next;
        Start->Next = NULL;
    }
    else
    {
        temp = Start;
        Head = NULL;
    }
    cout << " Deleted Item From END: " << temp->Data << endl;
    delete temp;

}

Upvotes: 1

Himanshu Singh
Himanshu Singh

Reputation: 2165

In your function DeleteFromEND(), you just need to check if this is the last node.

if(start->next == NULL){
   // write code to delete single node
}
else{
  // your remaining code
}

This way your code will work for last node deletion.

Upvotes: 1

Nikhil Badyal
Nikhil Badyal

Reputation: 1697

You can simply use debugger to check which part is causing issue to the code. Btw This is how last node is deleted.

struct node* delete_last(struct node* head)
 {  int i;  
 if(head == NULL){ // if list is empty.
  return head;
}
if(haed->next->next == NULL){ // if list has only 1 element.
  free(head); 
  return head;
}
 struct node* temp1 = head; 
while(temp1->next->next!=NULL)  
 {  temp1 = temp1->next;    }   
 struct node* temp2 = temp1->next; 
temp1->next=temp2->next;    
 free(temp2);   
 return head;
  }

Upvotes: 0

Related Questions