Code:
#ifndef NODE_H
#define NODE_H
#include <iostream>
#include "Square.h"
using namespace std;
template <class T> class Node;
template <class T> class Queue;
template< class T >
class Node{
public:
//**********************************************************
//Function :Default constructor
//PreCondition :None
//PostCondition:data row = col =0, next = NULL
//**********************************************************
Node();
//**********************************************************
//Function :Constructor
//PreCondition :None
//PostCondition:row and col set to value passed in, next = NULL
//**********************************************************
Node(const T&);
//**********************************************************
//Function :retrieves next pointer
//PreCondition :none
//PostCondition:next pointer is returned
//**********************************************************
Node< T > *GetNext();
//**********************************************************
//Function :sets next pointer
//PreCondition :none
//PostCondition:next = nodeptr
//**********************************************************
void SetNext(Node< T > *nodeptr);
//**********************************************************
//Function :retrieves data in node
//PreCondition :none
//PostCondition:class containing data is returned
//**********************************************************
const T& GetData();
private:
T m_data;
Node< T > *m_next;
friend class Queue<T>;
};
#include "Node.cpp"
#endif
//implementation
Code:
#ifndef NODE_CPP
#define NODE_CPP
#include "Node.h"
using namespace std;
//**********************************************************
//Function :Default constructor
//PreCondition :
//PostCondition:data holds two int = 0, next = NULL
//**********************************************************
template< class T >
Node<T>::Node() : m_data()
{
m_next = NULL;
}
//**********************************************************
//Function :constructor
//PreCondition :None
//PostCondition:row = row column =row next = NULL
//**********************************************************
template< class T >
Node<T>::Node(const T& Node_Data): m_data(Node_Data)
{
m_next = NULL;
}
//**********************************************************
//Function :get the next pointer
//PreCondition :none
//PostCondition:returns the next pointer
//**********************************************************
template< class T >
Node< T>* Node<T>::GetNext()
{
return m_next;
}
//**********************************************************
//Function : sets next pointer
//PreCondition : none
//PostCondition:next = nodeptr
//**********************************************************
template< class T >
void Node<T>::SetNext(Node<T> *nodeptr)
{
m_next = nodeptr;
}
//**********************************************************
//Function :retrieves data
//PreCondition :none
//PostCondition:data of the square class is returned
//**********************************************************
template< class T >
const T& Node<T>::GetData()
{
return m_data;
}
#endif
//Queue Class
Code:
#ifndef QUEUE_CPP
#define QUEUE_CPP
#include "Queue.h"
#include <assert.h>
#include "Exception.h"
using namespace std;
//********************************************************
//Function : Constructor
//PreCondition :
//PostCondition:
//********************************************************
template< class T >
Queue<T>::Queue()
{
m_head = NULL;
m_tail = NULL;
}
//********************************************************
//Function : Destructor
//PreCondition :
//PostCondition: All things in queue would be destroyed
//********************************************************
template< class T >
Queue<T>::~Queue()
{
DestroyQueue();
m_head = NULL;
m_tail = NULL;
}
//********************************************************
//Function : copy constructor
//PreCondition : object is not empty
//PostCondition: object is copied into another object
//********************************************************
template< class T >
Queue<T>::Queue(const Queue<T>& A_Queue)
{
m_head = m_tail = NULL;
CopyQueue(A_Queue);
}
//********************************************************
//Function : assignment operator
//PreCondition : not attempting a self copy
//PostCondition: object is copied into another object
//********************************************************
template< class T >
Queue<T>& Queue<T>::operator= (const Queue<T>& A_Queue)
{
if(this != &A_Queue)
CopyQueue(A_Queue);
else
throw ExceptClass("Attempting a self copy");
return *this;
}
//********************************************************
//Function : check if Queue is empty
//PreCondition : none
//PostCondition: true or false is returned
//********************************************************
template< class T >
bool Queue<T>::IsEmpty()const
{
return (m_head == NULL);
}
//********************************************************
//Function : places item into queue
//PreCondition : item being put in is not empty row and col
// are between 0 and 9
//PostCondition: item is placed at the front of the queue
//********************************************************
template< class T >
void Queue<T>::Enqueue(const T& Queuedata)
{
Node<T> *newNode;
newNode = new Node<T>(Queuedata);
if(newNode == NULL)
throw ExceptClass("Attempting to Enqueue empty data");
if(m_head == NULL)
{
m_head = newNode;
m_tail = newNode;
}
else
{
m_tail->m_next = newNode;
m_tail = m_tail->m_next;
}
}
//********************************************************
//Function : deletes item in queue(first item)
//PreCondition : queue is not empty
//PostCondition: first item is deleted from queue
//********************************************************
template< class T >
void Queue<T>::Dequeue()
{
Node<T> *temp;
if(!IsEmpty())
{
temp = m_head;
m_head = m_head->m_next;
delete temp;
if(m_head == NULL)
m_tail = NULL;
}
else
throw ExceptClass("Queue is empty:: Nothing to remove");
}
//********************************************************
//Function : retrieves item in the front of the queue
//PreCondition : queue is not empty
//PostCondition: item is returned to caller
//*******************************************************
template< class T >
const T& Queue<T>::Front()
{
if( m_head == NULL)
throw ExceptClass("Queue is empty: Nothing to retrieve");
return m_head->m_data;
}
//********************************************************
//Function : retrieves item from the back of the queue
//PreCondition : queue is not empty
//PostCondition: item is returned to caller
//********************************************************
template< class T >
const T& Queue<T>::Back()
{
if (m_head == NULL)
throw ExceptClass("Queue is empty: Nothing to retrieve");
return m_tail->m_data;
}
//********************************************************
//Function : Destroys every item in a queue
//PreCondition : queue is not empty
//PostCondition: queue is destroyed
//********************************************************
template< class T >
void Queue<T>::DestroyQueue()
{
Node<T> *temp;
while(!IsEmpty())
{
temp = m_head;
m_head = m_head->m_next;
delete temp;
}
}
//********************************************************
//Function : copies item from one queue to another queue
//PreCondition : queue is not empty
//PostCondition: object is copied to another object
//********************************************************
template< class T >
void Queue<T>::CopyQueue(const Queue<T>& A_Queue)
{
if(m_head != NULL)
m_head = m_tail = NULL;
if(A_Queue.m_head == NULL)
m_head = m_tail = NULL;
else
{
Node<T> *temp;
temp = A_Queue.m_head;
m_head = new Node<T>;
m_head->m_data = temp->m_data;
m_head->m_next = NULL;
m_tail = m_head;
temp = temp->m_next;
while(temp!= NULL)
{
Node<T> *newNode = new Node<T>;
newNode->m_data = temp->m_data;
newNode->m_next = NULL;
m_tail->m_next = newNode;
m_tail = newNode;
temp = temp->m_next;
}
}
}
//*********************************************************************
//Function : Outputs item in the queue to the screen
//Precondition : queue is not empty
//PostCondition: item is sent to the screen
//*********************************************************************
template<class T>
ostream& operator<< (ostream& out, const Queue<T>& A_Queue)
{
Node<T> *temp = new Node<T>;
temp = A_Queue.m_head;
if(A_Queue.IsEmpty())
throw ExceptClass("Queue is Empty: Nothing to output.");
else
{
while(temp != NULL)
{
out<<"Items in queue are: "<<temp->GetData();
temp = temp->GetNext();
}
}
delete temp;
return out;
}
#endif