0

I'm trying to implement a Linked List following this example.

Here is a resume what's going on:

class Node {
    public:
        string name; 
        int number;
        Node *next;
        Node () {
            next = NULL;
        }
        Node (string _name, int _number) {
            name = _name;
            number = _number;
            next = NULL;
        }
};

class List {
    public:
        Node *head; 
        List () {
            head = NULL;
        }

        void push_back (string _name, int _number) {
            Node *neww = neww Node;
            neww->name = _name;
            neww->number = _number;
            if (head == NULL)
                head = neww;
            else {
                Node *whre = head;
                while (whre->next)
                    whre = whre->next;
                whre->next = neww;
            }
        }
        bool find (int _number) {
            Node *pointer = new Node;
            if (!head)
                return false;
            pointer = head;
            for (; pointer; pointer = pointer->next)
                if (pointer->number == _number)
                    return true;
            return false;
        }
        bool delete_item (int _number) {
            Node *pointer = new Node;
            if (!find(_number))
                return false;
            while (head->number == _number) //problem is here
                head = head->next;
            if (!head)
                return false;
            pointer = head;
            while (pointer) {
                if (pointer->next)
                    if (pointer->next->number == _number)
                        pointer->next = pointer->next->next;
                pointer = pointer->next;
            }
            return true;
        }
};

At main(), I created a List object, pushed a element and deleted it. Worked fine.

But when I use a switch-case menu to make things more dinamically, I got this error:

program received signal SIGSEGV, segmentation fault

I've no idea what this error means and didn't find help at google. I'm using the delete_item() in the same situation, except by the swtich-case menu.

Any hints what I can do? Or just an explanation of this error.

Here is the case where I try to delete:

int number, choice;
do {
    cin >> choice;
    switch(choice){

        [...]

        case 5:{
            cout << "Number: ";
            cin >> number;
            if(!list_obj.delete_item(number))
                cout << "Not found." << endl;
            else
                cout << "Deleted." << endl;
            system("PAUSE");
            system("cls");
            break;
        }

        [...]

Any hints to improve the question will be Welcome.

Full code

6
  • 2
    It's not a good idea to use head after you freeed it. Commented Jan 19, 2015 at 13:22
  • declaring variables inside case. Are you serious about coding! Commented Jan 19, 2015 at 13:23
  • oh yeah... I forgot to remove It's a bad idea. thanks Commented Jan 19, 2015 at 13:23
  • removed. but still not working. Commented Jan 19, 2015 at 13:24
  • 2
    delete is a reserved keyword and can't be the name of a function. Is this your real code? Commented Jan 19, 2015 at 13:27

1 Answer 1

3
if(head->number == _number)

Should never be checked this way. You should dereference it when head is not null.

i.e:

if(head && head->number == _number)

Also avoid use of keywords as function/var name

Sign up to request clarification or add additional context in comments.

2 Comments

Put head && head->number == _number inside while. So head was being null? So when I tried to acces number, the error was thrown?
dereferencing a NULL pointer is invalid and results in SEGFAULT. So keeping (head && head->number) would evaluate head first. Since head is NULL, head->number wouldnt be evaluated and hence this will save you from dereferencing NULL pointer You can read more here: stackoverflow.com/questions/2346806/what-is-segmentation-fault

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.