#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. KVM虚拟机介绍

    一 KVM虚拟机简介 kernel-based Virtual Machine的简称,是一个开源的 系统虚拟化模块,自Linux 2.6.20之后集成在Linux的各个主要发行版本中.它使用Linux ...

  2. Unity Interface Serialization-Expose Interface field In Inspector

    Unity has some quirks about their inspector, so as a preface they are listed here: If you add a [Ser ...

  3. PHP - 使用 Pear 进行安装和卸载包

    安装: 首先运行到php根目录: 输入要安装的包文件名: 使用语法: pear install 要安装包的名称 回车确认: 如果没有其他意外,显示安装成功. 查看安装的包的信息: 语句: pear i ...

  4. BZOJ 1342: [Baltic2007]Sound静音问题( 单调队列 )

    一开始写了个RMQ然后就T了... 好吧正解是单调队列, 维护两个单调队列... ----------------------------------------------------------- ...

  5. Wireshark安装、简单使用、过滤器简介

    1.简介 Wireshark是一款非常著名的网络嗅探器,它的前身是Ethereal.Wireshark是一款免费的软件,只需要从官网下根据不同的系统(window,linux等)下载其对应的安装文件即 ...

  6. Oracle多表的简单查询

    Oracle多表的简单查询 .多表查询 多表查询是指基于两个和两个以上的表或是视图的查询. 问题:显示雇员名,雇员工资及所在部门的名字[笛卡尔集]? select t.ename,t.sal,t1.d ...

  7. Android 调整屏幕分辩率

    Android 可设置为随着窗口大小调整缩放比例及设定fixed的窗口大小. 对于surface的控制在SurfaceHolder类中进行 而Android 屏幕分辩率中已经有一个类DisplayMe ...

  8. linux学习之四---gdb调试

    在Linux应用程序开发中,最经常使用的调试器是gdb. 一.启动和退出gdb gdb调试的对象是可运行文件,而不是程序的源码.假设要使一个可运行文件能够被gdb调试,那么使用编译器gcc编译时须要增 ...

  9. makefile 必知必会

    Makefile 必知必会 Makefile的根本任务是根据规则生成目标文件. 规则 一条规则包含三个:目标文件,目标文件依赖的文件,更新(或生成)目标文件的命令. 规则: <目标文件>: ...

  10. QT中.pro文件的写法

    QT中.pro文件的写法   qmake 变量 含义 #xxxx 注释, 从“#”开始,到这一行结束 SOURCES 指定源文件 SOURCES = *.cpp 对于多源文件,可用空格分开 SOURC ...