FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
LeetCode-Solutions/C++/implement-queue-using-stacks.cpp at master · Monika-R/LeetCode-Solutions · GitHub
Monika-R
/
LeetCode-Solutions
Public
forked from
kamyu104/LeetCode-Solutions
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
LeetCode-Solutions
/
C++
/
implement-queue-using-stacks.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
39 lines (34 loc) · 793 Bytes
Breadcrumbs
LeetCode-Solutions
/
C++
/
implement-queue-using-stacks.cpp
Copy path
File metadata and controls
39 lines (34 loc) · 793 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//
Time: O(1), amortized
//
Space: O(n)
class
Queue
{
public:
//
Push element x to the back of queue.
void
push
(
int
x) {
A_
.
emplace
(x);
}
//
Removes the element from in front of queue.
void
pop
(
void
) {
peek
();
B_
.
pop
();
}
//
Get the front element.
int
peek
(
void
) {
if
(
B_
.
empty
()) {
//
Transfers the elements in A_ to B_.
while
(!
A_
.
empty
()) {
B_
.
emplace
(
A_
.
top
());
A_
.
pop
();
}
}
if
(
B_
.
empty
()) {
//
B_ is still empty!
throw
length_error
(
"
empty queue
"
);
}
return
B_
.
top
();
}
//
Return whether the queue is empty.
bool
empty
(
void
) {
return
A_
.
empty
() &&
B_
.
empty
();
}
private:
stack<
int
>
A_
,
B_
;
};
Back
|
FazBrowse Home
|
New Git URL