#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. 通过DWR简化AJAX开发

    DWR(Direct Web Remoting)是一个WEB远程调用框架,采取了一个类似AJAX的新方法来动态生成基于JAVA类的JavaScript代码.这样WEB开发人员就可以在JavaScrip ...

  2. javascript真的是异步的吗?且看setTimeout的实现原理以及setTimeout(0)的使用场景

    在今天之前我一直以为setTimeout这个函数是异步的,无意中看到了一篇关于setTimeout的文章.发现自己曾经的认识全是错误的,赶紧总结下. 先看一段代码: var start = new D ...

  3. Eclipse用法和技巧二十三:查看JDK源码

    使用java开发,如果能阅读JDK的经典代码,对自己的水平提高是很有帮助的.笔者在实际工作中总结了两种阅读JDK源码的方式.第一种下载android源代码,直接在android源码代码中,这里的代码虽 ...

  4. openssl生成pem,密钥证书的创建

    使用OpenSSL生成证书 首先得安装OpenSSL软件包openssl,安装了这个软件包之后,我们可以做这些事情: o Creation of RSA, DH and DSA Key Paramet ...

  5. 读取中兴3G告警log告警文件到集合

    1.文件格式 ALARM_ID=102305_404205 EVENT_TIME=-- :: NOTIFICATION_TYPE= MANAGED_OBJECT_INSTANCE=NodeId=,Bs ...

  6. 基于visual Studio2013解决面试题之1310随机数

     题目

  7. jquery mobile左右滑动切换页面

    jquery mobile左右滑动切换页面 $(function() {$("body").bind('swiperight', function() {  $.mobile.ch ...

  8. Android 调整屏幕分辩率

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

  9. QT使用scrollarea显示图片,完美解决方案

    需求: 在界面上点击“显示图片”按钮,会调用scrollarea窗口显示图片,窗口大小能根据图片大小自动调整,但是最大为1024*768,图片过大就要有滚动条来显示 IDE环境: QT Creator ...

  10. “add measurements”(添加度量)菜单问题