c++实现一个基本的队列,包括入队、出队、判断队空、判断队满等操作
作者:野牛程序员:2023-08-24 12:10:39 C++阅读 2807
题目一:队列的基本操作
实现一个基本的队列,包括入队、出队、判断队空、判断队满等操作。
#include <iostream>
const int MAX_SIZE = 100; // 假设队列的最大容量为100
int queue[MAX_SIZE];
int front = 0;
int rear = 0;
bool isEmpty() {
return front == rear;
}
bool isFull() {
return rear == MAX_SIZE;
}
void enqueue(int value) {
if (!isFull()) {
queue[rear++] = value;
}
}
void dequeue() {
if (!isEmpty()) {
front++;
}
}
int frontValue() {
if (!isEmpty()) {
return queue[front];
}
return -1; // 表示队列为空
}
int main() {
enqueue(1);
enqueue(2);
enqueue(3);
std::cout << frontValue() << std::endl; // 输出:1
dequeue();
std::cout << frontValue() << std::endl; // 输出:2
return 0;
}野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892

- 上一篇:python中的颜色代码
- 下一篇:c++循环队列的实现
