//双端队列,两边
using namespace std; typedef char ElemType; #include <time.h> #include <stdlib.h> #include <iostream> #define MaxSize 30 class DequeType{ public: int front; int back; int size; ElemType array[MaxSize]; public:/*构造函数,初始化成员变量*/ DequeType (){ front=back=size=0; } bool isEmpty(){//判断是否为空 if(0==size){ return true; }else { return false; } } bool isFull(){//判断是否满 if(MaxSize==size){ return true; }else{ return false; } } bool push_back(ElemType elem){//从后端插入 if(isFull()){ return false; } array[back]=elem; back=(back+1)%MaxSize;//循环 size++; return true; } bool pop_front(ElemType& elem){//从前端输出 if(isEmpty()){ return false; } elem=array[front]; front=(front+1)%MaxSize; size--; return true; } bool push_front(ElemType elem){//从前端输入 if(isFull()){ return false; } front=(front-1+MaxSize)%MaxSize;//循环队列 array[front]=elem; size++; return true; } bool pop_back(ElemType& elem){//从后端输出 if(isEmpty()){ return false; } back=(back-1+MaxSize)%MaxSize; elem=array[back]; size--; return true; } bool get_front(ElemType & elem){//从前端取数 if(isEmpty()){ return false; } elem=array[front]; return true; } bool get_back(ElemType& elem){//从后端取数 if(isEmpty()){ return false; } elem=array[(back-1+MaxSize)%MaxSize]; return true; } int length(){//求队列长度 return size; } void traverse(){//全部输出 if(isEmpty()){ cout<<"is empty"<<endl; return ; } int temp=front; while(temp!=back){ cout<<array[temp]<<" "; temp=(temp+1)%MaxSize; } cout<<endl<<"traverse is over!"<<endl; } }; int main(){ DequeType queue; srand(time(0));//取随即数字 ElemType elem; int flag; for(int i=0;i<10;i++){ // elem=rand()%26+'a'; // flag=rand()%2; cin>>elem>>flag; //若flag非0,则push elem从前端 //若flag==0,则push elem 从后端 if(flag){ queue.push_front(elem); cout<<"push "<<elem<<" from front"<<endl; }else { queue.push_back(elem); cout<<"push "<<elem<<" from back"<<endl; } } cout<<"--------traverse start---------"<<endl; queue.traverse(); cout << "Hello world!" << endl; return 0; }
//三)双端队列基本操作:
// (3.1)init:初始化
// front=back=size=0;
// (3.2)push_back:尾进
// (先赋值,然后++)array[back]=elem;back=(back+1)%MaxSize;
// ( 注:初始化back=0.所以back第一次填数刚好填的是array[0] )
//
// (3.3)push_front:头进
// (先--,再赋值)front=(front-1+MaxSize)%MaxSize; array[front]=elem;
// (注: 初始化front=0,所以front第一次填数刚好填的是array[MaxSize-1] )
//
// (3.4)pop_back:尾出
// (先--,再赋值)back=(back-1+MaxSize)%MaxSize;elem=array[back];
//
// (3.5)pop_front:头出
// (先赋值,再++) elem=array[front];front=(front+1)%MaxSize;
//
// (3.6)get_front:取头(与pop_front类似,只不过front不变化)
// elem=array[front];
//
// (3.7)get_back:取尾 (与pop_back类似,只不过back不变化)
// elem=array[(back-1+MaxSize)%MaxSize];
//
//
// (3.8)length:长度
// return size或者(back-front+MaxSize)%MaxSize;
//
// (3.9)isEmpty:判空
// size==0或者front==back;
//
// (3.10)isFull:判满
// size==MaxSize或者(back+1)%MaxSize==front;
//
// (3.11)traverse:从头到尾遍历:
// while(front!=back){
// cout<<array[front]<<endl;
// front=(front+1)%MaxSize;
// }
然后我就拿着这个去做了hdu的1702
然后,一天过去了,写出来了下面这个,一只wa,还没找出原因,先贴在这儿改日再战
//双端队列,两边#include <iostream> using namespace std; typedef int ElemType; #include <time.h> #include <stdlib.h> #include <iostream> #define MaxSize 30 class DequeType{ public: int front; int back; int size; ElemType array[MaxSize]; public:/*构造函数,初始化成员变量*/ DequeType (){ front=back=size=0; } bool isEmpty(){//判断是否为空 if(0==size){ return true; }else { return false; } } bool isFull(){//判断是否满 if(MaxSize==size){ return true; }else{ return false; } } bool pop_front(ElemType& elem){//从前端删除 if(isEmpty()){ return false; } elem=array[front]; front=(front+1)%MaxSize; size--; return true; } bool push_front(ElemType elem){//从前端输入 if(isFull()){ return false; } front=(front-1+MaxSize)%MaxSize;//循环队列 array[front]=elem; size++; return true; } bool pop_back(ElemType& elem){//从后端删除 if(isEmpty()){ return false; } back=(back-1+MaxSize)%MaxSize; elem=array[back]; size--; return true; } bool get_front(ElemType & elem){//从前端取数 if(isEmpty()){ return false; } elem=array[front]; return true; } bool get_back(ElemType& elem){//从后端取数 if(isEmpty()){ return false; } elem=array[(back-1+MaxSize)%MaxSize]; return true; } int length(){//求队列长度 return size; } }; int main(){ DequeType queue; ElemType elem; int ca,n,e; char s[10],m[5]; cin>>ca; int flag=10; while(ca--){ cin>>n; flag=10; for(int i=1;i<=4;i++){ cin>>s[i]; } if(s[3]=='L'){ flag=1;//先进后出,即头进头出 } else if(s[3]=='F'){ flag=0;//先进先出,即头进尾出 } for(int i=0;i<n;i++){ scanf("%s",&m); if(m[0]=='I'){ cin>>elem; queue.push_front(elem); }else{ if(flag){ if(queue.get_front(e)){ cout<<e<<endl; }else{ cout<<"None"<<endl; } queue.pop_front(e); }else{ if(queue.get_back(e)){ cout<<e<<endl; }else{ cout<<"None"<<endl; } queue.pop_back(e); } } } } return 0; }
转变思路,决定用队列跟栈一起搞
#include<iostream> #include<cstdio> #include<queue> #include<cstring> #include<stack> using namespace std; int main(){ int n,m,k; char s[5],ss[5]; cin>>n; while(n--){ cin>>m; cin>>s; if(s[2]=='F'){//队列 queue<int>mm; for(int i=0;i<m;i++){ cin>>ss; if(ss[0]=='I'){ cin>>k; mm.push(k); }else{ if(mm.empty()){ cout<<"None"<<endl; }else{ cout<<mm.front()<<endl; mm.pop(); } } } }else{//栈 stack<int>mmm; for(int i=0;i<m;i++){ cin>>ss; if(ss[0]=='I'){ cin>>k; mmm.push(k); }else{ if(mmm.empty()){ cout<<"None"<<endl; }else{ cout<<mmm.top()<<endl; mmm.pop(); } } } } } return 0; }
标签:return,队列,双端,back,elem,MaxSize,front,array,技能 From: https://www.cnblogs.com/killjoyskr/p/16841746.html