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

Commitbb2b2e1

Browse files
EASY/src/easy/ImplementStackUsingQueues.java
1 parente78bcb8 commitbb2b2e1

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
packageeasy;
2+
3+
importjava.util.LinkedList;
4+
importjava.util.Queue;
5+
6+
publicclassImplementStackUsingQueues {
7+
classMyStack {
8+
9+
Queue<Integer>q =newLinkedList();
10+
11+
// Push element x onto stack.
12+
publicvoidpush(intx) {
13+
q.offer(x);
14+
for(inti =1;i <q.size();i++){
15+
q.offer(q.remove());
16+
}
17+
}
18+
19+
// Removes the element on top of the stack.
20+
publicvoidpop() {
21+
q.poll();
22+
}
23+
24+
// Get the top element.
25+
publicinttop() {
26+
returnq.peek();
27+
}
28+
29+
// Return whether the stack is empty.
30+
publicbooleanempty() {
31+
returnq.isEmpty();
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp