//
// SimpleQueue.cpp
//
// Demonstrates a Simple Queue with templates
// Modified 11/15/2010
#include
#include
#define TRUE true
#define FALSE false
#define NULL 0
template class Node
{
public:
Type type;
Node(const Type& type,Node* previous,Node* next)
{
this->type=type;
this->previous=previous;
this->next=next;
}
Node* previous;
Node* next;
};
// the class with the template.
template class Queue
{
public:
Queue();
Type& font();
const Type &front() const;
void push(const Type&);// add element to back of Queue
void pop(); // remove element from head of queue.
bool empty() const; // true if no elements in the Queue.
private:
Node * first;
Node * last;
}; // end class Queue.
// implements the methods in the class Queue
template
Queue::Queue()
{
first=NULL;
last=NULL;
}
template
const Type& Queue::front() const
{
// we are not going to check for the
// exceptional conditions that can be arise in this code.
return last->type;
}
template
void Queue::push(const Type& type)
{
if(first==NULL)
{
// create a new node
Node * new_node =new Node(type,NULL,NULL);
first=new_node;
last=new_node;
return;
}else{
Node* new_node=new Node(type,NULL,first);
first->previous=new_node;
first=new_node;
return;
}
}
template
void Queue::pop()
{
if(last==NULL)
return ; // DO NOTHING
else
{
Node * tmp_node=last;
last=last->previous;
delete tmp_node;
}
}
template
bool Queue::empty() const
{
if(last==NULL)
return TRUE;
else
return FALSE;
}
// main method
int main(int argc,char** argv)
{
Queue integer_queue;
for(int i=0;i<100;i++)
integer_queue.push(i);
// now print the array;
for(int i=0;i<100;i++)
{
int front=integer_queue.front();
std::cout< integer_queue.pop();
}
return 0;
}
Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/_yuDy1JqDz4/12761