声明:本文用于学习使用,设计的双端队列手段稍有不同,但功能是完全一样的。不是pta里面的题目答案。能够独立写出来属于自己代码才是最重要的。思想是,队头入时,队头先移动数据再入,队尾入时直接入再移动。
1、简介
双端队列是一种能在队列两头进行操作的队列。在STL包里面存在dequeue是已经打包好的双端队列可直接使用
2、数据结构
typedef int ElemType;
typedef int Position;
struct QNode{
ElemType *Data;
Position front;
Position rear;
int MaxSize;
};
typedef struct QNode *PtrToQNode;
typedef PtrToQNode Deque;
3、函数实现
Deque InitQueue(int MaxSize);
bool Push(ElemType X, Deque D); //队头入
ElemType Pop(Deque D); //队头出
bool Inject(ElemType X, Deque D); //队尾入
ElemType Eject(Deque D); //队尾出
ElemType GetTop(Deque D);
void PrintDeQueue(Deque D);
3.1 初始化队列
Deque InitQueue(int MaxSize){
Deque D = new QNode;
//增加一个空间区分队空与队满,类似于循环队列
MaxSize++;
D->MaxSize = MaxSize;
D->Data = new ElemType [MaxSize];
fill(D->Data,D->Data+D->MaxSize,-1);
D->front = D->rear = 0;
return D;
}
3.2 队头入
bool Push(ElemType X, Deque D){
if((D->rear+1)%D->MaxSize == D->front){
cout << "Queue Full" << endl;
return false;
}else{
D->front = (D->front-1 + D->MaxSize)%D->MaxSize;
D->Data[D->front] = X;
cout << "Push Successfully: " << X << endl;
return true;
}
}
3.3 队头出
ElemType Pop(Deque D){
if(D->rear == D->front){
cout << "Queue is Empty" << endl;
return Error;
}else{
int t = D->Data[D->front];
D->Data[D->front] = -1;
D->front = (D->front+1)%D->MaxSize;
cout << "Pop successfully:" << t << endl;
return t;
}
}
3.4 队尾入
bool Inject(ElemType X, Deque D){
if((D->rear+1)%D->MaxSize == D->front){
cout << "Queue Full" << endl;
return false;
}else{
D->Data[D->rear] = X;
cout << "Inject successfully:" << D->Data[D->rear] << endl;
D->rear = (D->rear+1)%D->MaxSize;
return true;
}
}
3.5 队尾出
ElemType Eject(Deque D){
if(D->rear == D->front){
cout << "Queue Empty" << endl;
return Error;
}else{
D->rear = (D->rear-1+D->MaxSize) % D->MaxSize;
ElemType t = D->Data[D->rear];
D->Data[D->rear] = -1;
cout << "Eject successfully:" << t << endl;
return t;
}
}
3.6 获取头元素
//看你怎么理解GetTop了,我理解的是取头指针指向的位置。
ElemType GetTop(Deque D){
if(D->rear == D->front){
cout << "Queue Empty" << endl;
return Error;
}else{
return D->Data[D->front];
}
}
3.7 打印队列
void PrintDeQueue(Deque D){
for (int i = 0; i < D->MaxSize; i++){
cout << D->Data[i] << " ";
}
cout << endl;
}
4、 完整代码
#include<iostream>
#include<algorithm>
using namespace std;
typedef int ElemType;
typedef int Position;
struct QNode{
ElemType *Data;
Position front;
Position rear;
int MaxSize;
};
typedef struct QNode *PtrToQNode;
typedef PtrToQNode Deque;
#define Error -1
Deque InitQueue(int MaxSize);
bool Push(ElemType X, Deque D); //队头入
ElemType Pop(Deque D); //队头出
bool Inject(ElemType X, Deque D); //队尾入
ElemType Eject(Deque D); //队尾出
ElemType GetTop(Deque D);
void PrintDeQueue(Deque D);
int main(){
Deque D = InitQueue(5);
cout << D->rear << " " << D->front << endl;
cout << "Init Successfully" << endl;
Push(2,D);
Push(5,D);
PrintDeQueue(D);
Pop(D);
Pop(D);
//Pop(D);
PrintDeQueue(D);
Inject(33,D);
Inject(44,D);
Inject(55,D);
//Inject(66,D);
PrintDeQueue(D);
Eject(D);
Eject(D);
PrintDeQueue(D);
Push(3,D);
PrintDeQueue(D);
cout << "GetTop:" << GetTop(D) << endl;
return 0;
}
Deque InitQueue(int MaxSize){
Deque D = new QNode;
//增加一个空间区分队空与队满,类似于循环队列
MaxSize++;
D->MaxSize = MaxSize;
D->Data = new ElemType [MaxSize];
fill(D->Data,D->Data+D->MaxSize,-1);
D->front = D->rear = 0;
return D;
}
bool Push(ElemType X, Deque D){
if((D->rear+1)%D->MaxSize == D->front){
cout << "Queue Full" << endl;
return false;
}else{
D->front = (D->front-1 + D->MaxSize)%D->MaxSize;
D->Data[D->front] = X;
cout << "Push Successfully: " << X << endl;
return true;
}
}
void PrintDeQueue(Deque D){
for (int i = 0; i < D->MaxSize; i++){
cout << D->Data[i] << " ";
}
cout << endl;
}
ElemType Pop(Deque D){
if(D->rear == D->front){
cout << "Queue is Empty" << endl;
return Error;
}else{
int t = D->Data[D->front];
D->Data[D->front] = -1;
D->front = (D->front+1)%D->MaxSize;
cout << "Pop successfully:" << t << endl;
return t;
}
}
bool Inject(ElemType X, Deque D){
if((D->rear+1)%D->MaxSize == D->front){
cout << "Queue Full" << endl;
return false;
}else{
D->Data[D->rear] = X;
cout << "Inject successfully:" << D->Data[D->rear] << endl;
D->rear = (D->rear+1)%D->MaxSize;
return true;
}
}
ElemType Eject(Deque D){
if(D->rear == D->front){
cout << "Queue Empty" << endl;
return Error;
}else{
D->rear = (D->rear-1+D->MaxSize) % D->MaxSize;
ElemType t = D->Data[D->rear];
D->Data[D->rear] = -1;
cout << "Eject successfully:" << t << endl;
return t;
}
}
//看你怎么理解GetTop了,我理解的是取头指针指向的位置。
ElemType GetTop(Deque D){
if(D->rear == D->front){
cout << "Queue Empty" << endl;
return Error;
}else{
return D->Data[D->front];
}
}