2

I'm attempting to implement a Queue Class (using a Node struct, and Queue class). I'm getting a segmentation fault and my eyes are failing me, I can't seem to find it. My pushBack won't work and I'm pretty sure my popFront probably doesn't work. I'm just hoping somebody is able to give me a good push in the right direction!

Also, if you haven't been able to figure it out yet. I'm clearly very new to C++.

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

            if (back == NULL) {
                  back = temp;
                  front = temp;
            } else {
                  front->link = temp;
              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;
}
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740

2 Answers2

0

There is at least one major issue, in pushBack you are using temp without initializing it:

 void Queue::pushBack(int d) 
 {
   Node* temp;

    if (temp == NULL) {
        ^^^^

Compiling with warnings turned on would have helped you here, using the -Wall flag with gcc would have given you the following warning:

warning: 'temp' is used uninitialized in this function [-Wuninitialized]
 if (temp == NULL) {
 ^

Using a variable an unintialized automatic variable like this is undeined behavior which means the behavior of your program is unpredictable. Also see Has C++ standard changed with respect to the use of indeterminate values and undefined behavior in C++1y? for reference.

What you probably meant to do was something like this:

   Node* temp = new Node();

   temp->data = d ;

Although setting up a constructor for Node would be better.

Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • Thank you! That is exactly what my issue was. I'll keep that in mind for future reference. :) –  Mar 17 '13 at 03:14
0

you cannot set a variable like this: temp->link = NULL;