1

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;
}
1
  • 1
    temp has not been initialised... but it's a local variable so it points to a random location in the stack. Commented Mar 28, 2015 at 4:38

3 Answers 3

2

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;
        }

}

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

Comments

1

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

Comments

0

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.

Comments

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.