简单队列的实现(基于链表)
2014-03-04 17:04
417 查看
实现一个简单的队列,基于链表。
测试代码:
#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; } }
相关文章推荐
- 基于链表及环形队列的生产者与消费者简单实现
- 队列--基于简单循环数组实现(Java)
- Java 实现双向链表,基于双向链表实现双端队列
- 基于链表实现Java 自定义Queue队列
- linux网络编程二十一:利用SIGALRM信号,实现一个简单的基于升序链表的定时器
- 简单列表的实现(基于链表)
- 基于链表实现队列
- 基于Java关于链表的简单实现
- 单链表队列的简单实现
- 每日一省————链表之队列的简单实现
- 链表实现一个简单的队列
- java基于有序链表的优先级队列实现
- 算法导论第三版第十章 单链表实现栈和队列(思路简单清晰)
- 简单优先队列实现-基于最小堆
- 几种简单的数据结构的C++实现,树,图,链表,环形队列,栈等---栈篇
- 典型数据结构--基于链表的队列实现
- 基于redis+mysql+php的简单队列实现
- 基于链表实现一个简单通讯录功能
- 算法导论第三版第十章 单链表实现栈和队列(思路简单清晰)