1. Definiation

What is a queue?

A queue is a list. With a queue, inseration is done at one end (known as rear) whereas deletion is performed at the other end (known as front).

2. Operations

指针对列

无法自定义队长

// array queue
#include<iostream>
using namespace std; struct Node
{
int data;
}; class Queue
{
public: //初始化
Queue()
{
front = -1;
rear = -1;
} //判断是否为空
bool isEmpty()
{
if (rear == front)
return true;
return false;
} //判队满
bool isFull()
{
if (rear - front == 10)
return true;
return false;
} //入队列
void EnQueue(int item)
{
if (!isFull())
{
rear += 1;
node[rear].data = item;
}
else
{
cout << "Full" << endl;
}
} //出队列
void DiQueue()
{
if (!isEmpty())
{
front += 1;
}
else
{
cout << "Empty" << endl;
}
} //取队头
int GetHead()
{
if (!isEmpty())
{
int temp = node[front+1].data;
return temp;
}
else
{
cout << "Empty" << endl;
return 0;
}
} //得队长
int GetSize()
{
return rear - front;
} private:
int front; //队头指针总是指向队头元素的前一个位置
int rear;
Node node[10];
};

可自定义队长

//自己动手写queue
//array queue
template<typename Object>
struct Node
{
Object data;
}; template<typename Object>
class Queue
{
public:
//初始化
Queue(int m)
{
init(m);
} Queue&operator=(Queue&q)
{
if (this == &q)
return *this;
clear();
for (int i = 0; i < q.curSize; i++)
this->EnQueue(q.list[i].data);
return *this;
} Queue(Queue&q)
{
init(q.maxsize);
*this = q;
} //删除队列
~Queue()
{
delete list;
} //判断是否为空
bool isEmpty()
{
if (curSize == 0)
return true;
return false;
} //判队满
bool isFull()
{
if (curSize == maxsize)
return true;
return false;
} //入队列
void EnQueue(int item)
{
if (!isFull())
{
rear += 1;
list[rear].data = item;
curSize++;
}
else
{
cout << "Full" << endl;
}
} //出队列
void DiQueue()
{
if (!isEmpty())
{
front += 1;
curSize--;
}
else
{
cout << "Empty" << endl;
}
} //取队头
int GetHead()
{
if (!isEmpty())
{
int temp = list[front + 1].data;
return temp;
}
else
{
cout << "Empty" << endl;
return 0;
}
} //得队长
int GetSize()
{
return curSize;
} //清空队列
void clear()
{
for (int i = 0; i<curSize; i++)
{
if (isEmpty())
{
return;
}
else
{
rear--;
}
}
curSize = 0;
} private:
int front;
int rear;
int maxsize;
int curSize;
Node<Object>* list; void init(int m)
{
front = -1;
rear = -1;
maxsize = m;
curSize = 0;
list = new Node<Object>[maxsize];
}
};

  

一般的数组队列会出现假溢出的问题,为了解决这个问题,我们可以利用循环队列

需要注意的是,你设计的队列大小与实际可以用到的队列大小是不一样的,总是-1

// circular queue
#include<iostream>
using namespace std; struct Node
{
int data;
}; class CirQueue
{
public: //初始化
CirQueue()
{
rear = 3;
front = 3;
} //判空
bool isEmpty()
{
if (front == rear)
return true;
return false;
} //判满,牺牲队列的一个空间来判满
bool isFull()
{
if ((rear + 1) % 4 == front)
return true;
return false;
} //入队
void EnQueue(int item)
{
if (!isFull())
{
rear = (rear + 1) % 4;
node[rear].data = item;
}
else
{
cout << "Full" << endl;
}
} //出队
void DiQueue()
{
if (!isEmpty())
{
front = (front + 1) % 4;
}
else
{
cout << "Empty" << endl;
}
} //取队头
int GetHead()
{
if (!isEmpty())
{
return node[(front+1)%4].data;
}
else return -1;
}
private:
int front;
int rear;
Node node[4];
};

其实不用游标来判断队空与队满就不用牺牲一个空间了

//自己动手写queue
//circular queue
template<typename Object>
struct Node
{
Object data; Node(const Object &d = Object()) :data(d){}
}; template<typename Object>
class CircularQueue
{
public:
//the big three
CircularQueue(int m)
{
init(m);
} CircularQueue(CircularQueue& cq)
{
init(cq.maxSize);
*this = cq;
} ~CircularQueue()
{
delete[] node;
} CircularQueue& operator=(CircularQueue &cq)
{
if (this == &cq)
return *this;
clear();
for (int i = 0; i<cq.curSize; i++)
this->EnQueue(cq.node[i].data);
return *this;
} //判空
bool isEmpty()
{
return curSize == 0;
} //判满
bool isFull()
{
return curSize == maxSize;
} //入队
void EnQueue(Object item)
{
if (!isFull())
{
curSize++;
}
rear = (rear + 1) % maxSize;
node[rear].data = item;
} //出队
void DIQueue()
{
if (!isEmpty())
{
head = (head + 1) % maxSize;
curSize--;
}
} //取队头
Object GetHead()
{
return node[(head + 1) % maxSize].data;
} //得队长
int GetSize()
{
return curSize;
} //清空队列
void clear()
{
head = maxSize - 1;
rear = maxSize - 1;
curSize = 0;
} private:
int head;
int rear;
int maxSize;
int curSize;
Node<Object>*node; void init(int m)
{
head = m - 1;
rear = m - 1;
maxSize = m;
curSize = 0;
node = new Node<Object>[maxSize];
}
};

  

  

链表队列

就无什么假溢出的问题了

template<typename Type>
struct Node
{
Type data;
Node*next;
Node*prev; Node(const Type& d = Type(), Node*p = NULL, Node *n = NULL) :data(d), prev(p), next(n){}
}; template<typename Type>
class Queue
{
public:
Queue()
{
init();
} Queue(Queue& q)
{
init();
*this = q;
} ~Queue()
{
clear();
delete head;
delete tail;
} const Queue& operator= (const Queue& q)
{
if (this == &q)
return *this;
clear();
Node<Type>*p = q.head->next;
while (p->next != q.tail)
{
this.EnQueue(p->data);
p = p->next;
}
return *this;*/
} bool IsEmpty()
{
return size == 0;
} void EnQueue(Type item)
{
Node<Type>*p = new Node<Type>;
p->data = item;
p->prev = tail->prev;
tail->prev->next = p;
tail->prev = p;
p->next = tail;
size++;
} void DIQueue()
{
if (!IsEmpty())
{
Node<Type>*p = head->next;
head->next = p->next;
p->next->prev = head;
delete p;
size--;
}
} Type GetHead()
{
return (head->next->data);
} void clear()
{
while (head->next != tail)
{
DIQueue();
}
size = 0;
} int GetSize()
{
return size;
} private:
Node<Type>*tail;
Node<Type>*head;
int size; void init()
{
head = new Node<Type>;
tail = new Node<Type>;
head->next = tail;
tail->prev = head;
size = 0;
}
};

 

Queues 队列的更多相关文章

  1. C++ Queues(队列)

    C++ Queues(队列) C++队列是一种容器适配器,它给予程序员一种先进先出(FIFO)的数据结构.1.back() 返回一个引用,指向最后一个元素2.empty() 如果队列空则返回真3.fr ...

  2. 225 Implement Stack using Queues 队列实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack.pop ...

  3. C++ Priority Queues(优先队列) and C++ Queues(队列)

    C++优先队列类似队列, 但是在这个数据结构中的元素按照一定的断言排列有序. empty() 如果优先队列为空,则返回真 pop() 删除第一个元素 push() 加入一个元素 size() 返回优先 ...

  4. 3.Queues(队列)

    一.概述 C++队列是一种容器适配器,它给予程序员一种先进先出(FIFO)的数据结构,与stack刚好相反. 二.常用API back() 返回最后一个元素 empty() 如果队列空则返回真 fro ...

  5. Laravel Queues 队列应用实战

    队列,顾名思义,排着队等着做事情.在生活场景中,凡是排队的人,都是带有目的性的.要完成某件事情,才去排队的,要不没有谁会闲到排队玩儿.而在软件应用层面,队列是什么,队列有什么优点,我们什么时候需要用队 ...

  6. 队列理论和队列网络模型 queueing theory and queueing network model

    1队列理论 1.1队列在生活中随处可见,例如排队买票,排队打饭,排队做地铁等等.那将诸如此类的队列抽象一下,可归纳为一下5要术: 到达过程arrival process 服务时间的分布 service ...

  7. c++ STL:队列queue、优先队列priority queue 的使用

    说明:本文全文转载而来,原文链接:http://www.cppblog.com/wanghaiguang/archive/2012/06/05/177644.html C++ Queues(队列) C ...

  8. EXCHANGE 2013 队列

    每当咱在Exchange里查看队列的时候,我们会看到队列分成好几个组,每个邮箱数据库都有自己的目标队列,DAG.AD站点也是,AD林也是一个队列,最后最多的就是外部SMTP域队列. 当传输服务处理队列 ...

  9. 数据结构与算法JavaScript描述——使用队列

    1.使用队列:方块舞的舞伴分配问题 前面我们提到过,经常用队列模拟排队的人.下面我们使用队列来模拟跳方块舞的人.当 男男女女来到舞池,他们按照自己的性别排成两队.当舞池中有地方空出来时,选两个队 列中 ...

随机推荐

  1. PDF转换成Txt

    我的弱智想法是所有能转换成PDF的文件,就都用PDF预览,上传成功后开启一个线程把文档转换成PDF,PDF再转换成txt. 目的是把txt插入索引进行全文检索. 调用的时候 string filePa ...

  2. vue+webpack构建项目

    概述 -- 项目中会用到的插件 vue-router vue-resource 打包工具 webpack 依赖环境 node.js start 安装vue开发的模板 # 全局安装 vue-cli $ ...

  3. java系统参数

    package com.test; import java.sql.SQLException; import java.util.Properties; import com.mchange.v2.c ...

  4. PNG文件转png8

    png8比普通png图片会小很多,所以在开发中为了是图片加载速度更快我们可以把Png图片都转成png8 首先添加  ImageProcessor  包 private byte[] ConvertTo ...

  5. vultr新用户注册享受50美元优惠码,长期有效

    vultr vps服务器,我用了三年多,购买了几十台vps,性价比非常高. 近期,vutlr推出了最新优惠码DOMORE长期有效,新用户注册账号时候,可在付款方式界面输入这个优惠码,享受50美元余额, ...

  6. 初步学习nodejs,业余用node写个一个自动创建目录和文件的小脚本,希望对需要的人有所帮助

    初步学习nodejs,业余用node写个一个自动创建目录和文件的小脚本,希望对需要的人有所帮助,如果有bug或者更好的优化方案,也请批评与指正,谢谢,代码如下: var fs = require('f ...

  7. sublime文字处理技巧

    1.针对多行文本去除重复行,而不改变文本原来的顺序,即不通过排序的方式移除重复行 安装ShellCommand插件,全选文本,ctrl+alt+|调出shell执行终端,输入 awk '!x[$0]+ ...

  8. 关于float和position

      在div 块级元素中,一般我们的div块都是流式的,如果你设定一个div,接下来的div就会另起行,也就是块级元素的定义 但是一般排版不是这样的,最典型的应该就是这种布局了,那么中间的那三个div ...

  9. vue实例生命周期

    实例生命周期 var vm = new Vue({ data: { a: 1 }, created: function () { // `this` 指向 vm 实例 console.log('a i ...

  10. cocos2d-html5 之重要概念

    1,CCDirector:导演: 2,CCCamera: 摄像机:细到每个节点都要用摄像机,例如节点发生放大,缩小,旋转的时候,要继承摄像机,让其重新渲染: 3,CCScene:场景,拍电影时的一段剪 ...