queue C++
来源:互联网 发布:做淘宝要注册公司吗 编辑:程序博客网 时间:2024/05/05 00:11
#include <iostream>using namespace std;class DequeEmptyException{public: DequeEmptyException() { cout << "Deque empty" << endl; }};// Each node in a doubly linked listclass Node{public: int data; Node* next; Node* prev;};class Deque{ private: Node* front; Node* rear; int count;public: Deque() { front = NULL; rear = NULL; count = 0; } void InsertFront(int element) { // Create a new node Node* tmp = new Node(); tmp->data = element; tmp->next = NULL; tmp->prev = NULL; if ( isEmpty() ) { // Add the first element front = rear = tmp; } else { // Prepend to the list and fix links tmp->next = front; front->prev = tmp; front = tmp; } count++; } int RemoveFront() { if ( isEmpty() ) { throw new DequeEmptyException(); } // Data in the front node int ret = front->data; // Delete the front node and fix the links Node* tmp = front; if ( front->next != NULL ) { front = front->next; front->prev = NULL; } else { front = NULL; } count--; delete tmp; return ret; } void InsertBack(int element) { // Create a new node Node* tmp = new Node(); tmp->data = element; tmp->next = NULL; tmp->prev = NULL; if ( isEmpty() ) { // Add the first element front = rear = tmp; } else { // Append to the list and fix links rear->next = tmp; tmp->prev = rear; rear = tmp; } count++; } int RemoveBack() { if ( isEmpty() ) { throw new DequeEmptyException(); } // Data in the rear node int ret = rear->data; // Delete the front node and fix the links Node* tmp = rear; if ( rear->prev != NULL ) { rear = rear->prev; rear->next = NULL; } else { rear = NULL; } count--; delete tmp; return ret; } int Front() { if ( isEmpty() ) throw new DequeEmptyException(); return front->data; } int Back() { if ( isEmpty() ) throw new DequeEmptyException(); return rear->data; } int Size() { return count; } bool isEmpty() { return count == 0 ? true : false; }};int main(){ // Stack behavior using a general dequeue Deque q; try { if ( q.isEmpty() ) { cout << "Deque is empty" << endl; } // Push elements q.InsertBack(100); q.InsertBack(200); q.InsertBack(300); // Size of queue cout << "Size of dequeue = " << q.Size() << endl; // Pop elements cout << q.RemoveBack() << endl; cout << q.RemoveBack() << endl; cout << q.RemoveBack() << endl; } catch (...) { cout << "Some exception occured" << endl; } // Queue behavior using a general dequeue Deque q1; try { if ( q1.isEmpty() ) { cout << "Deque is empty" << endl; } // Push elements q1.InsertBack(100); q1.InsertBack(200); q1.InsertBack(300); // Size of queue cout << "Size of dequeue = " << q1.Size() << endl; // Pop elements cout << q1.RemoveFront() << endl; cout << q1.RemoveFront() << endl; cout << q1.RemoveFront() << endl; } catch (...) { cout << "Some exception occured" << endl; }}
0 0
- queue.c
- queue.c
- c-queue?
- C/C++-----------00queue-------
- Queue-C++template1
- Queue-C++template
- Queue-c数组
- queue-c链式队列
- #include<queue> 【c++】
- c++queue容器介绍
- freeBSD queue.c--定时器
- [C++] Stack / queue / priority_queue
- QUEUE C语言实现
- #include<queue> 【c++】
- c++stack,queue
- c++queue容器介绍
- C++_队列(Queue)
- [c]队列queue
- RSA算法原理(一)
- Ubuntu下SSH设置
- LeetCode - Longest Substring with At Most Two Distinct Characters
- java编码规范(google)
- RSA算法原理(二)
- queue C++
- 加拿大支付方式介绍
- 第五章 电子标签
- 关于java对象初始化的一点有意思的东西
- C#中的匿名函数使用,类名<T>
- linux 各种相关下载
- S3C6410开发板linux下触摸屏驱动分析
- 毕设开发之三 gdb实现linux c++ debug
- LeetCode - Copy List with Random Pointer