-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplementStack.java
More file actions
44 lines (37 loc) · 963 Bytes
/
ImplementStack.java
File metadata and controls
44 lines (37 loc) · 963 Bytes
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
40
41
42
43
44
package Stack;
import java.util.LinkedList;
import java.util.Queue;
/**
* Author - archit.s
* Date - 05/09/18
* Time - 12:07 AM
*/
public class ImplementStack {
class MyStack {
private Queue<Integer> q;
/** Initialize your data structure here. */
public MyStack() {
q = new LinkedList<>();
}
/** Push element x onto stack. */
public void push(int x) {
q.add(x);
for(int i=0;i<q.size()-1;i++){
int top = q.poll();
q.add(top);
}
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
return q.poll();
}
/** Get the top element. */
public int top() {
return q.peek();
}
/** Returns whether the stack is empty. */
public boolean empty() {
return q.isEmpty();
}
}
}