EFruchter
EFruchter

Reputation: 56

Failure to try to remove item not in LInkedList in C++

In a simple LinkedList class, I'm trying to remove an object and when the item is there it works fine but when I try to remove an item that isn't there, my program terminates and says that it just has stopped working... Posted below is the code. Any suggestions?

#include<iostream>
using namespace std;
class Node{
public:
    int data;
    Node* next;
    Node(){
        data=-1;
        next=NULL;
    }
    Node(int d){
        data=d;
        next=NULL;
    }
    Node(int d, Node* n){
        data=d;
        next=n;
    }
};
class LinkedList{
    public:
    Node* head;
    Node* dummy = new Node();
    LinkedList(){
        head=dummy;
    }
    LinkedList(Node* n){
        head=dummy;
        dummy->next=n;
    }
    void ins(Node* n){
        Node* current = head;
        while(current->next!=NULL&&current->next->data<=n->data){
            current=current->next;
        }
        n->next=current->next;
        current->next=n;
    }
    void print(){
        Node* current = head;
        while(current->next!=NULL){
            cout<<current->next->data<<endl;
            current=current->next;
        }
    }
    int peek(){
        if(head->next==NULL){
            cout<<"List is Empty"<<endl;
        }
        return head->next->data;
    }
    void rem(int toRemove){
        Node* current = head;
        while(current->next!=NULL&&current->next->data!=toRemove){
            current=current->next;
        }
        if(current->next->data==toRemove){
            current->next=current->next->next;
            cout<<"Removing Item"<<endl;
            return;
        }
        if(current->next->data!=toRemove){
            cout<<"No Item Found"<<endl;
            return;
        }
        if(current->next==NULL){
            cout<<"Not Removable since not there"<<endl;
            return;
        }
    }
};
int main(){
LinkedList* a = new LinkedList();
Node* n = new Node(5);
Node* nn = new Node(10);
Node* nnn = new Node(15);
Node* nnnn = new Node(12);
Node* nnnnn = new Node(7);
a->ins(n);
a->ins(nn);
a->ins(nnn);
a->ins(nnnn);
a->ins(nnnnn);
a->print();
a->rem(5);
a->print();
a->rem(13);
a->print();
return 0;
}

Any help is appreciated. Thanks,

Upvotes: 0

Views: 56

Answers (2)

Christophe
Christophe

Reputation: 73446

After this loop:

   while(current->next!=NULL&&current->next->data!=toRemove){
        current=current->next;

you can have two situation:

  • You've found your data:
  • or current->next==NULL because you managed to arrive at end of the list

The next statement you execute is

    if(current->next->data==toRemove){

But if current->next was NULL you try to dereference a null pointer and get your segfault ! You have to first check that you're not at NULL.

Edit: Once you've corrected this, I believe there's other issues you have to think of:

  • what if you want to delete the first element ? You don't foresee to update head when this happens. Anyway, as you start to lookig at the next data, you'll miss it. Sorry, I didn't notice the dummy node ;-)
  • Your three if seem to be mutually exclusive, so you should connect them with an else

Upvotes: 0

Evan
Evan

Reputation: 497

In your rem() function, your while loop gets you safely to a node that is not null, but after the while loop you don't check to see if current->next is not null. If it is null, you will crash when dereferencing current->next->data. Which is what happened when I ran your code.

I would suggest loop until you FIND the one to remove, not loop while you DON'T find it -- you may never find it.

Upvotes: 2

Related Questions