#include<iostream>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<map>
#define ElemType int
#define MaxSize 100
using namespace std;
typedef struct{
ElemType data[MaxSize];
int front,rear;
//waste storage space
int size; // the first way
int tag; // the secend way
// front=rear&&tag this is a good ideal
}SqQueue;
// (rear+Maxsize-front)%MaxSize
bool EnQueue(SqQueue &Q,ElemType x){
// if((Q.rear+1)%MaxSize==Q.front)
// return false;
if(size==MaxSize)
return false;
Q.data[Q.rear]=x;
Q.rear=(Q.rear+1)%MaxSize;
Q.size+=1;
Q.tag=1;
return true;
}
bool DeQueue(SqQueue &Q,ElemType &x){
if(Q.rear==Q.front)
return false;
x=Q.data[Q.front];
Q.front=(Q.front+1)%MaxSize;
Q.size=Q.size-1;
Q.tag=0;
return true;
}
bool GetHead(SqQueue Q,ElemType &x){
if(Q.front==Q.rear)
return false;
x=Q.data[Q.front]
return true;
}
void InitQueue(SqQueue &Q)
{
Q.rear=Q.front=0;
}
int main()
{
SqQueue Q;
InitQueue(Q);
}
数据结构队列(循环队列)
最新推荐文章于 2024-08-15 11:54:06 发布