Skip to content

Instantly share code, notes, and snippets.

@JunJaBoy
Created April 26, 2024 13:28
Show Gist options
  • Save JunJaBoy/04b3d71c4e7c0e8c7bbf7271a56b691f to your computer and use it in GitHub Desktop.
Save JunJaBoy/04b3d71c4e7c0e8c7bbf7271a56b691f to your computer and use it in GitHub Desktop.
HackerRank/Algorithm/QueueUsingTwoStacks kotlin
import java.util.*
const val QUEUE_ENQUEUE = 1
const val QUEUE_DEQUEUE = 2
const val QUEUE_PRINT_HEAD = 3
fun queueUsingTwoStacks(
numberOfQueues: Int,
scanner: Scanner = Scanner(System.`in`),
) {
val queue = LinkedList<Int>()
repeat(numberOfQueues) {
val input = scanner.nextInt()
when (input) {
QUEUE_ENQUEUE -> {
queue.add(scanner.nextInt())
}
QUEUE_DEQUEUE -> {
queue.removeFirst()
}
QUEUE_PRINT_HEAD -> {
println(queue.peek())
}
}
}
}
fun main(args: Array<String>) {
queueUsingTwoStacks(numberOfQueues = readln().toInt())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment