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. install plugin elasticsearch-analysis-ik

    1.github下载分词器插件(请各位下载自己elasticsearch相对应的版本,否则会有兼容性问题) https://github.com/medcl/elasticsearch-analysi ...

  2. Unity3D脚本使用:游戏对象访问

    Unity3D中用到的组件 组件在js中对应的对象 使用如图: 注意:一个物体可以添加多个组件和多个js 同个物体上添加的js间引用

  3. CSS 待解决问题

    记录一些待解决的问题 1. 关于 table 边框参差不齐问题 乍一看表头的边框有 间隙, 下边框也有点没有对其的问题. 解决方案:  给 table 添加: border-collapse: col ...

  4. Python学习笔记——基础篇【第五周】——正则表达式(re)

    目录 1.简介 2.字符匹配 1.简介:就其本质而言,正则表达式(或 RE)是一种小型的.高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现.正则表达式模式被编译 ...

  5. css3制作3d旋转相册

    此处只是记录,解析可见原文:http://www.cnblogs.com/skyblue-li/p/6092799.html <!DOCTYPE html> <html xmlns= ...

  6. UNIX基础--进程和守护进程

    进程和守护进程 Processes and Daemons 进程(Processes) FreeBSD 是一个多任务操作系统. 这就意味着好像一次可以运行一个以上的程序. 每个占用一定时间运行的程序就 ...

  7. nginx构建https

    1.生成证书#openssl genrsa -des3 -out server.key 1024#openssl req -new -key server.key -out server.csr#op ...

  8. jquery里面的attr和css来设置轮播图竟然效果不一致

    /*封装$*/ // window.$=HTMLElement.prototype.$=function(selector){ // var elems=(this==window?document: ...

  9. js基础 2

  10. QT 报错:Project ERROR: Xcode not set up properly. You may need to confirm the license agreement by running /usr/bin/xcodebuild.

    1.打开终端,输入指令并按回车键: sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer 2.如果电脑设置有密码,终 ...