Queue Data structure app crash with front() method
Posted
by
Programer
on Stack Overflow
See other posts from Stack Overflow
or by Programer
Published on 2012-11-19T10:55:21Z
Indexed on
2012/11/19
11:00 UTC
Read the original article
Hit count: 268
I am implementing queue data strcutre but my app gets crashed, I know I am doing something wrong with Node pointer front or front() method of queue class
#include <iostream>
using namespace std;
class Node
{
public:
int get() { return object; };
void set(int object) { this->object = object; };
Node * getNext() { return nextNode; };
void setNext(Node * nextNode) { this->nextNode = nextNode; };
private:
int object;
Node * nextNode;
};
class queue{
private:
Node *rear;
Node *front;
public:
int dequeue()
{
int x = front->get();
Node* p = front;
front = front->getNext();
delete p;
return x;
}
void enqueue(int x)
{
Node* newNode = new Node();
newNode->set(x);
newNode->setNext(NULL);
rear->setNext(newNode);
rear = newNode;
}
int Front()
{
return front->get();
}
int isEmpty()
{
return ( front == NULL );
}
};
main()
{
queue q;
q.enqueue(2);
cout<<q.Front();
system("pause");
}
© Stack Overflow or respective owner