Skip to content

Commit

Permalink
Create 0225-implement-stack-using-queues.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
a93a committed Jan 31, 2023
1 parent 930dd2c commit 9d5e1bd
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions kotlin/0225-implement-stack-using-queues.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class MyStack() {

val stack = LinkedList<Int>()

fun push(x: Int) = stack.addLast(x)

// Circular behaviour
fun pop(): Int {
repeat(stack.size-1) {
stack.addLast(stack.removeFirst())
}
return stack.removeFirst()
}

fun top() = stack.peekLast()

fun empty() = stack.isEmpty()

}

/**
* Your MyStack object will be instantiated and called as such:
* var obj = MyStack()
* obj.push(x)
* var param_2 = obj.pop()
* var param_3 = obj.top()
* var param_4 = obj.empty()
*/

0 comments on commit 9d5e1bd

Please sign in to comment.