循环队列(顺序存储)
#include<stdio.h> #include<stdlib.h> #define MaxSize 10 //定义队列 typedef struct{ int data[MaxSize]; //存放队列元素 int front,rear; //定义队首指针和队尾指针 }SqQueue; //初始化队列 void InitQueue(SqQueue &Q){ Q.rear=Q.front=0; //初始化队首队尾指针 } //判断队空 bool isEmpty(SqQueue Q){ if(Q.rear==Q.front){ return true; }else{ return false; } } //入队 bool EnQueue(SqQueue &Q,int x){ if((Q.rear+1)%MaxSize==Q.front){ //判断队满 return false; } Q.data[Q.rear]=x; Q.rear=(Q.rear+1)%MaxSize; //防止rear超过MaxSize,同时实现队列循环 return true; } //出队 bool DeQueue(SqQueue &Q,int &x){ if(Q.rear==Q.front){ //判断队空 return false; } x=Q.data[Q.front]; Q.front=(Q.front+1)%MaxSize; //实现队列循环1 return true; } //查找 bool GetQueue(SqQueue Q,int &x){ if(Q.front==Q.rear){ return false; } x=Q.data[Q.front]; return true; } int main(){ }