#include<iostream>
using namespace std;
#define maxsize 5
typedef struct sqQueue {
int data[maxsize];//maxsize-1大小的数组
int front, rear;
} sqQueue;
// 队列的初始化
void InitQueue(sqQueue &q) {
q.rear = q.front = 0;
}
// 判空
bool isEmpty(sqQueue &q) {
return q.rear == q.front;
}
// 入队
bool EnQueue(sqQueue &q, int x) {
if ((q.rear + 1) % maxsize == q.front) {
return false; // 队列满了
}
q.data[q.rear++] = x;
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;
return true;
}
int main() {
sqQueue q;
InitQueue(q);
bool result = isEmpty(q);
if (result) {
cout << "该队列目前没有任何元素" << endl;
} else {
cout << "该队列目前不为空" << endl;
}
EnQueue(q, 1);
EnQueue(q, 2);
EnQueue(q, 3);
bool ret = EnQueue(q, 5);
if (ret) {
cout << "insert success" << endl;
} else {
cout << "insert failed" << endl;
}
int x;
bool re = DeQueue(q, x);
if (re) {
cout << "delete success" << endl;
cout << "删除的是:" << x;
}
return 0;
}
顺序队列的实现
最新推荐文章于 2024-09-02 16:35:28 发布