queue C++
#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++的更多相关文章
- [数据结构]——链表(list)、队列(queue)和栈(stack)
在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...
- Azure Queue Storage 基本用法 -- Azure Storage 之 Queue
Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob.Queue.File 和 Table. 笔者在<Azure File Storage 基 ...
- C++ std::queue
std::queue template <class T, class Container = deque<T> > class queue; FIFO queue queue ...
- 初识Message Queue之--基础篇
之前我在项目中要用到消息队列相关的技术时,一直让Redis兼职消息队列功能,一个偶然的机会接触到了MSMQ消息队列.秉着技术还是专业的好为原则,对MSMQ进行了学习,以下是我个人的学习笔记. 一.什么 ...
- 搭建高可用的rabbitmq集群 + Mirror Queue + 使用C#驱动连接
我们知道rabbitmq是一个专业的MQ产品,而且它也是一个严格遵守AMQP协议的玩意,但是要想骚,一定需要拿出高可用的东西出来,这不本篇就跟大家说 一下cluster的概念,rabbitmq是erl ...
- PriorityQueue和Queue的一种变体的实现
队列和优先队列是我们十分熟悉的数据结构.提供了所谓的“先进先出”功能,优先队列则按照某种规则“先进先出”.但是他们都没有提供:“固定大小的队列”和“固定大小的优先队列”的功能. 比如我们要实现:记录按 ...
- C#基础---Queue(队列)的应用
Queue队列,特性先进先出. 在一些项目中我们会遇到对一些数据的Check,如果数据不符合条件将会把不通过的信息返回到界面.但是对于有的数据可能会Check很多条件,如果一个数据一旦很多条件不 ...
- [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 ...
- [LeetCode] Implement Queue using Stacks 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- 源码之Queue
看源码可以把python看得更透,更懂,想必也是开发人员的必经之路. 现在有个任务,写个线程池.使用Queue就能写一个最简单的,下面就来学学Queue源码. 源码之Queue: class Queu ...
随机推荐
- 【AC大牛陈鸿的ACM总结贴】【ID AekdyCoin】人家当初也一样是菜鸟
acm总结帖_By AekdyCoin 各路大牛都在中国大陆的5个赛区结束以后纷纷发出了退役帖,总结帖,或功德圆满,或死不瞑目,而这也许又会造就明年的各种"炸尸"风波.为了考虑在发 ...
- android压缩解压zip文件
网上各种方法的收集: 1.上次写了个解压缩功能,但有局限性,比如压缩文件xx.zip 里包括子目录的情况下,执行上次解压缩的功能就不能实现我们想要的效果,于是在网上参考了一下java的解压缩功能.对上 ...
- Cocos2d-x游戏的场景结构布局
- 学IT技术 轻松高薪就业
如今的社会是大鱼吃小鱼的时代,假设你没有过强的技术,是非常难在社会上立足,更不要谈占有一席之地了.假设你想学一门好技术,那你想知道如今学什么技术好吗?我想这要看如今市场须要什么人才,缺什么人才.同一时 ...
- POJ 2250(最长公共子序列 变形)
Description In a few months the European Currency Union will become a reality. However, to join the ...
- Spring Uploading Files
1,在servlet-dispatcher.xml中添加代码 <bean id="multipartResolver" class="org.springframe ...
- 基于visual Studio2013解决面试题之0507字符串转整数
题目
- dos批量替换当前目录后缀名
有时候有些后缀名不满足条件,就需要进行批量的替换,如果人为的去替换,那么如果量少的话还好说,量多的话一个个去替换就太傻了,今天从网络上面查找了一些批量替换的dos命令,用起来还挺好用的,就直接把代码贴 ...
- 通过程序预览Office文档
我承认,标题是夸大了,就是为了吸引注意力.这里只有Word文档和Excel文档的预览代码. Word://此部分来源:http://princed.mblogger.cn/posts/11885.as ...
- SilkTest Q&A 9
Q81:我应该如何存取excel sheet里面的空字符串? A81:定制代码如下: 解决方案1: [-] if sText!=NULL [ ] Page.tfldName.SetText(sText ...