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.使用队列:方块舞的舞伴分配问题 前面我们提到过,经常用队列模拟排队的人.下面我们使用队列来模拟跳方块舞的人.当 男男女女来到舞池,他们按照自己的性别排成两队.当舞池中有地方空出来时,选两个队 列中 ...
随机推荐
- 自己通过centos6.5配置NFS 成功后的笔记,希望对需要的人有点点帮助吧!
环境介绍: 服务器:centos 172.16.250.170 客户端:centos 172.16.250.172 先用rpm -qa ...
- gc学习(转)
一.GC特性以及各种GC的选择 1.垃圾回收器的特性 2.对垃圾回收器的选择 2.1 连续 VS. 并行 2.2 并发 VS. stop-the-world 2.3 压缩 VS. 不压缩 VS. 复制 ...
- Duilib使用wke显示echarts
不得不说wke是个简洁好用的浏览器内核.网上很多大神已经把wke嵌入到duilib中了,先感谢他们辛勤的工作.这里通过wke吧C++的数据在ECharts上美观的显示出来.借鉴前人,将ECharts进 ...
- Java 彩色图转灰度图
1. 方法1 BufferedImage grayImage = new BufferedImage(width, height, colorImage.TYPE_BYTE_GRAY); Graphi ...
- "The Application was unable to start correctly (0xc000007b). Click OK to close the application"
我有时将MFC编译成64位并运行,就会报这个错误. 后来查找原因,就在于系统中使用了错误的dll.比如这个程序要使用64位的dll,而你拷贝进去的是同名的32位dll.解决方法就是放置正确的dll. ...
- Java中的Class类
Class 类是在Java语言中定义一个特定类的实现.一个类的定义包含成员变量,成员方法,还有这个类实现的接口,以及这个类的父类.Class类的对象用于表示当前运行的 Java 应用程序中的类和接口. ...
- [UWP小白日记-2]SQLite数据库DOME
数据库说简单点就是增删改查,但是对新手来说也是要爆肝的.作为一个新手爆肝无数次啊, 血的教训啊现在UWP的教程又少,说多了都是泪.留下来免得以后又爆肝.还有:一定要写注释!一定要写注释!一定要写注释! ...
- dev Gridcontrol根据其cell里面的值显示不同颜色
要改变cell值得颜色 需要用到cellTemplate和convert <DataTemplate x:Key="PercentageCellColorTemplate"& ...
- shell获取系统时间
获取系统时间 date -d"yesterday" +"%F %H:%M:%S" #输出昨天这个时候的时间 date -d"tomorrow" ...
- php获取url字符串截取路径的文件名和扩展名
<?php //获取连接里边的id $url = 'http://www.rong123.com/cjbkscbsd/x_dfsdfs/24454_1_1.html'; function get ...