STL-queue和循环队列基本操作的实现

时间:2023-03-08 20:01:46
STL-queue和循环队列基本操作的实现

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。