We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 930dd2c commit 9d5e1bdCopy full SHA for 9d5e1bd
kotlin/0225-implement-stack-using-queues.kt
@@ -0,0 +1,28 @@
1
+class MyStack() {
2
+
3
+ val stack = LinkedList<Int>()
4
5
+ fun push(x: Int) = stack.addLast(x)
6
7
+ // Circular behaviour
8
+ fun pop(): Int {
9
+ repeat(stack.size-1) {
10
+ stack.addLast(stack.removeFirst())
11
+ }
12
+ return stack.removeFirst()
13
14
15
+ fun top() = stack.peekLast()
16
17
+ fun empty() = stack.isEmpty()
18
19
+}
20
21
+/**
22
+ * Your MyStack object will be instantiated and called as such:
23
+ * var obj = MyStack()
24
+ * obj.push(x)
25
+ * var param_2 = obj.pop()
26
+ * var param_3 = obj.top()
27
+ * var param_4 = obj.empty()
28
+ */
0 commit comments