You are viewing a single comment's thread. Return to all comments →
Not sure everyone here is understanding the assignment. Here it is in Kotlin:
class Queue2s<T> { val stack1 = Stack<T>() val stack2 = Stack<T>() fun enqueue(value:T) { if (stack2.isEmpty() && stack1.isEmpty()) stack1.push(value) else stack2.push(value) } fun dequeue():T { if (stack1.isEmpty()) moveStack() return stack1.pop() } fun peek():T { if (stack1.isEmpty()) moveStack() return stack1.peek() } fun moveStack() { while (stack2.isNotEmpty()){ stack1.push(stack2.pop()) } } }
Seems like cookies are disabled on this browser, please enable them to open this website
Queue using Two Stacks
You are viewing a single comment's thread. Return to all comments →
Not sure everyone here is understanding the assignment. Here it is in Kotlin: