I’m creating a queue class in c++ and am having trouble getting the front function to work. It is supposed to print the value of the first node in the queue. My queue.cpp class is here
#include "queue.h"
#include <stdlib.h>
#include <iostream>
using namespace std;
queue::queue()
{
front_p = NULL;
back_p = NULL;
current_size = 0;
}
void queue::enqueue(int item)
{
node newnode = node(item, NULL);
if (front_p == NULL) //queue is empty
{
front_p = &newnode;
back_p = &newnode;
}
else
{
back_p->next = &newnode;
back_p = &newnode;
}
current_size ++;
}
int queue::dequeue()
{
//if there is only one node
if (front_p == back_p)
{
front_p = NULL;
back_p = NULL;
}
//if there are two or more
else
front_p = front_p->next;
current_size --;
}
int queue::front()
{
if (front_p != NULL)
return (*front_p).data;
}
bool queue::empty()
{
if (front_p == NULL && back_p == NULL)
return true;
else
return false;
}
int queue::size()
{
return current_size;
}
My header file (queue.h) is here
class queue
{
public:
queue(); // constructor - constructs a new empty queue.
void enqueue( int item ); // enqueues item.
int dequeue(); // dequeues the front item.
int front(); // returns the front item without dequeuing it.
bool empty(); // true iff the queue contains no items.
int size(); // the current number of items in the queue.
int remove(int item); // removes all occurrances of item
// from the queue, returning the number removed.
private:
class node // node type for the linked list
{
public:
node(int new_data, node * next_node ){
data = new_data ;
next = next_node ;
}
int data ;
node * next ;
};
node* front_p ;
node* back_p ;
int current_size ; // current number of elements in the queue.
};
test program (tester.cpp)
#include <iostream>
#include "queue.h"
#include <stdlib.h>
using namespace std;
int main(int argc, char * const argv[])
{
queue q1;
q1.enqueue(5);
cout << "front: " << q1.front() << endl;
cout << "front: " << q1.front() << endl;
cout << "front: " << q1.front() << endl;
q1.enqueue(10);
cout << "front: " << q1.front() << endl;
cout << "front: " << q1.front() << endl;
cout << "size: " << q1.size() << endl;
}
makefile
all: tester
tester: queue.o tester.o
g++ tester.o queue.o -o tester
tester.o: tester.cpp
g++ -c tester.cpp
queue.o: queue.cpp queue.h
g++ -c queue.cpp
clean:
rm -f tester *.o
When I run my test program I get this:
front: 5
front: 6299744
front: 6299744
front: 10
front: 6299744
size: 2
As you can see, after the first enqueue, front returns what it is supposed to, the value of the front of the queue. But after that it returns some strange number and I have no idea where it comes from! Then when I enqueue again, it prints fine again. Only after calling front twice does it start printing messed up values. Can anyone help me understand whats going on?
Your program runs into undefined behavior because you have pointers to memory you don’t own:
At the end of the function,
newnodeis destroyed, butfront_pandback_pare still pointing to that memory location. Either allocate dynamically:or use
std::shared_ptr<node>.