简单队列的实现(基于链表)
来源:互联网 发布:windows程序设计中文版 编辑:程序博客网 时间:2024/06/07 03:05
实现一个简单的队列,基于链表。
#ifndef _LINKEDQUEUE_#define _LINKEDQUEUE_#include <iostream>#include <stdexcept>using namespace std;template <typename T>class LinkedQueue{public:LinkedQueue() : _size(0),_head(NULL),_tail(NULL) {}~LinkedQueue() { Clear(); }LinkedQueue(const LinkedQueue& rhs) : _size(0),_head(NULL),_tail(NULL){operator=(rhs);}const LinkedQueue& operator= (const LinkedQueue& rhs){if( this != &rhs ){Clear();LinkedQueueNode* temp = rhs._head;while( temp != NULL){this->Enqueue(temp->data);temp = temp->next;}}return *this;}int Size() const { return _size; }bool IsEmpty() const { return _size == 0; }void Clear(){_size = 0;LinkedQueueNode* temp = _head;while(temp != NULL){_head = _head->next;delete temp;temp = _head;}_head = NULL;_tail = NULL;}void Print() const{cout<< "Size=" << _size <<endl;LinkedQueueNode* temp = _head;while(temp != NULL){cout << temp->data << ",";temp = temp->next;}cout << endl;}void Enqueue(const T& value){if(_size == 0){_tail = new LinkedQueueNode(value,NULL);_head = _tail;}else{_tail->next = new LinkedQueueNode(value,NULL);_tail = _tail->next;}++_size;}T Dequeue(){if(IsEmpty())throw logic_error("Queue is empty");LinkedQueueNode* temp = _head;T value = temp->data;if(_size == 1){_head = _tail = NULL;}else{_head = _head->next;}--_size;delete temp;return value;}T& Peek() const{if(IsEmpty())throw logic_error("Queue is empty");return _head->data;}private:class LinkedQueueNode{public:T data;LinkedQueueNode* next;LinkedQueueNode(const T& v, LinkedQueueNode* n=NULL) : data(v),next(n) {}};private:int _size;LinkedQueueNode* _head;LinkedQueueNode* _tail;};#endif
测试代码:
#include "LinkedQueue.cpp"void LinkedQueueTest1();void Test( void (*fp)() );int main(int argc, char** argv){Test(LinkedQueueTest1);return 0;}void LinkedQueueTest1(){LinkedQueue<int> q;q.Print();q.Enqueue(1);q.Enqueue(2);q.Enqueue(3);q.Enqueue(4);q.Enqueue(5);q.Print();cout << "Peek=" << q.Peek() << endl;cout << "Dequeue=" << q.Dequeue() << endl;q.Print();q.Enqueue(6);q.Print();q.Dequeue();q.Dequeue();q.Print();LinkedQueue<int> q1 = q;cout<< "q1 content" << endl;q1.Print();cout << q1.Dequeue() << endl;cout << q1.Dequeue() << endl;cout << q1.Dequeue() << endl;q1.Print();cout << "q content" << endl;q.Print();}void Test( void (*fp)() ){try{fp();}catch(out_of_range e){cout<< "Catch Exception:" << e.what() << endl;}catch(overflow_error e){cout<< "Catch Exception:" << e.what() << endl;}catch(logic_error e){cout<< "Catch Exception:" << e.what() << endl;}}
0 0
- 简单队列的实现(基于链表)
- 简单队列的实现(基于数组)
- 基于js的简单队列实现
- 基于链表的队列实现
- 基于链表的队列实现
- 基于链表及环形队列的生产者与消费者简单实现
- 基于链表实现队列
- 链队列的简单实现
- 基于C++11的阻塞队列简单实现
- 基于redis+mysql+php的简单队列实现
- 典型数据结构--基于链表的队列实现
- 基于数组和链表的队列实现
- 【数据结构练习】基于链表结构实现的队列
- java基于有序链表的优先级队列实现
- 队列的简单实现
- 队列的简单实现
- 队列的简单实现
- 队列的简单实现
- 中国余数定理
- 【IE6 Bug】IE6的怪异解析模式
- 存储那些事儿(三):OpenStack的块存储Cinder与商业存储的融合
- Win7下Python3.3环境安装paramiko模块
- opencv学习笔记4-sobel算子
- 简单队列的实现(基于链表)
- FFT算法的完整DSP实现
- 《java解惑》读书笔记8——更多类谜题
- Lucene入门
- Go语言实现Fibonacci数列的两个算法(使用递归和不使用递归)
- cocos2d-x 加载第三方.so文件
- 关于“构造函数不能有返回类型”的解决办法
- MongoDB数据库安装
- Tomcat