Skip to content

Commit aba3cb4

Browse files
committed
新增 LeetCode 225 用队列实现栈的 C++ 实现,包含基本操作的代码
1 parent e7c9b52 commit aba3cb4

4 files changed

Lines changed: 397 additions & 0 deletions

File tree

  • book/代码随想录/栈和队列/225. 用队列实现栈
  • leetcode/Study Plan/225. 用队列实现栈
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* @Author: tkzzzzzz6
3+
* @Date: 2026-04-14 15:13:11
4+
* @LastEditors: tkzzzzzz6
5+
* @LastEditTime: 2026-04-14 21:14:52
6+
*/
7+
/*
8+
* @lc app=leetcode.cn id=225 lang=cpp
9+
* @lcpr version=30204
10+
*
11+
* [225] 用队列实现栈
12+
*/
13+
14+
15+
// @lcpr-template-start
16+
using namespace std;
17+
#include <algorithm>
18+
#include <array>
19+
#include <bitset>
20+
#include <climits>
21+
#include <deque>
22+
#include <functional>
23+
#include <iostream>
24+
#include <list>
25+
#include <queue>
26+
#include <stack>
27+
#include <tuple>
28+
#include <unordered_map>
29+
#include <unordered_set>
30+
#include <utility>
31+
#include <vector>
32+
// @lcpr-template-end
33+
// @lc code=start
34+
class MyStack {
35+
public:
36+
queue<int> q1,q2; //q2用于备份
37+
MyStack() {
38+
39+
}
40+
41+
void push(int x) {
42+
q1.push(x);
43+
}
44+
45+
int pop() {
46+
int n = q1.size();
47+
if(n < 1)return -1;
48+
--n;
49+
while(n--){
50+
q2.push(q1.front());
51+
q1.pop();
52+
}
53+
54+
int res = q1.front();
55+
q1.pop();
56+
57+
q1 = q2;
58+
while(!q2.empty())q2.pop();
59+
return res;
60+
}
61+
62+
int top() {
63+
int n = q1.size();
64+
if(n < 1)return -1;
65+
--n;
66+
while(n--){
67+
q2.push(q1.front());
68+
q1.pop();
69+
}
70+
71+
int res = q1.front();
72+
q2.push(q1.front());
73+
74+
q1 = q2;
75+
while(!q2.empty())q2.pop();
76+
return res;
77+
}
78+
79+
bool empty() {
80+
return q1.empty();
81+
}
82+
};
83+
84+
/**
85+
* Your MyStack object will be instantiated and called as such:
86+
* MyStack* obj = new MyStack();
87+
* obj->push(x);
88+
* int param_2 = obj->pop();
89+
* int param_3 = obj->top();
90+
* bool param_4 = obj->empty();
91+
*/
92+
// @lc code=end
93+
94+
95+
96+
/*
97+
// @lcpr case=start
98+
// ["MyStack", "push", "push", "top", "pop", "empty"][[], [1], [2], [], [], []]\n
99+
// @lcpr case=end
100+
101+
*/
102+
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* @Author: tkzzzzzz6
3+
* @Date: 2026-04-14 15:13:11
4+
* @LastEditors: tkzzzzzz6
5+
* @LastEditTime: 2026-04-14 21:35:34
6+
*/
7+
/*
8+
* @lc app=leetcode.cn id=225 lang=cpp
9+
* @lcpr version=30204
10+
*
11+
* [225] 用队列实现栈
12+
*/
13+
14+
// 使用单个队列实现
15+
16+
// @lcpr-template-start
17+
using namespace std;
18+
#include <algorithm>
19+
#include <array>
20+
#include <bitset>
21+
#include <climits>
22+
#include <deque>
23+
#include <functional>
24+
#include <iostream>
25+
#include <list>
26+
#include <queue>
27+
#include <stack>
28+
#include <tuple>
29+
#include <unordered_map>
30+
#include <unordered_set>
31+
#include <utility>
32+
#include <vector>
33+
// @lcpr-template-end
34+
// @lc code=start
35+
class MyStack {
36+
public:
37+
queue<int> q1;
38+
MyStack() {
39+
40+
}
41+
42+
void push(int x) {
43+
q1.push(x);
44+
}
45+
46+
int pop() {
47+
int n = q1.size();
48+
if(n < 1)return -1;
49+
--n;
50+
while(n--){
51+
q1.push(q1.front());
52+
q1.pop();
53+
}
54+
55+
int res = q1.front();
56+
q1.pop();
57+
return res;
58+
}
59+
60+
int top() {
61+
int n = q1.size();
62+
if(n < 1)return -1;
63+
--n;
64+
while(n--){
65+
q1.push(q1.front());
66+
q1.pop();
67+
}
68+
69+
int res = q1.front();
70+
q1.push(q1.front());
71+
q1.pop();
72+
return res;
73+
}
74+
75+
bool empty() {
76+
return q1.empty();
77+
}
78+
};
79+
80+
/**
81+
* Your MyStack object will be instantiated and called as such:
82+
* MyStack* obj = new MyStack();
83+
* obj->push(x);
84+
* int param_2 = obj->pop();
85+
* int param_3 = obj->top();
86+
* bool param_4 = obj->empty();
87+
*/
88+
// @lc code=end
89+
90+
91+
92+
/*
93+
// @lcpr case=start
94+
// ["MyStack", "push", "push", "top", "pop", "empty"][[], [1], [2], [], [], []]\n
95+
// @lcpr case=end
96+
97+
*/
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* @Author: tkzzzzzz6
3+
* @Date: 2026-04-14 15:13:11
4+
* @LastEditors: tkzzzzzz6
5+
* @LastEditTime: 2026-04-14 15:59:39
6+
*/
7+
/*
8+
* @lc app=leetcode.cn id=225 lang=cpp
9+
* @lcpr version=30204
10+
*
11+
* [225] 用队列实现栈
12+
*/
13+
14+
15+
// @lcpr-template-start
16+
using namespace std;
17+
#include <algorithm>
18+
#include <array>
19+
#include <bitset>
20+
#include <climits>
21+
#include <deque>
22+
#include <functional>
23+
#include <iostream>
24+
#include <list>
25+
#include <queue>
26+
#include <stack>
27+
#include <tuple>
28+
#include <unordered_map>
29+
#include <unordered_set>
30+
#include <utility>
31+
#include <vector>
32+
// @lcpr-template-end
33+
// @lc code=start
34+
class MyStack {
35+
public:
36+
queue<int> q1,q2; //q2用于备份
37+
MyStack() {
38+
39+
}
40+
41+
void push(int x) {
42+
q1.push(x);
43+
}
44+
45+
int pop() {
46+
int n = q1.size();
47+
if(n < 1)return -1;
48+
--n;
49+
while(n--){
50+
q2.push(q1.front());
51+
q1.pop();
52+
}
53+
54+
int res = q1.front();
55+
q1.pop();
56+
57+
q1 = q2;
58+
while(!q2.empty())q2.pop();
59+
return res;
60+
}
61+
62+
int top() {
63+
int n = q1.size();
64+
if(n < 1)return -1;
65+
--n;
66+
while(n--){
67+
q2.push(q1.front());
68+
q1.pop();
69+
}
70+
71+
int res = q1.front();
72+
q2.push(q1.front());
73+
74+
q1 = q2;
75+
while(!q2.empty())q2.pop();
76+
return res;
77+
}
78+
79+
bool empty() {
80+
return q1.empty();
81+
}
82+
};
83+
84+
/**
85+
* Your MyStack object will be instantiated and called as such:
86+
* MyStack* obj = new MyStack();
87+
* obj->push(x);
88+
* int param_2 = obj->pop();
89+
* int param_3 = obj->top();
90+
* bool param_4 = obj->empty();
91+
*/
92+
// @lc code=end
93+
94+
95+
96+
/*
97+
// @lcpr case=start
98+
// ["MyStack", "push", "push", "top", "pop", "empty"][[], [1], [2], [], [], []]\n
99+
// @lcpr case=end
100+
101+
*/

0 commit comments

Comments
 (0)