├── README.md └── LinkedList.py /README.md: -------------------------------------------------------------------------------- 1 | # Linked-List-in-Python 2 | Implementation of Linked List in python 3 | 4 | LinkedList.py is a simple implementation of Linked List in python. 5 | I have refered various resources to make the different methods of the Linked List. 6 | 7 | 8 | Following are the methods implemented 9 | 10 | 1. isEmpty() : method returns true if the list is empty 11 | 12 | 2. addToStart(): method to add a node at starting 13 | 14 | 3. addToEnd() : method to add a node at the end 15 | 16 | 4. display() : method to display all the elements of the Linked List 17 | 18 | 5. length() : method that returns the length of the Linked List 19 | 20 | 6. insert() : method to insert element at a given position in the Linked List 21 | 22 | 7. removePosition() : method to delete a element at a given position 23 | 24 | 8. remove() : method to delete a data element 25 | 26 | 9. findMin() : method that returns the max element 27 | 28 | 10. findMax() : method that returns the min element 29 | 30 | 11. count() : method that returns the occurences of an element 31 | 32 | 12. pop() : pop method removes last element of the Linked List 33 | 34 | 13. tostring() : method that returns a string of all elements of the String 35 | 36 | 14. copy() : method that returns the copy of the list 37 | 38 | 15. clear() : method that clears the Linked List 39 | 40 | 16. reverse() : method that returns reversed linked list 41 | 42 | 17. push() : method pushes element to the Linked List 43 | 44 | 18. index() : method that returns index of a particular element 45 | 46 | 19. atIndex() : method that returns element at a particular position 47 | 48 | 20. toList() : method returns builtin List of python consisting of Elements of Linked List 49 | 50 | 21. toSet() : method returns builtin Set of python consisting of Elements of Linked List 51 | 52 | 22. sort() : method that sorts Linked List 53 | 54 | 23. sorted() : method returns new instance of the sorted LinkedList without changing original Linked List 55 | 56 | If you think you have a better implementation for any method or if you have any doubts or if you want some method implementations please feel free to comment. 57 | -------------------------------------------------------------------------------- /LinkedList.py: -------------------------------------------------------------------------------- 1 | # program to implement Linked List 2 | 3 | class LinkedList: 4 | def __init__(self): 5 | self.head = None 6 | 7 | # method adds elements to the left of the Linked List 8 | def addToStart(self, data): 9 | # create a temporary node 10 | tempNode = Node(data) 11 | tempNode.setLink(self.head) 12 | self.head = tempNode 13 | del tempNode 14 | 15 | # method adds elements to the right of the Linked List 16 | def addToEnd(self, data): 17 | start = self.head 18 | tempNode = Node(data) 19 | while start.getNextNode(): 20 | start = start.getNextNode() 21 | start.setLink(tempNode) 22 | del tempNode 23 | return True 24 | 25 | # method displays Linked List 26 | def display(self): 27 | start = self.head 28 | if start is None: 29 | print("Empty List!!!") 30 | return False 31 | 32 | while start: 33 | print(str(start.getData()), end=" ") 34 | start = start.link 35 | if start: 36 | print("-->", end=" ") 37 | print() 38 | 39 | # method returns length of linked list 40 | def length(self): 41 | start = self.head 42 | size = 0 43 | while start: 44 | size += 1 45 | start = start.getNextNode() 46 | # print(size) 47 | return size 48 | 49 | # method returns index of the recieved data 50 | def index(self, data): 51 | start = self.head 52 | position = 1 53 | 54 | while start: 55 | if start.getData() == data: 56 | return position 57 | else: 58 | position += 1 59 | start = start.getNextNode() 60 | 61 | 62 | # method removes item passed from the Linked List 63 | def remove(self, item): 64 | start = self.head 65 | previous = None 66 | found = False 67 | 68 | # search element in list 69 | while not found: 70 | if start.getData() == item: 71 | found = True 72 | else: 73 | previous = start 74 | start = start.getNextNode() 75 | 76 | # if previous is None then the data is found at first position 77 | if previous is None: 78 | self.head = start.getNextNode() 79 | else: 80 | previous.setLink(start.getNextNode()) 81 | return found 82 | 83 | # method returns max element from the List 84 | def Max(self): 85 | start = self.head 86 | largest = start.getData() 87 | while start: 88 | if largest < start.getData(): 89 | largest = start.getData() 90 | start = start.getNextNode() 91 | return largest 92 | 93 | # method returns minimum element of Linked list 94 | def Min(self): 95 | start = self.head 96 | smallest = start.getData() 97 | while start: 98 | if smallest > start.getData(): 99 | smallest = start.getData() 100 | start = start.getNextNode() 101 | return smallest 102 | 103 | # method pushes element to the Linked List 104 | def push(self, data): 105 | self.addToEnd(data) 106 | return True 107 | 108 | # method removes and returns the last element from the Linked List 109 | def pop(self): 110 | start = self.head 111 | previous = None 112 | 113 | while start.getNextNode(): 114 | previous = start 115 | start = start.getNextNode() 116 | 117 | if previous is None: 118 | self.head = None 119 | else: 120 | previous.setLink(None) 121 | data = start.getData() 122 | del start 123 | return data 124 | 125 | 126 | # method returns the element at given position 127 | def atIndex(self, position): 128 | start = self.head 129 | position = int(position) 130 | pos = 1 131 | while pos != position: 132 | start = start.getNextNode() 133 | pos += 1 134 | 135 | data = start.getData() 136 | return data 137 | 138 | # method returns a copy of the current Linked List 139 | def copy(self): 140 | temp = LinkedList() 141 | start = self.head 142 | 143 | temp.addToStart(start.getData()) 144 | start = start.getNextNode() 145 | 146 | while start: 147 | temp.addToEnd(start.getData()) 148 | start = start.getNextNode() 149 | 150 | return temp 151 | 152 | # method to clear LinkedList 153 | def clear(self): 154 | self.head = None 155 | return True 156 | 157 | # method returns and removes element at recieved position 158 | def removePosition(self, position): 159 | data = self.atIndex(position) 160 | self.remove(data) 161 | return data 162 | 163 | # method returns string of elements of Linked list 164 | # the Elements are seperated by seperator if passed else all elements are appended 165 | def toString(self, seperator=""): 166 | start = self.head 167 | finalString = "" 168 | while start: 169 | tempString = start.getData() 170 | finalString += str(tempString) 171 | start = start.getNextNode() 172 | 173 | # if next node exists only the append seperator 174 | if start: 175 | finalString += seperator 176 | 177 | return finalString 178 | 179 | # method returns count of Element recieved 180 | def count(self, element): 181 | start = self.head 182 | count1 = 0 183 | while start: 184 | if start.getData() == element: 185 | count1 += 1 186 | start = start.getNextNode() 187 | return count1 188 | 189 | # method returns builtin List of python consisting of Elements of LinkedList 190 | def toList(self): 191 | start = self.head 192 | tempList = [] 193 | while start: 194 | tempElement = start.getData() 195 | tempList.append(tempElement) 196 | start = start.getNextNode() 197 | return tempList 198 | 199 | # method returns builtin List of python consisting of Elements of LinkedList 200 | 201 | # method returns builtin Set of python consisting of Elements of LinkedList 202 | def toSet(self): 203 | start = self.head 204 | tempSet = set() 205 | while start: 206 | tempElement = start.getData() 207 | if tempElement not in tempSet: 208 | tempSet.add(tempElement) 209 | start = start.getNextNode() 210 | return tempSet 211 | 212 | # method reverses the LinkedList 213 | def reverse(self): 214 | start = self.head 215 | tempNode = None 216 | previousNode = None 217 | 218 | while start: 219 | tempNode = start.getNextNode() 220 | start.setLink(previousNode) 221 | previousNode = start 222 | start = tempNode 223 | 224 | self.head = previousNode 225 | return True 226 | 227 | # method that sorts LinkedList 228 | def sort(self): 229 | start = self.head 230 | beginNode = start 231 | while beginNode: 232 | tempNode = beginNode 233 | tempNode2 = beginNode 234 | smallest = beginNode.getData() 235 | while tempNode: 236 | if smallest > tempNode.getData(): 237 | smallest = tempNode.getData() 238 | tempNode2 = tempNode 239 | tempNode = tempNode.getNextNode() 240 | 241 | # swap data of beginNode and tempNode2 242 | temp = beginNode.getData() 243 | beginNode.updateData(tempNode2.getData()) 244 | tempNode2.updateData(temp) 245 | 246 | beginNode = beginNode.getNextNode() 247 | 248 | # method returns new instance of the sorted LinkedList without changing original LinkedList 249 | def sorted(self): 250 | start = self.head 251 | tempList = self.copy() 252 | tempList.sort() 253 | return tempList 254 | 255 | 256 | 257 | # node class 258 | class Node: 259 | # default value of data and link is none if no data is passed 260 | def __init__(self, data=None, link=None): 261 | self.data = data 262 | self.link = link 263 | 264 | # method to update the data feild of Node 265 | def updateData(self, data): 266 | self.data = data 267 | 268 | # method to set Link feild the Node 269 | def setLink(self, node): 270 | self.link = node 271 | 272 | # method returns data feild of the Node 273 | def getData(self): 274 | return self.data 275 | 276 | # method returns address of the next Node 277 | def getNextNode(self): 278 | return self.link 279 | 280 | 281 | # main method 282 | 283 | # creating LinkedList 284 | myList = LinkedList() 285 | 286 | # adding some elements to the start of LinkedList 287 | myList.addToStart(5) 288 | myList.addToStart(4) 289 | myList.addToStart(3) 290 | myList.addToStart(2) 291 | myList.addToStart(1) 292 | 293 | 294 | myList.display() 295 | 296 | # adding some elements to the End of the LinkedList 297 | myList.addToEnd(12) 298 | myList.addToEnd(13) 299 | myList.addToEnd(3) 300 | myList.display() 301 | 302 | # printing Length 303 | print(myList.length()) 304 | 305 | # printing index of an element 306 | print(myList.index(3)) 307 | 308 | # printing element at a particular index 309 | print(myList.atIndex(5)) 310 | 311 | # removing an element 312 | print(myList.remove(12)) 313 | 314 | # removing element from a particular position 315 | myList.removePosition(2) 316 | 317 | myList.display() 318 | 319 | # printing max and min element 320 | print(myList.Max()) 321 | print(myList.Min()) 322 | 323 | # pushing and poping elements 324 | print(myList.push(31)) 325 | myList.display() 326 | print(myList.pop()) 327 | myList.display() 328 | 329 | # creating a copy of the Linked List 330 | myList2 = myList.copy() 331 | myList2.display() 332 | 333 | # removing all elements from the LinkedList 334 | myList2.clear() 335 | myList2.display() 336 | 337 | 338 | # printing a string of elements of the LinkedList 339 | print(myList.toString(",")) 340 | 341 | # printing count of particular element in the List 342 | print(myList.count(3)) 343 | 344 | # making a builtIn List from the LinkedList 345 | newList = myList.toList() 346 | print(newList) 347 | 348 | # making a List from the LinkedList 349 | newSet = myList.toSet() 350 | print(newSet) 351 | 352 | # reversing the LinkedLkst 353 | myList.reverse() 354 | myList.display() 355 | 356 | # making a sorted LinkedList out of the Original 357 | myList3 = myList.sorted() 358 | myList3.display() 359 | 360 | # sorting the LinkedList 361 | myList.sort() 362 | myList.display() 363 | --------------------------------------------------------------------------------