Queues 队列
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 队列的更多相关文章
- C++ Queues(队列)
C++ Queues(队列) C++队列是一种容器适配器,它给予程序员一种先进先出(FIFO)的数据结构.1.back() 返回一个引用,指向最后一个元素2.empty() 如果队列空则返回真3.fr ...
- 225 Implement Stack using Queues 队列实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack.pop ...
- C++ Priority Queues(优先队列) and C++ Queues(队列)
C++优先队列类似队列, 但是在这个数据结构中的元素按照一定的断言排列有序. empty() 如果优先队列为空,则返回真 pop() 删除第一个元素 push() 加入一个元素 size() 返回优先 ...
- 3.Queues(队列)
一.概述 C++队列是一种容器适配器,它给予程序员一种先进先出(FIFO)的数据结构,与stack刚好相反. 二.常用API back() 返回最后一个元素 empty() 如果队列空则返回真 fro ...
- Laravel Queues 队列应用实战
队列,顾名思义,排着队等着做事情.在生活场景中,凡是排队的人,都是带有目的性的.要完成某件事情,才去排队的,要不没有谁会闲到排队玩儿.而在软件应用层面,队列是什么,队列有什么优点,我们什么时候需要用队 ...
- 队列理论和队列网络模型 queueing theory and queueing network model
1队列理论 1.1队列在生活中随处可见,例如排队买票,排队打饭,排队做地铁等等.那将诸如此类的队列抽象一下,可归纳为一下5要术: 到达过程arrival process 服务时间的分布 service ...
- c++ STL:队列queue、优先队列priority queue 的使用
说明:本文全文转载而来,原文链接:http://www.cppblog.com/wanghaiguang/archive/2012/06/05/177644.html C++ Queues(队列) C ...
- EXCHANGE 2013 队列
每当咱在Exchange里查看队列的时候,我们会看到队列分成好几个组,每个邮箱数据库都有自己的目标队列,DAG.AD站点也是,AD林也是一个队列,最后最多的就是外部SMTP域队列. 当传输服务处理队列 ...
- 数据结构与算法JavaScript描述——使用队列
1.使用队列:方块舞的舞伴分配问题 前面我们提到过,经常用队列模拟排队的人.下面我们使用队列来模拟跳方块舞的人.当 男男女女来到舞池,他们按照自己的性别排成两队.当舞池中有地方空出来时,选两个队 列中 ...
随机推荐
- Leetcode easy
1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...
- js 中的 exec( )方法
JavaScript exec() 方法 JavaScript RegExp 对象 定义和用法 exec() 方法用于检索字符串中的正则表达式的匹配. 语法:RegExpObject.exec(str ...
- Tiny6410之重定位代码到SRAM+4096
重定位代码 两个不同的地址概念: 对于程序而言,需要理解两个地址,一个是程序当前所处的地址,即程序运行时所处的当前地址.二是程序应该位于的运行地址,即编译程序时所指定的程序的链接地址.在Tiny641 ...
- python绝技 — 用Scapy测试无线网卡的嗅探功能
代码 #!/usr/bin/python #--*--coding=utf-8--*-- from scapy.all import * def pktPrint(pkt): if pkt.hasla ...
- select与ajax结合
要实现的功能是,点击select输入框,数据库里面的数据会以option弹出. 这需要用到ajax异步连接数据库 下面贴出代码 先说明一下后台传递的数据是json,以map的形式传入的.后台代码很简单 ...
- Centos7 安装高版本php
1. 配置yum源 查看yum源的链接是不是有效的.可以参考此链接 https://webtatic.com/projects/yum-repository/ # rpm -Uvh http://f ...
- Python的加入!
今天有幸领略了Python的风采. 真是好清新>_< 赶紧尝试一下. 好酷. 以后会在项目中使用
- python 之文本搜索与替换文件中的文本
#!/usr/local/env python import os, sys nargs = len(sys.argv) if not 3 <= nargs <= 5: print &qu ...
- Tiny6410之LED裸机驱动
操作步骤: 第一步:查看开发板电路原理图 找到LED 的管脚所对应的寄存器 nLED_1 - GPK4 nLED_2 - GPK5 nLED_3 - GPK6 nLED_4 - GPK7 由原理图可知 ...
- PotPlayer播放器 莫尼卡汉化绿色版 V1.6.48089 32位
软件名称: PotPlayer播放器 莫尼卡汉化绿色版 软件语言: 简体中文 授权方式: 免费软件 运行环境: Win7 / Vista / Win2003 / WinXP 软件大小: 10.5MB ...