From d991e4bf31602f1d8c268173ce41bff4f99f461a Mon Sep 17 00:00:00 2001 From: Balaviknesh Sekar Date: Mon, 2 Feb 2026 18:51:56 -0600 Subject: [PATCH] Added implementation --- Exercise_1.kt | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ Exercise_2.kt | 46 +++++++++++++++++++++++++++++++++++++++ Exercise_3.kt | 49 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 Exercise_1.kt create mode 100644 Exercise_2.kt create mode 100644 Exercise_3.kt diff --git a/Exercise_1.kt b/Exercise_1.kt new file mode 100644 index 000000000..831ac4a67 --- /dev/null +++ b/Exercise_1.kt @@ -0,0 +1,59 @@ +// Time Complexity: push() - O(1), pop() - O(1), peek() - O(1), isEmpty() - O(1) +// Space Complexity: O(MAX) where MAX = 1000 + +class Stack { + // Please read sample.java file before starting. + // Kindly include Time and Space complexity at top of each file + + companion object { + const val MAX = 1000 + } + + // Initialize your constructor + private var top: Int = -1 + private val a = IntArray(MAX) // Maximum size of Stack + + fun isEmpty(): Boolean { + // Write your code here + return top == -1 + } + + fun push(x: Int): Boolean { + // Check for stack Overflow + // Write your code here + if (top >= MAX - 1) { + println("Stack Overflow") + return false + } + a[++top] = x + return true + } + + fun pop(): Int { + // If empty return 0 and print "Stack Underflow" + // Write your code here + if (isEmpty()) { + println("Stack Underflow") + return 0 + } + return a[top--] + } + + fun peek(): Int { + // Write your code here + if (isEmpty()) { + println("Stack is empty") + return 0 + } + return a[top] + } +} + +// Driver code +fun main() { + val s = Stack() + s.push(10) + s.push(20) + s.push(30) + println("${s.pop()} Popped from stack") +} diff --git a/Exercise_2.kt b/Exercise_2.kt new file mode 100644 index 000000000..0f34464de --- /dev/null +++ b/Exercise_2.kt @@ -0,0 +1,46 @@ +// Time Complexity: push() - O(1), pop() - O(1), peek() - O(1), isEmpty() - O(1) +// Space Complexity: O(n) where n = number of elements in the stack + +class StackAsLinkedList { + private var root: StackNode? = null + + private class StackNode(val data: Int, var next: StackNode? = null) + + fun isEmpty(): Boolean { + return root == null + } + + fun push(data: Int) { + val newNode = StackNode(data, root) + root = newNode + } + + fun pop(): Int { + if (isEmpty()) { + println("Stack Underflow") + return 0 + } + val popped = root!!.data + root = root!!.next + return popped + } + + fun peek(): Int { + if (isEmpty()) { + println("Stack is empty") + return 0 + } + return root!!.data + } +} + +fun main() { + val sll = StackAsLinkedList() + + sll.push(10) + sll.push(20) + sll.push(30) + + println("${sll.pop()} popped from stack") + println("Top element is ${sll.peek()}") +} diff --git a/Exercise_3.kt b/Exercise_3.kt new file mode 100644 index 000000000..e613053fa --- /dev/null +++ b/Exercise_3.kt @@ -0,0 +1,49 @@ +// Time Complexity: insert() - O(n), printList() - O(n) +// Space Complexity: O(n) where n = number of nodes in the list + +class LinkedList { + var head: Node? = null + + class Node(var data: Int, var next: Node? = null) +} + +fun insert(list: LinkedList, data: Int): LinkedList { + val newNode = LinkedList.Node(data) + + // If the Linked List is empty, make the new node as head + if (list.head == null) { + list.head = newNode + } else { + // Else traverse till the last node and insert the new_node there + var last = list.head!! + while (last.next != null) { + last = last.next!! + } + last.next = newNode + } + // Return the list by head + return list +} + +fun printList(list: LinkedList) { + var current = list.head + while (current != null) { + print("${current.data} ") + current = current.next + } + println() +} + +fun main() { + val list = LinkedList() + + // Insert the values + insert(list, 1) + insert(list, 2) + insert(list, 3) + insert(list, 4) + insert(list, 5) + + // Print the LinkedList + printList(list) +}