#include<iostream>
#include<vector>
using namespace std;
//队列
class Queue {
private:
//head指向队列数组的首个元素
//tail指定队列数组的最后一个元素的下一个位置
int head, tail;
vector<int> arr;
public:
Queue(int n = 10) :arr(n), head(0), tail(0) {};
//入队
void push(int x) {
if (full()) {
cout << "queue full" << endl;
return;
}
arr[tail] = x;
tail += 1;
return;
}
//出队
void pop() {
if (empty()) {
return;
}
head += 1;
return;
}
//判空
bool empty() {
return head == tail;
}
//判满
bool full() {
return tail == arr.size();
}
//查看队首元素
int front() {
return arr[head];
}
//查看队列所有元素
void output() {
cout << "Queue:";
for (int i = head; i < tail;i++) {
cout << arr[i] << " ";
}
cout << endl;
return;
}
};
//主函数入口
int main(){
string op;
int value;
Queue q(5);
while (cin >> op) {
if (op == "insert") {
cin >> value;
q.push(value);
}
else if (op == "front") {
cout << "front element:" << q.front() << endl;
}
else if (op == "pop") {
q.pop();
}
q.output();
}
}
队列代码实现(c++)
最新推荐文章于 2024-07-28 08:47:13 发布