|
| 1 | +import java.util.Scanner |
| 2 | + |
| 3 | +class CircularLinkedList { |
| 4 | + private fun addToEmpty(last: Node?, data: Int): Node? { |
| 5 | + var last = last |
| 6 | + if (last != null) return last |
| 7 | + |
| 8 | + // allocate memory to the new node |
| 9 | + val newNode = Node() |
| 10 | + |
| 11 | + // assign data to the new node |
| 12 | + newNode.data = data |
| 13 | + |
| 14 | + // assign last to newNode |
| 15 | + last = newNode |
| 16 | + |
| 17 | + // create link to iteself |
| 18 | + newNode.next = last |
| 19 | + return last |
| 20 | + } |
| 21 | + |
| 22 | + // add node to the front |
| 23 | + private fun addFront(last: Node?, data: Int): Node? { |
| 24 | + if (last == null) return addToEmpty(last, data) |
| 25 | + |
| 26 | + // allocate memory to the new node |
| 27 | + val newNode = Node() |
| 28 | + |
| 29 | + // add data to the node |
| 30 | + newNode.data = data |
| 31 | + |
| 32 | + // store the address of the current first node in the newNode |
| 33 | + newNode.next = last.next |
| 34 | + |
| 35 | + // make newNode as head |
| 36 | + last.next = newNode |
| 37 | + return last |
| 38 | + } |
| 39 | + |
| 40 | + // add node to the end |
| 41 | + private fun addEnd(last: Node?, data: Int): Node? { |
| 42 | + var last = last |
| 43 | + if (last == null) return addToEmpty(last, data) |
| 44 | + |
| 45 | + // allocate memory to the new node |
| 46 | + val newNode = Node() |
| 47 | + |
| 48 | + // add data to the node |
| 49 | + newNode.data = data |
| 50 | + |
| 51 | + // store the address of the head node to next of newNode |
| 52 | + newNode.next = last.next |
| 53 | + |
| 54 | + // point the current last node to the newNode |
| 55 | + last.next = newNode |
| 56 | + |
| 57 | + // make newNode as the last node |
| 58 | + last = newNode |
| 59 | + return last |
| 60 | + } |
| 61 | + |
| 62 | + private fun addAfter(last: Node?, data: Int, item: Int): Node? { |
| 63 | + var last = last ?: return null |
| 64 | + val newNode: Node |
| 65 | + var p: Node? |
| 66 | + p = last.next |
| 67 | + do { |
| 68 | + // if the item is found, place newNode after it |
| 69 | + if (p!!.data == item) { |
| 70 | + // allocate memory to the new node |
| 71 | + newNode = Node() |
| 72 | + |
| 73 | + // add data to the node |
| 74 | + newNode.data = data |
| 75 | + |
| 76 | + // make the next of the current node as the next of newNode |
| 77 | + newNode.next = p.next |
| 78 | + |
| 79 | + // put newNode to the next of p |
| 80 | + p.next = newNode |
| 81 | + |
| 82 | + // if p is the last node, make newNode as the last node |
| 83 | + if (p == last) last = newNode |
| 84 | + return last |
| 85 | + } |
| 86 | + p = p.next |
| 87 | + } while (p != last.next) |
| 88 | + println(item.toString() + "The given node is not present in the list") |
| 89 | + return last |
| 90 | + } |
| 91 | + |
| 92 | + // delete a node |
| 93 | + private fun deleteNode(last: Node?, key: Int): Node? { |
| 94 | + // if linked list is empty |
| 95 | + var last: Node? = last ?: return null |
| 96 | + |
| 97 | + // if the list contains only a single node |
| 98 | + if (last!!.data == key && last.next == last) { |
| 99 | + last = null |
| 100 | + return last |
| 101 | + } |
| 102 | + var temp = last |
| 103 | + var d: Node? = Node() |
| 104 | + |
| 105 | + // if last is to be deleted |
| 106 | + if (last.data == key) { |
| 107 | + // find the node before the last node |
| 108 | + while (temp!!.next != last) { |
| 109 | + temp = temp.next |
| 110 | + } |
| 111 | + |
| 112 | + // point temp node to the next of last i.e. first node |
| 113 | + temp!!.next = last.next |
| 114 | + last = temp.next |
| 115 | + } |
| 116 | + |
| 117 | + // travel to the node to be deleted |
| 118 | + while (temp!!.next != last && temp.next!!.data != key) { |
| 119 | + temp = temp.next |
| 120 | + } |
| 121 | + |
| 122 | + // if node to be deleted was found |
| 123 | + if (temp.next!!.data == key) { |
| 124 | + d = temp.next |
| 125 | + temp.next = d!!.next |
| 126 | + } |
| 127 | + return last |
| 128 | + } |
| 129 | + |
| 130 | + private fun traverse(last: Node?) { |
| 131 | + var p: Node? |
| 132 | + if (last == null) { |
| 133 | + println("List is empty.") |
| 134 | + return |
| 135 | + } |
| 136 | + p = last.next |
| 137 | + do { |
| 138 | + print(p!!.data.toString() + " ") |
| 139 | + p = p.next |
| 140 | + } while (p != last.next) |
| 141 | + } |
| 142 | + |
| 143 | + private fun reverse(head_ref: Node?): Node? { |
| 144 | + // if list is empty |
| 145 | + var head_ref: Node? = head_ref ?: return null |
| 146 | + |
| 147 | + // reverse procedure same as reversing a |
| 148 | + // singly linked list |
| 149 | + var prev: Node? = null |
| 150 | + var current = head_ref |
| 151 | + var next: Node? |
| 152 | + do { |
| 153 | + next = current!!.next |
| 154 | + current.next = prev |
| 155 | + prev = current |
| 156 | + current = next |
| 157 | + } while (current != head_ref) |
| 158 | + |
| 159 | + // adjusting the links so as to make the |
| 160 | + // last node point to the first node |
| 161 | + head_ref!!.next = prev |
| 162 | + head_ref = prev |
| 163 | + return head_ref |
| 164 | + } |
| 165 | + |
| 166 | + fun main(args: Array<String>) { |
| 167 | + val sc = Scanner(System.`in`) |
| 168 | + var last: Node? = null |
| 169 | + println("Enter number of nodes: ") |
| 170 | + val n = sc.nextInt() |
| 171 | + println("Enter the value of nodes: ") |
| 172 | + for (i in 0 until n) { |
| 173 | + val s = sc.nextInt() |
| 174 | + last = addEnd(last, s) |
| 175 | + } |
| 176 | + |
| 177 | + // last = addToEmpty(last, 6); |
| 178 | + // last = addEnd(last, 8); |
| 179 | + // last = addFront(last, 2); |
| 180 | + |
| 181 | + // last = addAfter(last, 10, 2); |
| 182 | + println("Linked List: ") |
| 183 | + traverse(last) |
| 184 | + last = reverse(last) |
| 185 | + println("\nReversed Linked List: ") |
| 186 | + traverse(last) |
| 187 | + |
| 188 | + // deleteNode(last, 8); |
| 189 | + // traverse(last); |
| 190 | + } |
| 191 | + |
| 192 | + class Node { |
| 193 | + var data = 0 |
| 194 | + var next: Node? = null |
| 195 | + } |
| 196 | +} |
0 commit comments