Cheshire Cat
Cheshire Cat

Reputation: 11

Segmentation fault in queue program when using push

Im getting a segmentation fault when i try to push elements into the queue, im not an expert working with queues so i dont recognize where the problem is. I have been searching for the solution to this problem and even though people get similar problems i didnt help me fix my problem. Here is the code:

(I used the debug option in Dev-c ++ 5.9.2 and it told me the line "temp->link = NULL;" is causing the problem but i have no idea how to fix it)

#include <iostream>
using namespace std;

struct Node {
    int data;
    Node* link;
};

class Queue {
public:
    Queue(); 
    ~Queue(); 
    void pushBack(int d);
    bool popFront(); 
    bool isEmpty(); 
    void displayQueue();
private:
    Node* back;
    Node* front;
};

Queue::Queue() {
    back = NULL;
    front = NULL;
}

Queue::~Queue() {
        while (!isEmpty()) {
        popFront();
    }
}

void Queue::pushBack(int d) {
    Node* temp;
    if (temp == NULL) {
    return;
    } else {
            temp->link = NULL; <========== This is where is get the error
            if (back == NULL) {
                  back = temp;
                  front = temp;
            } else {
                  front->link = temp; <===== here too
              front = temp;
            }
      }
}


bool Queue::popFront() {
    if (front == NULL) {
        return false;
    } else {
        Node* removeNode;
        removeNode = front;

        if (back == front) {
            back = NULL;
            front = NULL;
        } else {
            Node* previousFront = back;
            while (previousFront->link != front) {
                previousFront = previousFront->link;
            }

            front = previousFront;
            front->link = NULL;
        }

        delete removeNode;
        return true;
    }
}

bool Queue::isEmpty() {
    return (back == NULL);    
}

void Queue::displayQueue() {
    if (isEmpty()) {
        cout << "Queue is empty!" << endl;
    } else {
        Node *current;

        current = back;

        cout << endl << "-- BACK --  ";

        while (current != NULL) {
        cout << current->data << "  ";
        current = current->link;
        }

        cout << "-- FRONT --" << endl << endl;
    }
}

int main(){
    Queue q;
    q.displayQueue();
    q.pushBack(20);
    q.pushBack(30);
    q.displayQueue();
    q.pushBack(40);
    q.pushBack(12);
    q.displayQueue();
    q.popFront();
    q.displayQueue();

    return 0;
}

Upvotes: 1

Views: 2010

Answers (3)

Omar Natour
Omar Natour

Reputation: 111

You have to know that when you add a new node to the list you constructed, you need to allocate a dynamic location for the new node and then add it to the list -queue-;

second thing : when the back is pointing already at some node in the link you need to make the new node points at the node the back was pointing at, then make the back pointer points at the new node .

the new function (pushBack) bacomes :

void Queue::pushBack ( int d ) {
Node* temp = new Node;
temp->data = d;
temp->link = NULL;

        if (back == NULL) {
            back = temp;
            front = temp;
        }
        else {
            temp->link = back;
            back = temp;
        }

}

Upvotes: 2

Chaves MainMan
Chaves MainMan

Reputation: 29

You are creating a pointer to a node, but you have not created the node yet. (what everyone else has said)

change

Node* temp; - stack memory

To

Node *temp = new Node() - heap memory

Upvotes: 1

BraveNewCurrency
BraveNewCurrency

Reputation: 13065

im not an expert working with queues so i dont recognize where the problem is

Note that the problem has nothing to do with queues: The problem is understanding how the language works.

As Thornkey pointed out, you have a temp var in your pushBack function. It's a pointer, but it points to random data until you tell what to point at. When you follow the pointer, it could go anywhere and get a segfault or break some other part of your program.

Upvotes: 0

Related Questions