|
| 1 | +# Python program for all basic functionalities of Singly Linked List |
| 2 | + |
| 3 | +class Node: |
| 4 | + # Constructor to initialize the node object |
| 5 | + def __init__(self, data): |
| 6 | + self.data = data |
| 7 | + self.next = None |
| 8 | + |
| 9 | +class LinkedList: |
| 10 | + # Constructor to initialize head |
| 11 | + def __init__(self): |
| 12 | + self.head = None |
| 13 | + |
| 14 | + def insertNodeAtFront(self, ndata): |
| 15 | + nnode = Node(ndata) |
| 16 | + nnode.next = self.head |
| 17 | + self.head = nnode |
| 18 | + |
| 19 | + def insertNodeAtEnd(self, ndata): |
| 20 | + nnode = Node(ndata) |
| 21 | + if self.head == None: |
| 22 | + self.head = nnode |
| 23 | + else: |
| 24 | + last = self.head |
| 25 | + while last.next != None: |
| 26 | + last = last.next |
| 27 | + last.next = nnode |
| 28 | + |
| 29 | + def insertNodeAtPosition(self, index, ndata): |
| 30 | + if index == 1: |
| 31 | + insertNodeAtFront(self, ndata) |
| 32 | + else: |
| 33 | + cur = 1 |
| 34 | + pos = self.head |
| 35 | + while cur < index-1 and pos != None: |
| 36 | + pos = pos.next |
| 37 | + cur += 1 |
| 38 | + if pos == None: |
| 39 | + insertNodeAtEnd(self, ndata) |
| 40 | + else: |
| 41 | + nnode = Node(ndata) |
| 42 | + nnode.next = pos.next |
| 43 | + pos.next = nnode |
| 44 | + |
| 45 | + def deleteNodeAtFront(self): |
| 46 | + if self.head == None: |
| 47 | + print("Empty list, nothing to delete!") |
| 48 | + else: |
| 49 | + self.head = self.head.next |
| 50 | + |
| 51 | + def deleteNodeAtEnd(self): |
| 52 | + if self.head == None: |
| 53 | + print("Empty list, nothing to delete!") |
| 54 | + elif self.head.next == None: |
| 55 | + self.head=None |
| 56 | + else: |
| 57 | + pos = self.head |
| 58 | + while pos.next.next != None: |
| 59 | + pos = pos.next |
| 60 | + pos.next = None |
| 61 | + |
| 62 | + def deleteNodeAtPosition(self, index): |
| 63 | + if self.head == None: |
| 64 | + print("Empty list, nothing to delete!") |
| 65 | + return |
| 66 | + pos = self.head |
| 67 | + if index == 1: |
| 68 | + self.head = pos.next |
| 69 | + temp = None |
| 70 | + return |
| 71 | + for ind in range(1,index-1): |
| 72 | + pos = pos.next |
| 73 | + if pos == None: |
| 74 | + break |
| 75 | + if pos == None or pos.next == None: |
| 76 | + print("Index more than number of nodes!") |
| 77 | + else: |
| 78 | + next = pos.next.next |
| 79 | + pos.next = None |
| 80 | + pos.next = next |
| 81 | + |
| 82 | + def traverseList(self): |
| 83 | + if self.head == None: |
| 84 | + print("Empty list, nothing to print!") |
| 85 | + else: |
| 86 | + n = self.head |
| 87 | + while n != None: |
| 88 | + print(n.data) |
| 89 | + n = n.next |
| 90 | + |
| 91 | + def countNodes(self): |
| 92 | + cnt = 0 |
| 93 | + ind = self.head |
| 94 | + while ind != None: |
| 95 | + cnt += 1 |
| 96 | + ind = ind.next |
| 97 | + print(cnt) |
| 98 | + |
| 99 | +if __name__=='__main__': |
| 100 | + newLinkedList = LinkedList() |
| 101 | + print("SINGLY LINKED LIST") |
| 102 | + print("1. Insert Node at Front") |
| 103 | + print("2. Insert Node at End") |
| 104 | + print("3. Insert Node at Position") |
| 105 | + print("4. Delete Node at Front") |
| 106 | + print("5. Delete Node at End") |
| 107 | + print("6. Delete Node at Position") |
| 108 | + print("7. Print the List") |
| 109 | + print("8. Count nodes in the List") |
| 110 | + print("9. Exit") |
| 111 | + print("--Use 1-based indexing--") |
| 112 | + while(True): |
| 113 | + ch = int(input("Enter your choice: ")) |
| 114 | + if ch == 1: |
| 115 | + ndata = int(input("Enter the element to insert: ")) |
| 116 | + newLinkedList.insertNodeAtFront(ndata) |
| 117 | + elif ch == 2: |
| 118 | + ndata = int(input("Enter the element to insert: ")) |
| 119 | + newLinkedList.insertNodeAtEnd(ndata) |
| 120 | + elif ch == 3: |
| 121 | + ndata = int(input("Enter the element to insert: ")) |
| 122 | + ind = int(input("Enter index to insert this element: ")) |
| 123 | + newLinkedList.insertNodeAtPosition(ind,ndata) |
| 124 | + elif ch == 4: |
| 125 | + newLinkedList.deleteNodeAtFront() |
| 126 | + elif ch == 5: |
| 127 | + newLinkedList.deleteNodeAtEnd() |
| 128 | + elif ch == 6: |
| 129 | + ind = int(input("Enter index to delete element: ")) |
| 130 | + newLinkedList.deleteNodeAtPosition(ind) |
| 131 | + elif ch == 7: |
| 132 | + newLinkedList.traverseList() |
| 133 | + elif ch == 8: |
| 134 | + newLinkedList.countNodes() |
| 135 | + elif ch == 9: |
| 136 | + break |
| 137 | + else: |
| 138 | + print("Enter a valid choice (1-9)") |
| 139 | + |
| 140 | + |
| 141 | +# --- EXAMPLE --- |
| 142 | + |
| 143 | +# SINGLY LINKED LIST |
| 144 | +# 1. Insert Node at Front |
| 145 | +# 2. Insert Node at End |
| 146 | +# 3. Insert Node at Position |
| 147 | +# 4. Delete Node at Front |
| 148 | +# 5. Delete Node at End |
| 149 | +# 6. Delete Node at Position |
| 150 | +# 7. Print the List |
| 151 | +# 8. Count nodes in the List |
| 152 | +# 9. Exit |
| 153 | +# --Use 1-based indexing-- |
| 154 | +# Enter your choice: 1 |
| 155 | +# Enter the element to insert: 10 |
| 156 | +# Enter your choice: 2 |
| 157 | +# Enter the element to insert: 5 |
| 158 | +# Enter your choice: 3 |
| 159 | +# Enter the element to insert: 3 |
| 160 | +# Enter index to insert this element: 2 |
| 161 | +# Enter your choice: 7 |
| 162 | +# 10 |
| 163 | +# 3 |
| 164 | +# 5 |
| 165 | +# Enter your choice: 6 |
| 166 | +# Enter index to delete element: 2 |
| 167 | +# Enter your choice: 7 |
| 168 | +# 10 |
| 169 | +# 5 |
| 170 | +# Enter your choice: 8 |
| 171 | +# 2 |
| 172 | +# Enter your choice: 4 |
| 173 | +# Enter your choice: 7 |
| 174 | +# 5 |
| 175 | +# Enter your choice: 5 |
| 176 | +# Enter your choice: 7 |
| 177 | +# Empty list, nothing to print! |
| 178 | +# Enter your choice: 10 |
| 179 | +# Enter a valid choice (1-9) |
| 180 | +# Enter your choice: 9 |
0 commit comments