From b211778e6ed8a5fdc758809c3a61dae1f4e34c3a Mon Sep 17 00:00:00 2001 From: Ralph Date: Tue, 26 Sep 2023 18:18:19 -0400 Subject: [PATCH 1/2] Stacks and Queues in Kotlin! --- .../stacks_and_queues/code/kotlin/Queue.kt | 33 +++++++++++++++++++ .../stacks_and_queues/code/kotlin/Stack.kt | 33 +++++++++++++++++++ .../stacks_and_queues/stacks_and_queues.md | 4 +++ 3 files changed, 70 insertions(+) create mode 100644 contents/stacks_and_queues/code/kotlin/Queue.kt create mode 100644 contents/stacks_and_queues/code/kotlin/Stack.kt diff --git a/contents/stacks_and_queues/code/kotlin/Queue.kt b/contents/stacks_and_queues/code/kotlin/Queue.kt new file mode 100644 index 000000000..23f3b64dd --- /dev/null +++ b/contents/stacks_and_queues/code/kotlin/Queue.kt @@ -0,0 +1,33 @@ +class Queue { + private val list = mutableListOf() + + fun enqueue(item: T) { + list.add(item) + } + + fun dequeue(): T? { + return if (list.isEmpty()) { + null + } else list.removeAt(0) + } + + fun front(): T? { + return if (list.isEmpty()) { + null + } else list[0] + } + + fun size(): Int = return list.size +} + +fun main(args: Array) { + val queue = Queue() + queue.enqueue(1) + queue.enqueue(2) + queue.enqueue(3) + + println("Front: ${queue.front()}") + println(queue.dequeue()) + println("Size: ${queue.size()}") + println(queue.dequeue()) +} \ No newline at end of file diff --git a/contents/stacks_and_queues/code/kotlin/Stack.kt b/contents/stacks_and_queues/code/kotlin/Stack.kt new file mode 100644 index 000000000..2833fced4 --- /dev/null +++ b/contents/stacks_and_queues/code/kotlin/Stack.kt @@ -0,0 +1,33 @@ +class Stack { + private val stack = mutableListOf() + + fun push(item: T) { + stack.add(item) + } + + fun pop(): T? { + return if (stack.isEmpty()) { + null + } else stack.removeAt(stack.size - 1) + } + + fun size(): Int = return stack.size + + fun top(): T? { + return if (stack.isEmpty()) { + null + } else stack[stack.size - 1] + } +} + +fun main(args: Array) { + val stack = Stack() + stack.push(1) + stack.push(2) + stack.push(3) + + println("Top: ${stack.top()}") + println(stack.pop()) + println("Size: ${stack.size()}") + println(stack.pop()) +} \ No newline at end of file diff --git a/contents/stacks_and_queues/stacks_and_queues.md b/contents/stacks_and_queues/stacks_and_queues.md index e63f72a41..8198b5da1 100644 --- a/contents/stacks_and_queues/stacks_and_queues.md +++ b/contents/stacks_and_queues/stacks_and_queues.md @@ -28,6 +28,8 @@ Here is a simple implementation of a stack: [import, lang:"rust"](code/rust/Stack.rs) {% sample lang="python" %} [import, lang:"python"](code/python/stack.py) +{% sample lang="kotlin" %} +[import, lang:"kotlin"](code/kotlin/Stack.kt) {% endmethod %} Here is a simple implementation of a queue: @@ -42,6 +44,8 @@ Here is a simple implementation of a queue: [import, lang:"rust" ](code/rust/Queue.rs) {% sample lang="python" %} [import, lang:"python"](code/python/queue.py) +{% sample lang="kotlin" %} +[import, lang:"kotlin"](code/kotlin/Queue.kt) {% endmethod %} ## License From eca0a4c5365dfadbcec3efd6ae7503036f8e6c80 Mon Sep 17 00:00:00 2001 From: Ralph Date: Tue, 26 Sep 2023 19:17:07 -0400 Subject: [PATCH 2/2] Some clean up --- contents/stacks_and_queues/code/kotlin/Queue.kt | 2 +- contents/stacks_and_queues/code/kotlin/Stack.kt | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/contents/stacks_and_queues/code/kotlin/Queue.kt b/contents/stacks_and_queues/code/kotlin/Queue.kt index 23f3b64dd..08ff48ffc 100644 --- a/contents/stacks_and_queues/code/kotlin/Queue.kt +++ b/contents/stacks_and_queues/code/kotlin/Queue.kt @@ -17,7 +17,7 @@ class Queue { } else list[0] } - fun size(): Int = return list.size + fun size(): Int = list.size } fun main(args: Array) { diff --git a/contents/stacks_and_queues/code/kotlin/Stack.kt b/contents/stacks_and_queues/code/kotlin/Stack.kt index 2833fced4..ecfb75c3b 100644 --- a/contents/stacks_and_queues/code/kotlin/Stack.kt +++ b/contents/stacks_and_queues/code/kotlin/Stack.kt @@ -1,22 +1,22 @@ class Stack { - private val stack = mutableListOf() + private val list = mutableListOf() fun push(item: T) { - stack.add(item) + list.add(item) } fun pop(): T? { - return if (stack.isEmpty()) { + return if (list.isEmpty()) { null - } else stack.removeAt(stack.size - 1) + } else list.removeAt(list.size - 1) } - fun size(): Int = return stack.size + fun size(): Int = list.size fun top(): T? { - return if (stack.isEmpty()) { + return if (list.isEmpty()) { null - } else stack[stack.size - 1] + } else list[list.size - 1] } }