隊(duì)列 Queue
一买羞、存儲(chǔ)
typedef struct{
ElementType data[MAX_SIZE]; // 隊(duì)列的順序存儲(chǔ)
int front, rear; // 隊(duì)頭隊(duì)尾指針
}Queue;
#define MAX_SIZE 100
typedef int ElementType;
typedef struct{
ElementType data[MAX_SIZE]; // 隊(duì)列的順序存儲(chǔ)
int front, rear; // 隊(duì)頭隊(duì)尾指針
}Queue;
二熊杨、操作
1.入隊(duì)
bool EnQueue(Queue &q, int x)
{
// 隊(duì)列滿 入隊(duì)失敗
if((q.rear + 1) % MAX_SIZE == q.front) return false;
// 隊(duì)列未滿 入隊(duì)成功 隊(duì)尾插入
q.data[rear] = x;
q.rear = (q.rear + 1) % MAX_SIZE;
return true;
}
2.出隊(duì)
bool DeQueue(Queue &q, int &x)
{
if(q.rear == q.front) return false; // 隊(duì)列空 出隊(duì)失敗
// 隊(duì)列未空 出隊(duì)成功并返回元素到x 隊(duì)頭刪除
x = q.data[q.front];
q.front = (q.front + 1) % MAX_SIZE;
return true;
}