Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions Exercise_1.kt
Original file line number Diff line number Diff line change
@@ -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")
}
46 changes: 46 additions & 0 deletions Exercise_2.kt
Original file line number Diff line number Diff line change
@@ -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()}")
}
49 changes: 49 additions & 0 deletions Exercise_3.kt
Original file line number Diff line number Diff line change
@@ -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)
}