队列代码实现(c++)

#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();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值