|
| 1 | +# Author: OMKAR PATHAK |
| 2 | + |
| 3 | +# Linked List and Node can be accomodated in separate classes for convenience |
| 4 | + |
| 5 | +class Node(object): |
| 6 | + # Each node has its data and a pointer that points to next node in the Linked List |
| 7 | + def __init__(self, data, next = None): |
| 8 | + self.data = data; |
| 9 | + self.next = next; |
| 10 | + |
| 11 | + # function to set data |
| 12 | + def setData(self, data): |
| 13 | + self.data = data; |
| 14 | + |
| 15 | + # function to get data of a particular node |
| 16 | + def getData(self): |
| 17 | + return self.data |
| 18 | + |
| 19 | + # function to set next node |
| 20 | + def setNext(self, next): |
| 21 | + self.next = next |
| 22 | + |
| 23 | + # function to get the next node |
| 24 | + def getNext(self): |
| 25 | + return self.next |
| 26 | + |
| 27 | +class LinkedList(object): |
| 28 | + # Defining the head of the linked list |
| 29 | + def __init__(self): |
| 30 | + self.head = None |
| 31 | + |
| 32 | + # printing the data in the linked list |
| 33 | + def printLinkedList(self): |
| 34 | + temp = self.head |
| 35 | + while(temp): |
| 36 | + print(temp.data, end=' ') |
| 37 | + temp = temp.next |
| 38 | + |
| 39 | + # inserting the node at the beginning |
| 40 | + def insertAtStart(self, data): |
| 41 | + if self.head == None: |
| 42 | + newNode = Node(data) |
| 43 | + self.head = newNode |
| 44 | + else: |
| 45 | + newNode = Node(data) |
| 46 | + newNode.next = self.head |
| 47 | + self.head = newNode |
| 48 | + |
| 49 | + # inserting the node in between the linked list (after a specific node) |
| 50 | + def insertBetween(self, previousNode, data): |
| 51 | + if (previousNode.next is None): |
| 52 | + print('Previous node should have next node!') |
| 53 | + else: |
| 54 | + newNode = Node(data) |
| 55 | + newNode.next = previousNode.next |
| 56 | + previousNode.next = newNode |
| 57 | + |
| 58 | + # inserting at the end of linked list |
| 59 | + def insertAtEnd(self, data): |
| 60 | + newNode = Node(data) |
| 61 | + temp = self.head |
| 62 | + while(temp.next != None): # get last node |
| 63 | + temp = temp.next |
| 64 | + temp.next = newNode |
| 65 | + |
| 66 | + # deleting an item based on data(or key) |
| 67 | + def delete(self, data): |
| 68 | + temp = self.head |
| 69 | + # if data/key is found in head node itself |
| 70 | + if (temp.next is not None): |
| 71 | + if(temp.data == data): |
| 72 | + self.head = temp.next |
| 73 | + temp = None |
| 74 | + return |
| 75 | + else: |
| 76 | + # else search all the nodes |
| 77 | + while(temp.next != None): |
| 78 | + if(temp.data == data): |
| 79 | + break |
| 80 | + prev = temp #save current node as previous so that we can go on to next node |
| 81 | + temp = temp.next |
| 82 | + |
| 83 | + # node not found |
| 84 | + if temp == None: |
| 85 | + return |
| 86 | + |
| 87 | + prev.next = temp.next |
| 88 | + return |
| 89 | + |
| 90 | + # iterative search |
| 91 | + def search(self, node, data): |
| 92 | + if node == None: |
| 93 | + return False |
| 94 | + if node.data == data: |
| 95 | + return True |
| 96 | + return self.search(node.getNext(), data) |
| 97 | + |
| 98 | +if __name__ == '__main__': |
| 99 | + List = LinkedList() |
| 100 | + List.head = Node(1) # create the head node |
| 101 | + node2 = Node(2) |
| 102 | + List.head.setNext(node2) # head node's next --> node2 |
| 103 | + node3 = Node(3) |
| 104 | + node2.setNext(node3) # node2's next --> node3 |
| 105 | + List.insertAtStart(4) # node4's next --> head-node --> node2 --> node3 |
| 106 | + List.insertBetween(node2, 5) # node2's next --> node5 |
| 107 | + List.insertAtEnd(6) |
| 108 | + List.printLinkedList() |
| 109 | + print() |
| 110 | + List.delete(3) |
| 111 | + List.printLinkedList() |
| 112 | + print() |
| 113 | + print(List.search(List.head, 1)) |
0 commit comments