//队列的实现
#ifndef MINIQUEUE_H
#define MINIQUEUE_H
#include <iostream>
#include <string>
#include <exception>
#include <new>
using std::string;
//自定义异常类
class underflowException : public std::exception {
public:
underflowException(const string& msg = ""):msgString(msg)
{}
virtual const char* what() const throw()
{
return msgString.c_str();
}
~underflowException() throw()
{}
protected:
string msgString;
};
class memoryAllocationError : public std::exception
{
public:
memoryAllocationError(const string& msg = ""):msgString(msg)
{}
virtual const char* what() const throw()
{
return msgString.c_str();
}
~ memoryAllocationError() throw()
{}
protected:
string msgString;
};
template <class T>
struct Node
{
T data;
Node<T> *next;
Node():next(NULL)
{}
Node(const T& n = T(), Node<T> *ptr = NULL): data(n), next(ptr)
{}
};
template <class T>
class MiniQueue
{
public:
//默认构造函数
MiniQueue():first(NULL),last(NULL),length(0)
{}
//析构函数
~MiniQueue()
{
clear();
}
//拷贝构造函数
MiniQueue(const MiniQueue<T>& rhs);
//重载复制操作符operator=
MiniQueue<T> & operator=( const MiniQueue<T>& rhs);
bool empty() const
{
return length == 0;
}
size_t size() const
{
return length;
}
void push(const T& val);
void pop();
T & front();
const T& front() const;
T& back();
const T& back() const;
private:
Node<T> *first; //指向队首第一个元素
Node<T> *last ; //指向对尾最后一个元素
int length;
void copy(const MiniQueue<T>& rhs);
void clear();
Node<T>* getNode( const T& val);
};
template <class T>
Node<T>* MiniQueue<T>::getNode(const T& val)
{
Node<T> *newNode = new Node<T>(val, NULL );
if (newNode == NULL )
throw memoryAllocationError("MiniQueue: getNode() memory allocation failure");
return newNode;
}
template <class T>
void MiniQueue<T>::copy(const MiniQueue<T>& rhs)
{
Node<T> *p = rhs.first; //p用于遍历被拷贝的队列
Node<T> *newNode;
first = last = NULL;
if( p != NULL )
{
first = last = new Node<T>( p->data );
p = p->next;
while ( p != NULL)
{
newNode = getNode( p->data );
last->next = newNode;
last = newNode;
p = p->next;
}
}
// the size of the new list is the size of rhs
length = rhs.length;
}
template <class T>
MiniQueue<T> & MiniQueue<T>::operator=( const MiniQueue<T>& rhs)
{
if ( *this != rhs )
{
clear();
copy(rhs);
}
return *this;
}
template <class T>
void MiniQueue<T>::clear()
{
Node<T> *curr = first;
Node<T> * p;
while(curr != NULL )
{
p = curr;
curr = curr->next;
delete p;
}
first = last = NULL;
length = 0;
}
template <class T>
void MiniQueue<T>::push(const T& val)
{
Node<T> *newNode = getNode(val);
if (first == NULL )
first = last = newNode;
else
{
last->next = newNode;
last = newNode;
}
length++;
}
template <class T>
void MiniQueue<T>::pop()
{
Node<T> *p = first;
//如果队列为空,产生异常
if (empty())
throw underflowException("MiniQuee pop():empty queue.");
first = first->next;
if ( first == NULL)
last = NULL;
delete p;
length--;
}
template <class T>
T& MiniQueue<T>::front()
{
if(length == 0)
throw underflowException("MiniQuee pop():empty queue.");
return first->data;
}
template <class T>
const T& MiniQueue<T>::front() const
{
if(length == 0)
throw underflowException("MiniQuee pop():empty queue.");
return first->data;
}
template <class T>
T& MiniQueue<T>::back()
{
if(length == 0)
throw underflowException("MiniQuee pop():empty queue.");
return last->data;
}
template <class T>
const T& MiniQueue<T>::back() const
{
if(length == 0)
throw underflowException("MiniQuee pop():empty queue.");
return first->data;
}
#endif //MINIQUEUE_H
队列的实现
最新推荐文章于 2018-10-31 17:02:16 发布