The purpose of the queue is to store pointers to objects of type T
. The queue is used to push and pop objects on and off the queue as I traverse various data structures.
The queue works fine except the line :
out = t->payload;
(four lines from the bottom)
out never changes value to what is in t->payload
. Looking at it in the debugger, I can see that t->payload
is set correctly, but out is not assigned the value that is in t->payload
. Can someone tell me what is going on?
template<typename T>
class Queue
{
private:
struct node
{
T *payload;
struct node *next;
};
node *head;
node *tail;
public:
Queue()
{
head = new node;
tail = new node;
head->next = tail;
tail->next = tail;
}
...
void push(T *payload)
{
node *newNode;
node *temp;
newNode = new node;
temp = head->next;
head->next = newNode;
newNode->next = temp;
newNode->payload = payload;
}
bool pop(T *out)
{
node *t;
node *x;
t = head;
x = head;
if(t == tail)
{
return false; //already empty
}
while(t->next != tail)
{
x = t;
t = t->next;
}
x->next = tail;
out = t->payload;
delete t;
return true;
}
}