队列的实现
来源:互联网 发布:ubuntu怎么安装apache 编辑:程序博客网 时间:2024/06/16 04:45
//队列的实现#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
- 队列的实现:顺序队列
- 队列的实现:链式队列
- 队列----循环队列的实现
- 队列实现 队列的链式结构实现
- 堆栈,队列的实现
- 链队列的实现
- 队列的实现
- 队列的实现
- java 实现的队列
- 工作队列的实现
- 工作队列的实现
- 用链表实现的队列
- 队列的实现
- 队列的实现
- 队列的实现
- 队列的数组实现
- 循环队列的实现
- 数据结构 队列的实现
- Python笔记
- 你好,C++(8)如何表达那些始终保持不变的数据量?3.2.2 常量
- linux 各种后台运行
- 手机app性能测试
- MySQL删除群成员问题
- 队列的实现
- vs2010自动展开选中当前代码所在的文件位置的功能
- 警惕:23种榨干时间和精力的生命水蛭
- android基础笔记:服务(Service):基础知识及本地服务的实现
- Jmeter(二)-使用代理录制脚本
- 庄子·内篇·养生主第三
- redis 配置说明
- spring MVC集成Mybatis3
- gitHub使用简介