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

Appearance settings

Commit 7e40fb9

Browse filesBrowse files
Sean PrashadSean Prashad
Sean Prashad
authored and
Sean Prashad
committed
Add 225_Implement_Stack_using_Queues.java
1 parent b831704 commit 7e40fb9
Copy full SHA for 7e40fb9

File tree

Expand file treeCollapse file tree

1 file changed

+32
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+32
-0
lines changed
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class MyStack {
2+
Queue<Integer> q;
3+
4+
/** Initialize your data structure here. */
5+
public MyStack() {
6+
q = new LinkedList<>();
7+
}
8+
9+
/** Push element x onto stack. */
10+
public void push(int x) {
11+
q.offer(x);
12+
13+
for (int i = 0; i < q.size() - 1; i++) {
14+
q.offer(q.poll());
15+
}
16+
}
17+
18+
/** Removes the element on top of the stack and returns that element. */
19+
public int pop() {
20+
return q.remove();
21+
}
22+
23+
/** Get the top element. */
24+
public int top() {
25+
return q.peek();
26+
}
27+
28+
/** Returns whether the stack is empty. */
29+
public boolean empty() {
30+
return q.isEmpty();
31+
}
32+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.