From ec536c614c531e9e0f615b87a141d139d0027551 Mon Sep 17 00:00:00 2001 From: mohamedtamer0 Date: Mon, 21 Feb 2022 17:03:52 +0200 Subject: [PATCH] update --- .idea/uiDesigner.xml | 124 ++++++++++++++ src/main/kotlin/03-linked-list/LinkedList.kt | 56 ++++++- .../03-linked-list/LinkedListIterator.kt | 34 ++++ src/main/kotlin/03-linked-list/Main.kt | 154 +++++++++++------- src/main/kotlin/Main.kt | 7 - src/main/kotlin/Utils.kt | 8 + 6 files changed, 310 insertions(+), 73 deletions(-) create mode 100644 .idea/uiDesigner.xml create mode 100644 src/main/kotlin/03-linked-list/LinkedListIterator.kt delete mode 100644 src/main/kotlin/Main.kt create mode 100644 src/main/kotlin/Utils.kt diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/kotlin/03-linked-list/LinkedList.kt b/src/main/kotlin/03-linked-list/LinkedList.kt index 0d11c34..b095080 100644 --- a/src/main/kotlin/03-linked-list/LinkedList.kt +++ b/src/main/kotlin/03-linked-list/LinkedList.kt @@ -1,11 +1,13 @@ package `03-linked-list` -class LinkedList { +class LinkedList : Iterable, + Collection, MutableIterable, MutableCollection { private var head: Node? = null private var tail: Node? = null - private var size = 0 + override var size = 0 + private set - fun isEmpty(): Boolean { + override fun isEmpty(): Boolean { return size == 0 } @@ -121,5 +123,53 @@ class LinkedList { return result } + override fun iterator(): MutableIterator { + return LinkedListIterator(this) + } + + override fun contains(element: T): Boolean { + for (item in this) { + if (item == element) return true + } + return false + } + + override fun containsAll(elements: Collection): Boolean { + for (searched in elements) { + if (!contains(searched)) return false + } + return true + } + + override fun add(element: T): Boolean { + append(element) + return true + } + + override fun addAll(elements: Collection): Boolean { + for (element in elements) { + append(element) + } + return true + } + + override fun clear() { + head = null + tail = null + size = 0 + } + + override fun remove(element: T): Boolean { + TODO("Not yet implemented") + } + + override fun removeAll(elements: Collection): Boolean { + TODO("Not yet implemented") + } + + override fun retainAll(elements: Collection): Boolean { + TODO("Not yet implemented") + } + } \ No newline at end of file diff --git a/src/main/kotlin/03-linked-list/LinkedListIterator.kt b/src/main/kotlin/03-linked-list/LinkedListIterator.kt new file mode 100644 index 0000000..373308f --- /dev/null +++ b/src/main/kotlin/03-linked-list/LinkedListIterator.kt @@ -0,0 +1,34 @@ +package `03-linked-list` + +class LinkedListIterator(private val list: LinkedList) : Iterator, MutableIterator { + private var index = 0 + private var lastNode: Node? = null + + override fun hasNext(): Boolean { + return index < list.size + } + + override fun next(): T { + if (index >= list.size) throw IndexOutOfBoundsException() + + lastNode = if (index == 0) { + list.nodeAt(0) + } else { + lastNode?.next + } + index++ + return lastNode!!.value + } + + override fun remove() { + if (index == 1) { + list.pop() + } else { + val prevNode = list.nodeAt(index - 2) ?: return + + list.removeAfter(prevNode) + lastNode = prevNode + } + index-- + } +} \ No newline at end of file diff --git a/src/main/kotlin/03-linked-list/Main.kt b/src/main/kotlin/03-linked-list/Main.kt index 9622785..916e420 100644 --- a/src/main/kotlin/03-linked-list/Main.kt +++ b/src/main/kotlin/03-linked-list/Main.kt @@ -1,93 +1,121 @@ package `03-linked-list` +import example + fun main() { //creating and linking nodes - val node1 = Node(value = 1) - val node2 = Node(value = 2) - val node3 = Node(value = 3) - node1.next = node2 - node2.next = node3 - println("creating and linking nodes") - println(node1) + "creating and linking nodes" example { + val node1 = Node(value = 1) + val node2 = Node(value = 2) + val node3 = Node(value = 3) + node1.next = node2 + node2.next = node3 + println("creating and linking nodes") + println(node1) + } println("========================") + //Push > O(1) - val list = LinkedList() - list.push(3) - list.push(2) - list.push(1) - println("Push") - println(list) + "push" example { + val list = LinkedList() + list.push(3) + list.push(2) + list.push(1) + println(list) + } println("========================") - - //Append > O(1) - val list2 = LinkedList() - list2.append(1) - list2.append(2) - list2.append(3) - println("Append") - println(list2) + "append" example { + val list2 = LinkedList() + list2.append(1) + list2.append(2) + list2.append(3) + println(list2) + } println("========================") + //Insert > O(1) - val list3 = LinkedList() - list3.push(3) - list3.push(2) - list3.push(1) - println("Insert") - println("Before inserting: $list3") - var midNode = list3.nodeAt(1)!! - for (i in 1..3) { - midNode = list3.insert(-1 * i, midNode) + "inserting at a particular index" example { + val list3 = LinkedList() + list3.push(3) + list3.push(2) + list3.push(1) + println("Before inserting: $list3") + var midNode = list3.nodeAt(1)!! + for (i in 1..3) { + midNode = list3.insert(-1 * i, midNode) + } + println("After inserting: $list3") } - println("After inserting: $list3") - println("========================") + println("========================") //POP > O(1) - val list4 = LinkedList() - list4.push(3) - list4.push(2) - list4.push(1) - println("POP") - println("Before popping list: $list4") - val poppedValue = list4.pop() - val poppedValue1 = list4.pop() - println("After popping list: $list4") - println("Popped value: $poppedValue") - println("Popped value: $poppedValue1") + "pop" example { + val list4 = LinkedList() + list4.push(3) + list4.push(2) + list4.push(1) + println("Before popping list: $list4") + val poppedValue = list4.pop() + val poppedValue1 = list4.pop() + println("After popping list: $list4") + println("Popped value: $poppedValue") + println("Popped value: $poppedValue1") + } println("========================") //RemoveLast > O(n) - val list5 = LinkedList() - list5.push(3) - list5.push(2) - list5.push(1) - println("RemoveLast") - println("Before removing last node: $list5") - val removedValue = list5.removeLast() - println("After removing last node: $list5") - println("Removed value: $removedValue") + "removing the last node" example { + val list5 = LinkedList() + list5.push(3) + list5.push(2) + list5.push(1) + println("Before removing last node: $list5") + val removedValue = list5.removeLast() + println("After removing last node: $list5") + println("Removed value: $removedValue") + } println("========================") - //RemoveAfter > O(1) - val list6 = LinkedList() - list6.push(3) - list6.push(2) - list6.push(1) - println("Before removing at particular index: $list6") - val index = 1 - val node = list6.nodeAt(index - 1)!! - val removedAfterValue = list6.removeAfter(node) - println("After removing at index $index: $list6") - println("Removed value: $removedAfterValue") + "removing a node after a particular node" example { + val list6 = LinkedList() + list6.push(3) + list6.push(2) + list6.push(1) + println("Before removing at particular index: $list6") + val index = 1 + val node = list6.nodeAt(index - 1)!! + val removedAfterValue = list6.removeAfter(node) + println("After removing at index $index: $list6") + println("Removed value: $removedAfterValue") + } + println("========================") + + + //"printing doubles" LinkedListIterator + "printing doubles" example { + val list7 = LinkedList() + list7.push(4) + list7.push(3) + list7.push(2) + list7.push(1) + println(list7) + for (item in list7) { + println("Double: ${item * 2}") + } + } + println("========================") + + } \ No newline at end of file diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt deleted file mode 100644 index f2a59b6..0000000 --- a/src/main/kotlin/Main.kt +++ /dev/null @@ -1,7 +0,0 @@ -fun main(args: Array) { - println("Hello World!") - - // Try adding program arguments via Run/Debug configuration. - // Learn more about running applications: https://www.jetbrains.com/help/idea/running-applications.html. - println("Program arguments: ${args.joinToString()}") -} \ No newline at end of file diff --git a/src/main/kotlin/Utils.kt b/src/main/kotlin/Utils.kt new file mode 100644 index 0000000..8e2347d --- /dev/null +++ b/src/main/kotlin/Utils.kt @@ -0,0 +1,8 @@ + + + +infix fun String.example(function: () -> Unit) { + println("---Example of $this---") + function() + println() +} \ No newline at end of file