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.使用队列:方块舞的舞伴分配问题 前面我们提到过,经常用队列模拟排队的人.下面我们使用队列来模拟跳方块舞的人.当 男男女女来到舞池,他们按照自己的性别排成两队.当舞池中有地方空出来时,选两个队 列中 ...
随机推荐
- Linux sendmail 详解
Internet上最基本的服务,现在应该大部分人都有自己的邮箱吧,用的人多,但理解的人估计没多少,我自己以前也是常常用,但对其原理并不操心.今天就来操心下,进行个小总结 一.邮件服务的基本流程 ...
- Ubuntu中Qt+opencv图像显示
先抛出一个疑问,知道的希望留言解答下 (创建Application->Qt Console Application,代码与下面2.3一样,运行有时会显示图片,但大多数不显示,为什么?) 1.打开 ...
- ffmpeg的安装--opencv视频处理必备
安装yasm(ffmpeg必备)wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gztar xzvf yasm-1. ...
- attr 和 prop 区别
jquery 中 attr 和 prop 都表示 "属性",同样是属性为啥还要弄两个! attr 适用于自定义属性 如 定义一个懒加载用的src 栗子 <img class= ...
- NGINX----源码阅读一(main函数)
1.ngx_debug_init(); 初始化debug函数,一般为空. 2.ngx_strerror_init(): 将系统错误码+错误信息,以ngx_str_t数组保存. 3.ngx_get_op ...
- hibernate子查询
对于支持子查询的数据库,Hibernate支持在查询中使用子查询.一个子查询必须被圆括号包围起来(经常是SQL聚集函数的圆括号). 甚至相互关联的子查询(引用到外部查询中的别名的子查询)也是允许的. ...
- 网页数据抓取(B/S)
C# 抓取网页内容(转) 1.抓取一般内容 需要三个类:WebRequest.WebResponse.StreamReader 所需命名空间:System.Net.System.IO 核心代码: We ...
- Unity3D脚本使用:游戏对象访问
Unity3D中用到的组件 组件在js中对应的对象 使用如图: 注意:一个物体可以添加多个组件和多个js 同个物体上添加的js间引用
- (ASP.NET )去除字符串中的HTML标签
string strDoContent = "执行增加<a href="/AdminCX/Admin_CompanyDetail.aspx?CompanyGuid=cd8e1 ...
- cetos6 安装samba共享文件夹
yum方式安装 yum install samba 修改配置文件 vim /etc/samba/smb.conf [global] comment = global workgroup = QFpay ...