Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite78bcb8

Browse files
EASY/src/easy/ImplementQueueUsingStacks.java
1 parentda84213 commite78bcb8

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
packageeasy;
2+
3+
importjava.util.Stack;
4+
5+
publicclassImplementQueueUsingStacks {
6+
7+
classMyQueue {
8+
9+
Stack<Integer>input =newStack();
10+
Stack<Integer>output =newStack();
11+
12+
// Push element x to the back of queue.
13+
publicvoidpush(intx) {
14+
input.push(x);
15+
}
16+
17+
// Removes the element from in front of queue.
18+
publicvoidpop() {
19+
peek();
20+
output.pop();
21+
}
22+
23+
// Get the front element.
24+
publicintpeek() {
25+
if(output.isEmpty()){
26+
while(!input.isEmpty()){
27+
output.push(input.pop());
28+
}
29+
}
30+
returnoutput.peek();
31+
}
32+
33+
// Return whether the queue is empty.
34+
publicbooleanempty() {
35+
returninput.isEmpty() &&output.isEmpty();
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp