Queue/Stack ADTs with templates.
Jan 15, 2010 at 5:20pm UTC
Hi people,
First time poster, I have been trying to implement a basic Queue structure using templates. I am using VS2008 and I keep getting LNK2019 whenever I try deqeueing items.
I have set it up so you pass the Queue the data type to be stored and upon queueing an item it creates a node that holds a pointer to the template but more importantly when I dequeue an item I return the class data rather than the node.
Could I get some pointers on sorting this issue out. Code below
Node class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
template <class T>
class Node
{
public :
Node<T>();
Node<T>(T* n);
void setNext(Node* n);//Can't pass const Node* as you can't} Well, there was some error or other
void setPrev(Node* n);//assign a const value to a variable }
Node* getNext(void )const ;
Node* getPrev(void )const ;
void setData(T* data);
T* getData(void );
private :
Node<T>* next;
Node<T>* previous;
T* pData;
};
//Code omitted
template <class T>
Node<T>::Node(T* n)
{
pData = n;
next = 0;
previous = 0;
}
//code omitted
template <class T>
void setData(T* data)
{
pData = data;
}
template <class T>
T* getData(void )
{
return pData;
}
Queue Structure:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include "Node.h"
template <class T>
class Queue
{
public :
Queue<T>(void );
~Queue<T>(void );
void queue(T* n);
T* dequeue(void );
int getQueueSize(void );
private :
Node<T>* headNode;
Node<T>* tailNode;
int size;
};
template <class T>
Queue<T>::Queue()
{
headNode = new Node<T>;
tailNode = new Node<T>;
headNode->setNext(tailNode);
tailNode->setPrev(headNode);
}
template <class T>
Queue<T>::~Queue(void )
{
delete headNode;
}
template <class T>
void Queue<T>::queue(T* n)
{
Node<T>* pNode = new Node<T>(n);
pNode->setNext(tailNode);
pNode->setPrev(tailNode->getPrev());
size++;
}
template <class T>
T* Queue<T>::dequeue(void )
{
if (size>0)
{
Node<T>* pNode = headNode->getNext();
headNode->setNext(pNode->getNext());
size--;
return pNode->getData();
}
}
Thanks all, great site!
Jan 15, 2010 at 5:24pm UTC
You forgot Node<T>:: on lines 32 and 38
Jan 15, 2010 at 5:27pm UTC
I knew it, rookie mistake :D
Knew I should have been a carpenter.
Jan 15, 2010 at 5:34pm UTC
Try to implement a binary tree then ;-)
Feb 12, 2010 at 12:34pm UTC
Thanks for the tip
Topic archived. No new replies allowed.