队列的实现

//队列的实现
#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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值