|
| 1 | +'''Contains Most of the Singly Linked List functions.\n |
| 2 | +'variable_name' = singly_linked_list.LinkedList() to use this an external module.\n |
| 3 | +'variable_name'.insert_front('element') \t,'variable_name'.insert_back('element'),\n |
| 4 | +'variable_name'.pop_front() are some of its functions.\n |
| 5 | +To print all of its Functions use print('variable_name'.__dir__()).\n |
| 6 | +Note:- 'variable_name' = singly_linked_list.LinkedList() This line is Important before using any of the function. |
| 7 | +
|
| 8 | +Author :- Mugen https://github.com/Mugendesu |
| 9 | +''' |
| 10 | + |
| 11 | +class Node: |
| 12 | + def __init__(self, val=None , next = None ): |
| 13 | + self.data = val |
| 14 | + self.next = next |
| 15 | + |
| 16 | +class LinkedList: |
| 17 | + |
| 18 | + def __init__(self): |
| 19 | + self.head = self.tail = None |
| 20 | + self.length = 0 |
| 21 | + |
| 22 | + def insert_front(self , data): |
| 23 | + node = Node(data , self.head) |
| 24 | + if self.head == None: |
| 25 | + self.tail = node |
| 26 | + self.head = node |
| 27 | + self.length += 1 |
| 28 | + |
| 29 | + def insert_back(self , data): |
| 30 | + node = Node(data ) |
| 31 | + if self.head == None: |
| 32 | + self.tail = self.head = node |
| 33 | + self.length += 1 |
| 34 | + else: |
| 35 | + self.tail.next = node |
| 36 | + self.tail = node |
| 37 | + self.length += 1 |
| 38 | + |
| 39 | + def insert_values(self , data_values : list): |
| 40 | + self.head = self.tail = None |
| 41 | + self.length = 0 |
| 42 | + for data in data_values: |
| 43 | + self.insert_back(data) |
| 44 | + |
| 45 | + def pop_front(self): |
| 46 | + if not self.head: |
| 47 | + print('List is Empty!') |
| 48 | + return |
| 49 | + |
| 50 | + temp = self.head |
| 51 | + self.head = self.head.next |
| 52 | + temp.next = None |
| 53 | + self.length -= 1 |
| 54 | + |
| 55 | + def pop_back(self): |
| 56 | + if not self.head: |
| 57 | + print('List is Empty!') |
| 58 | + return |
| 59 | + |
| 60 | + temp = self.head |
| 61 | + while temp.next != self.tail: |
| 62 | + temp = temp.next |
| 63 | + self.tail = temp |
| 64 | + temp.next = None |
| 65 | + self.length -= 1 |
| 66 | + |
| 67 | + def print(self): |
| 68 | + if self.head is None: |
| 69 | + print('Linked List is Empty!') |
| 70 | + return |
| 71 | + |
| 72 | + temp = self.head |
| 73 | + while temp: |
| 74 | + print(f'{temp.data} ->' , end = ' ') |
| 75 | + temp = temp.next |
| 76 | + print('NULL') |
| 77 | + |
| 78 | + def len(self): |
| 79 | + return self.length # O(1) length calculation |
| 80 | + # if self.head is None: |
| 81 | + # return 0 |
| 82 | + # count = 0 |
| 83 | + # temp = self.head |
| 84 | + # while temp: |
| 85 | + # count += 1 |
| 86 | + # temp = temp.next |
| 87 | + # return count |
| 88 | + |
| 89 | + def remove_at(self , idx): |
| 90 | + if idx < 0 or self.len() <= idx: |
| 91 | + raise Exception('Invalid Position') |
| 92 | + if idx == 0: |
| 93 | + self.head = self.head.next |
| 94 | + self.length -= 1 |
| 95 | + return |
| 96 | + temp = self.head |
| 97 | + dist = 0 |
| 98 | + while dist != idx-1: |
| 99 | + dist += 1 |
| 100 | + temp = temp.next |
| 101 | + temp.next = temp.next.next |
| 102 | + self.length -= 1 |
| 103 | + |
| 104 | + def insert_at(self , idx : int , data ): |
| 105 | + if idx < 0 or self.len() < idx: |
| 106 | + raise Exception('Invalid Position') |
| 107 | + if idx == 0: |
| 108 | + self.insert_front(data) |
| 109 | + return |
| 110 | + temp = self.head |
| 111 | + dist = 0 |
| 112 | + while dist != idx-1: |
| 113 | + dist += 1 |
| 114 | + temp = temp.next |
| 115 | + node = Node(data , temp.next) |
| 116 | + temp.next = node |
| 117 | + self.length += 1 |
| 118 | + |
| 119 | + def insert_after_value(self , idx_data , data): |
| 120 | + if not self.head : # For Empty List case |
| 121 | + print('List is Empty!') |
| 122 | + return |
| 123 | + |
| 124 | + if self.head.data == idx_data: # To insert after the Head Element |
| 125 | + self.insert_at(1 , data) |
| 126 | + return |
| 127 | + temp = self.head |
| 128 | + while temp: |
| 129 | + if temp.data == idx_data: |
| 130 | + node = Node(data , temp.next) |
| 131 | + temp.next = node |
| 132 | + self.length += 1 |
| 133 | + return |
| 134 | + temp = temp.next |
| 135 | + print('The Element is not in the List!') |
| 136 | + |
| 137 | + def remove_by_value(self , idx_data): |
| 138 | + temp = self.head |
| 139 | + if temp.data == idx_data: |
| 140 | + self.head = self.head.next |
| 141 | + self.length -= 1 |
| 142 | + temp.next = None |
| 143 | + return |
| 144 | + while temp.next != None: |
| 145 | + if temp.next.data == idx_data: |
| 146 | + temp.next = temp.next.next |
| 147 | + self.length -= 1 |
| 148 | + return |
| 149 | + |
| 150 | + temp = temp.next |
| 151 | + print('Element is not in the List!') |
| 152 | + |
| 153 | + def index(self , data): |
| 154 | + '''Returns the index of the Element''' |
| 155 | + if not self.head : |
| 156 | + print('List is Empty!') |
| 157 | + return |
| 158 | + idx = 0 |
| 159 | + temp = self.head |
| 160 | + while temp: |
| 161 | + if temp.data == data: return idx |
| 162 | + temp = temp.next |
| 163 | + idx += 1 |
| 164 | + print('The Element is not in the List!') |
| 165 | + |
| 166 | + def search(self , idx): |
| 167 | + '''Returns the Element at the Given Index''' |
| 168 | + if self.len() == 0 or idx >= self.len(): |
| 169 | + raise Exception('Invalid Position') |
| 170 | + return |
| 171 | + temp = self.head |
| 172 | + curr_idx = 0 |
| 173 | + while temp: |
| 174 | + if curr_idx == idx: |
| 175 | + return temp.data |
| 176 | + temp = temp.next |
| 177 | + curr_idx += 1 |
| 178 | + |
| 179 | + def reverse(self): |
| 180 | + if not self.head: |
| 181 | + print('The List is Empty!') |
| 182 | + return |
| 183 | + prev = c_next = None |
| 184 | + curr = self.head |
| 185 | + while curr != None: |
| 186 | + c_next = curr.next |
| 187 | + curr.next = prev |
| 188 | + prev = curr |
| 189 | + curr = c_next |
| 190 | + self.tail = self.head |
| 191 | + self.head = prev |
| 192 | + |
| 193 | + def mid_element(self): |
| 194 | + if not self.head: |
| 195 | + print('List is Empty!') |
| 196 | + return |
| 197 | + slow = self.head.next |
| 198 | + fast = self.head.next.next |
| 199 | + while fast != None and fast.next != None: |
| 200 | + slow = slow.next |
| 201 | + fast = fast.next.next |
| 202 | + return slow.data |
| 203 | + |
| 204 | + def __dir__(self): |
| 205 | + funcs = ['insert_front', 'insert_back','pop_front','pop_back','print','len','length','remove_at','insert_after_value','index','search','reverse','mid_element','__dir__'] |
| 206 | + return funcs |
| 207 | + |
| 208 | +def main(): |
| 209 | + ll : Node = LinkedList() |
| 210 | + |
| 211 | + # # ll.insert_front(1) |
| 212 | + # # ll.insert_front(2) |
| 213 | + # # ll.insert_front(3) |
| 214 | + # # ll.insert_back(0) |
| 215 | + # ll.insert_values(['ZeroTwo' , 'Asuna' , 'Tsukasa' , 'Seras' ]) |
| 216 | + # # ll.remove_at(3) |
| 217 | + # ll.insert_at(2 , 'Raeliana') |
| 218 | + # # ll.pop_front() |
| 219 | + # ll.insert_after_value('Raeliana' , 'MaoMao') |
| 220 | + # # print(ll.search(5)) |
| 221 | + # ll.remove_by_value('Tsukasa') |
| 222 | + # ll.reverse() |
| 223 | + |
| 224 | + # ll.print() |
| 225 | + # print(ll.mid_element()) |
| 226 | + # print(ll.length) |
| 227 | + print(ll.__dir__()) |
| 228 | + |
| 229 | + |
| 230 | + |
| 231 | + |
| 232 | + |
| 233 | +if __name__ == '__main__': |
| 234 | + main() |
0 commit comments