Files
python/TangDou/LuoGuBook/15_3.cpp

50 lines
878 B
C++
Raw Normal View History

2025-08-30 18:35:01 +08:00
#include <bits/stdc++.h>
using namespace std;
queue<int> q;
vector<int> v1;
void Pop() {
q.pop();
}
int Top() {
return q.front();
}
void Delete(int x) {
//找到第一个x值,并删除掉
while (q.front() != x) {
v1.push_back(q.front());
q.pop();
}
//删除x
q.pop();
while (!q.empty()) {
v1.push_back(q.front());
q.pop();
}
//复制回来
for (int i = 0; i < v1.size(); i++)q.push(v1[i]);
}
const int N = 7;
int a[N] = {1, 3, 3, 5, 6, 8, 9};
int main() {
//初始化队列
for (int i = 0; i < N; i++) q.push(a[i]);
//测试Top
cout<<Top()<<endl;
//测试Pop
Pop();
//测试Delete
Delete(3);
//输出队列内容
while(!q.empty()){
cout << q.front() << " ";
q.pop();
}
return 0;
}