Created
April 26, 2024 13:28
-
-
Save JunJaBoy/04b3d71c4e7c0e8c7bbf7271a56b691f to your computer and use it in GitHub Desktop.
HackerRank/Algorithm/QueueUsingTwoStacks kotlin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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