#include <iostream>
using namespace std; class DequeEmptyException
{
public:
DequeEmptyException()
{
cout << "Deque empty" << endl;
}
}; // Each node in a doubly linked list
class 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;
}
}

queue C++的更多相关文章

  1. [数据结构]——链表(list)、队列(queue)和栈(stack)

    在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...

  2. Azure Queue Storage 基本用法 -- Azure Storage 之 Queue

    Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob.Queue.File 和 Table. 笔者在<Azure File Storage 基 ...

  3. C++ std::queue

    std::queue template <class T, class Container = deque<T> > class queue; FIFO queue queue ...

  4. 初识Message Queue之--基础篇

    之前我在项目中要用到消息队列相关的技术时,一直让Redis兼职消息队列功能,一个偶然的机会接触到了MSMQ消息队列.秉着技术还是专业的好为原则,对MSMQ进行了学习,以下是我个人的学习笔记. 一.什么 ...

  5. 搭建高可用的rabbitmq集群 + Mirror Queue + 使用C#驱动连接

    我们知道rabbitmq是一个专业的MQ产品,而且它也是一个严格遵守AMQP协议的玩意,但是要想骚,一定需要拿出高可用的东西出来,这不本篇就跟大家说 一下cluster的概念,rabbitmq是erl ...

  6. PriorityQueue和Queue的一种变体的实现

    队列和优先队列是我们十分熟悉的数据结构.提供了所谓的“先进先出”功能,优先队列则按照某种规则“先进先出”.但是他们都没有提供:“固定大小的队列”和“固定大小的优先队列”的功能. 比如我们要实现:记录按 ...

  7. C#基础---Queue(队列)的应用

       Queue队列,特性先进先出. 在一些项目中我们会遇到对一些数据的Check,如果数据不符合条件将会把不通过的信息返回到界面.但是对于有的数据可能会Check很多条件,如果一个数据一旦很多条件不 ...

  8. [LeetCode] Queue Reconstruction by Height 根据高度重建队列

    Suppose you have a random list of people standing in a queue. Each person is described by a pair of ...

  9. [LeetCode] Implement Queue using Stacks 用栈来实现队列

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  10. 源码之Queue

    看源码可以把python看得更透,更懂,想必也是开发人员的必经之路. 现在有个任务,写个线程池.使用Queue就能写一个最简单的,下面就来学学Queue源码. 源码之Queue: class Queu ...

随机推荐

  1. poj 3258 River Hopscotch 【二分】

    题目真是不好读,大意例如以下(知道题意就非常好解了) 大致题意: 一条河长度为 L,河的起点(Start)和终点(End)分别有2块石头,S到E的距离就是L. 河中有n块石头,每块石头到S都有唯一的距 ...

  2. wireshark 抓包分析 TCPIP协议的握手

    wireshark 抓包分析 TCPIP协议的握手 原网址:http://www.cnblogs.com/TankXiao/archive/2012/10/10/2711777.html 之前写过一篇 ...

  3. C#面向对象2 静态类、静态成员的理解

    理解:静态成员属于类所有,为各个类的实例所公用,与实例无关,需要全局共享的属性或者方法定义成静态的 C#静态成员:  1.静态成员属于类所有,故用类名调用,非静态成员属于类的实例所有,用实例名调用  ...

  4. DELPHI语法基础学习笔记-Windows 句柄、回调函数、函数重载等(Delphi中很少需要直接使用句柄,因为句柄藏在窗体、 位图及其他Delphi 对象的内部)

    函数重载重载的思想很简单:编译器允许你用同一名字定义多个函数或过程,只要它们所带的参数不同.实际上,编译器是通过检测参数来确定需要调用的例程.下面是从VCL 的数学单元(Math Unit)中摘录的一 ...

  5. CEGUI 输入法窗口实现

    游戏中经常要输入汉字,但当我们游戏没有自己实现输入法窗口时,windows会使用用户安装的输入法,但这个输入法窗口只会显示在游戏窗口外头,而且当我们游戏全屏时(真全屏,不是那种窗口式的假全屏),屏幕上 ...

  6. C++要点

           以下的这些要点是对全部的C++程序猿都适用的.我之所以说它们是最重要的,是由于这些要点中提到的是你通常在C++书中或站点上无法找到的.如:指向成员的指针,这是很多资料中都不愿提到的地方, ...

  7. Mac 安装配置启动Tomcat

    Tomcat Mac 下的安装: TomCat 下载地址,例如: http://tomcat.apache.org/download-70.cgi 在Mac 上下载的时候,下载tar.gz包 下载完成 ...

  8. 调用QQ截图

    var SHExecInfo: SHELLEXECUTEINFO; begin //截图前隐藏主程序窗口 Form1.Hide; //等待截图执行完成 SHExecInfo.cbSize := siz ...

  9. h和.cpp文件的区别

    关于头文件和源文件的分别 首先,我们可以将所有东西都放在一个.cpp文件内. 然后编译器就将这个.cpp编译成.obj,obj是什么东西? 就是编译单元了.一个程序,可以由一个编译单元组成, 也可以有 ...

  10. CSS中的!important属性用法

    关于CSS的运用技巧有很多, 今天主要探讨一下CSS中 !important 这个属性的用法.在CSS的使用中,遇到最多的问题就是不同浏览器之间的兼容问题. 由于IE并不严格执行W3C标准, 而又几乎 ...