segmentation fault 11 linked list

744 Views Asked by At

My class is doing a simulation of the Josephus Problem, by implementing a circular singly linked list. My code compiles but when it runs I get a segmentation fault: 11 after the list has been constructed. My debugging so far has led me to realize that the error occurs when the program enters the final while loop of the main function. I think it has something to do with how I'm using first->next but I'm not sure. Any help would be awesome, thanks. I'm coding in c++ if that wasn't obvious.

#include <iostream>
#include <string>
using namespace std;



/*
 * Node structure used by the linked list.
 */
struct Node {
    // The name stored in this node.
    string name;

    // A pointer to the next node in the list.
    Node * next;
};

/* 
 * Build the linked list
 */

class LinkedList {
    private:
        //pointer to the head of the list
        Node *head;

    public:
        // constructor for LinkedList
        LinkedList(){
            head = NULL;
        }

        /*
        * Add the given value to the list, making it the head.
        */
        void insert(string name){


            // Remember where old head was
            Node *oldHead = head;

            // allocate a new node in memory
            head = new Node;

            // set new node's fields
            head->name = name;
            head->next = oldHead;
        }

        /*
        * Remove the item on the top of the stack, returning nothing.
        */
        void remove() {
            // Remember what the new head will be
            Node* newSecond = head->next->next;

            // Deallocate the head from memory
            delete head->next;
            // Set the head to the new head
            head->next = newSecond;
        }
        /*
         * Shifts the head forward one node.
         */
        void cycle(){

            head = head->next;
        }

        Node* getHead(){
            return head;
        }

         // This is the opposite of a constructor - a destructor! You (almost)
        // never need these in Java, but in C++ they are essential because 
        // you have to clean up memory by yourself. In particular, we need to
        // empty out the stack.
        ~LinkedList() {
            // While there's a head node still left, remove the head node.
            while (head != NULL) {
                remove();
            }
        }
};



int main(){
    //create the circular linked list
    LinkedList circle;

    int people;
    cin >> people;
    string soldier;
    Node* first = circle.getHead();

    //Insert all the names
    for(int i = 0; i < people; i++){
        cin >> soldier;
        circle.insert(soldier);
    }

    //begin the killing
    while(first->next != NULL){
        circle.cycle();
        cout << " killed " << endl;
        Node* temp = first->next->next;
        circle.remove();
        first->next = temp;
    }
}
1

There are 1 best solutions below

1
On

Several problems in that code. First is this:

Node* newSecond = head->next->next;

If head->next is NULL, then you'll get a NULL pointer dereference. Which results in a crash.

However, the actual cause of this particular crash is that:

while(first->next != NULL)

is a NULL pointer dereference. At the beginning of main() you have:

Node* first = circle.getHead();

circle is empty at that point, so first is assigned NULL. And it stays NULL up to the point where you dereference it in your while statement. Thus, you get a crash.