2018-11-13-17:53:44

1.可增长循环队列

    队列是一种特殊的线性表,是一种先进先出(FIFO)的数据结构。它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作。进行插入操作的端称为队尾,进行删除操作的端称为队头。队列中没有元素时,称为空队列。

    下面是我用顺序结构实现的可增长循环队列,当队列元素的个数达到QueueSize-1时,队列的最大储存长度会定量增长。

 /*********************************************************
循环队列的基本操作的实现。
1.当队列元素的个数达到QueueSize-1时,队列的最大储存长度会定量增长。
mian函数操作:
1.输入一个字符串。
2.输入字符,如果字符为'+'或者'-'则进行入队或者出队操作。
3.每次入队出队后打印出现有的队列。
**********************************************************/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
using namespace std;
#define INITQUEUESIZE 100
#define QUEUEINCREAMENT 40
#define OverFlow -1
typedef char QElemtype;
typedef struct{
QElemtype*Elem;//存放队列元素数组的首地址
int Front,Rear;//储存队头和队尾的位置
int QueueSize;//队列当前的最大储存长度
}Queue;
bool Init_Queue(Queue&Q);
bool Queue_Empty(Queue Q);
bool Queue_Full(Queue Q);
QElemtype Get_Front(Queue Q);
bool Pop(Queue&Q,QElemtype&Elem);
bool Push(Queue&Q,QElemtype Elem);
void PrintQueue(Queue Q);
//main函数内所有数据均为测试数据,读者可根据自己测试方式自行调换 int main()
{
Queue Q;
Init_Queue(Q);
QElemtype Elem;
string s;
cin>>s;
for(int i=;i<s.length();i++){
char c=getchar();
if(c=='+'){
Push(Q,s[i]);
PrintQueue(Q);
}
else if(c=='-'){
if(!Queue_Empty(Q)){
Pop(Q,c);
PrintQueue(Q);
}
else
cout<<"Havn't Elem in this Queue"<<endl<<"Please repeate input:"<<endl;
}
else i--;//如果输入!'+'||!'-'则重新开始此步骤
}
PrintQueue(Q);
}
bool Init_Queue(Queue&Q){
Q.Elem=(QElemtype*)malloc(INITQUEUESIZE*sizeof(Queue));
if(!Q.Elem)
exit(OverFlow);
Q.Front=Q.Rear=;
Q.QueueSize=INITQUEUESIZE;
return true;
} bool Queue_Empty(Queue Q){
if(Q.Front==Q.Rear)
return true;
else
return false;
} int Size_Queen(Queue Q){
return (Q.Rear-Q.Front+Q.QueueSize)%Q.QueueSize;
} bool Queue_Full(Queue Q){
if((Q.Rear+)%Q.QueueSize==Q.Front)
return true;
else
return false;
} QElemtype Get_Front(Queue Q){
if(!Queue_Empty(Q)){
return Q.Elem[Q.Front];
}
} bool Pop(Queue&Q,QElemtype&Elem){
if(!Queue_Empty(Q)){
Elem=Q.Elem[Q.Front];
Q.Front=(Q.Front+)%Q.QueueSize;
return true;
}
return false;
} bool Push(Queue&Q,QElemtype Elem){
if(Queue_Full(Q)){
Q.Elem=(QElemtype*)realloc(Q.Elem,(Q.QueueSize+QUEUEINCREAMENT)*sizeof(QElemtype));
if(!Q.Elem)
exit(OverFlow);
Q.Rear=Q.Front+Q.QueueSize;
Q.QueueSize+=QUEUEINCREAMENT;
}
Q.Elem[Q.Rear]=Elem;
Q.Rear=(Q.Rear+)%Q.QueueSize;
}
void PrintQueue(Queue Q){
QElemtype Elem1,Elem2;
for(int i=Q.Front;i<Q.Rear;i++){
Elem1=Get_Front(Q);
Pop(Q,Elem2);
if(Elem1==Elem2)
cout<<Elem2<<'\t';
}
cout<<"The size of Q is "<<Q.QueueSize<<endl;
if(Queue_Full(Q)) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
} /****************************************
Author:CRUEL_KING
Time:2018/11/13
Program name:循环队列的基本操作的实现.cpp
****************************************/

2.STL之Queue队列

    C++中通常通过STL模板类定义队列,queue是一个容器适配器,具体而言,他是一个先进先出(FIFO)的数据结构。

      头文件:#include<queue>

      原型:templateclass T, class Container =std::deque<T> > class queue;

        如上,这对尖括号中有两个参数,第一个是T,表示队列中存放的数据的类型,比如int,double,或者结构体之类。

      第二个参数指明底层实现的容器类型,也就是指明这个栈的内部实现方式,比如vector,deque,list。如果不指明它,默认使用deque(双端队列)。

      队列的成员函数和基本操作:

        定义一个队列:   

 queue<char>q;//定义一个数据类型为char变量名为q的队列

        back()返回最后一个元素:

 q.back();//访问最后被压入队列的元素。

        empty()如果队列空则返回真:

 q.empty();//当队列空时,返回true。

        front()返回第一个元素:

q.front();//访问队列的第一个元素。

        pop()删除第一个元素:

 q.pop();// 弹出队列的第一个元素,注意,并不会返回被弹出元素的值。

        push()在末尾加入一个元素:

 q.push(x); //将x 接到队列的末端。

        size()返回队列中元素的个数:

 length=q.size();//将q队列的元素个数赋值给一个变量length。

STL-queue和循环队列基本操作的实现的更多相关文章

  1. [置顶] ※数据结构※→☆线性表结构(queue)☆============循环队列 顺序存储结构(queue circular sequence)(十)

    循环队列 为充分利用向量空间,克服"假溢出"现象的方法是:将向量空间想象为一个首尾相接的圆环,并称这种向量为循环向量.存储在其中的队列称为循环队列(Circular Queue). ...

  2. Leetcode622.Design Circular Queue设计循环队列

    设计你的循环队列实现. 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环.它也被称为"环形缓冲器". 循环队列的一个好处是 ...

  3. STL中的单向队列queue

    转载自:http://blog.csdn.net/morewindows/article/details/6950917 stl中的queue指单向队列,使用时,包含头文件<queue>. ...

  4. [Swift]LeetCode622. 设计循环队列 | Design Circular Queue

    Design your implementation of the circular queue. The circular queue is a linear data structure in w ...

  5. Java数据类型Stack栈、Queue队列、数组队列和循环队列的比较

    判断括号是否匹配:调用java本身 import java.util.Stack; public class Solution { public boolean isValid(String s){ ...

  6. java数据结构——队列、循环队列(Queue)

    每天进步一点点,坚持就是成功. 1.队列 /** * 人无完人,如有bug,还请斧正 * 继续学习Java数据结构————队列(列队) * 队列和栈一样,都是使用数组,但是队列多了一个队头,队头访问数 ...

  7. LeetCode 622:设计循环队列 Design Circular Queue

    LeetCode 622:设计循环队列 Design Circular Queue 首先来看看队列这种数据结构: 队列:先入先出的数据结构 在 FIFO 数据结构中,将首先处理添加到队列中的第一个元素 ...

  8. C#LeetCode刷题之#622-设计循环队列​​​​​​​(Design Circular Queue)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4126 访问. 设计你的循环队列实现. 循环队列是一种线性数据结构 ...

  9. STL - queue(队列)

    Queue简介 queue是队列容器,是一种"先进先出"的容器. queue是简单地装饰deque容器而成为另外的一种容器. #include <queue> queu ...

随机推荐

  1. beego orm 时间相差八小时

    使用beego框架,前端调用api插入一条数据到mysql,时间差了8个小时,fuck!!! 解决办法: 在db的url后面加上时区- dbDataSource = root:test@tcp(192 ...

  2. 获取本机内网、外网ip(C++)<转>

    基础知识 电脑在局域网内,通过网关/路由器连接到Internet则ip分为内网ip.外网ip.通过ipconfig得到的为局域网ip. 电脑直接拨号连接等,则本机通过ipconfig得到的就是外网ip ...

  3. js判断网页是否加载完毕

    1. document.onreadystatechange = function () { if(document.readyState=="complete") { docum ...

  4. google event

    一目了然,也不用多说了,随便记录下,内部实现基于观察者模式 TestEvent public class TestEvent { private final int message; public T ...

  5. UnicodeDecodeError: 'ascii' codec can't decode byte 0x9c in position 1: ordinal not in range(128)

    待研究: compressed_data = zlib.compress(json.dumps(data), 9) file_data = MySQLdb.escape_string(compress ...

  6. 如何使用JDBC连接Mysql数据库

    //java类名BaseDaopublic class BaseDao {    private Connection conn = null; // 声明Connection对象,Connectio ...

  7. 0 开发的准备工作一一虚拟机virturalbox

    https://www.virtualbox.org/wiki/Linux_Downloads官网下载linux版本 https://www.ubuntu.com/desktop/developers ...

  8. List of numerical libraries,Top Numerical Libraries For C#

    Top Numerical Libraries For C# AlgLib (http://alglib.net) ALGLIB is a numerical analysis and data pr ...

  9. JS计算滚动条的宽度

    1.此方法检验成功 function getScrollbarWidth() { var oP = document.createElement('p'), styles = { width: '10 ...

  10. unity 随笔

    转载 慕容小匹夫   从游戏脚本语言说起,剖析Mono所搭建的脚本基础      深入浅出聊优化:从Draw Calls到GC    谁偷了我的热更新?Mono,JIT,IOS     JS or C ...