|
| 1 | +# Class for creating Node |
| 2 | +class Node: |
| 3 | + def __init__(self, data=None, next=None, prev=None): |
| 4 | + self.data = data |
| 5 | + self.next = next |
| 6 | + self.prev = prev |
| 7 | + |
| 8 | +# Double linkedlist area |
| 9 | + |
| 10 | + |
| 11 | +class DLinkedList: |
| 12 | + # initializind head |
| 13 | + def __init__(self): |
| 14 | + self.head = None |
| 15 | + |
| 16 | + # insertion at start |
| 17 | + def addbeg(self, data): |
| 18 | + if self.head == None: |
| 19 | + node = Node(data, self.head, None) |
| 20 | + self.head = node |
| 21 | + else: |
| 22 | + node = Node(data, self.head, None) |
| 23 | + self.head.prev = node |
| 24 | + self.head = node |
| 25 | + |
| 26 | + # printing the list |
| 27 | + def prinnt(self): |
| 28 | + if self.head is None: |
| 29 | + print('empty') |
| 30 | + return |
| 31 | + temp = self.head |
| 32 | + st = '' |
| 33 | + while temp: |
| 34 | + st += '<-' + str(temp.data) + '->' |
| 35 | + temp = temp.next |
| 36 | + print(st) |
| 37 | + |
| 38 | + # insertion at end |
| 39 | + def addend(self, data): |
| 40 | + if self.head is None: |
| 41 | + self.head = Node(data, None, None) |
| 42 | + else: |
| 43 | + temp = self.head |
| 44 | + while temp.next: |
| 45 | + temp = temp.next |
| 46 | + node = Node(data, None, temp) |
| 47 | + temp.next = node |
| 48 | + |
| 49 | + #printing in forward |
| 50 | + def pforward(self): |
| 51 | + if self.head is None: |
| 52 | + print('empty') |
| 53 | + return |
| 54 | + temp = self.head |
| 55 | + st = '' |
| 56 | + while temp: |
| 57 | + st += str(temp.data) + '->' |
| 58 | + temp = temp.next |
| 59 | + print(st) |
| 60 | + |
| 61 | + # Finding the length of linkedlist |
| 62 | + def lenn(self): |
| 63 | + if self.head is None: |
| 64 | + print('empty') |
| 65 | + return |
| 66 | + count = 0 |
| 67 | + temp = self.head |
| 68 | + while temp: |
| 69 | + count += 1 |
| 70 | + temp = temp.next |
| 71 | + return count |
| 72 | + |
| 73 | + # finding last node |
| 74 | + def lastnode(self): |
| 75 | + temp = self.head |
| 76 | + while temp.next: |
| 77 | + temp = temp.next |
| 78 | + return temp |
| 79 | + |
| 80 | + # Inserting element at linkedlist with index |
| 81 | + def addmid(self, index, data): |
| 82 | + if index < 0 or index >= self.lenn(): |
| 83 | + print('Invalid index') |
| 84 | + return |
| 85 | + |
| 86 | + if index == 0: |
| 87 | + self.addbeg(data) |
| 88 | + return |
| 89 | + |
| 90 | + if index == self.lenn() - 1: |
| 91 | + self.addend(data) |
| 92 | + return |
| 93 | + |
| 94 | + temp = self.head |
| 95 | + count = 0 |
| 96 | + while temp: |
| 97 | + if count == index - 1: |
| 98 | + node = Node(data, temp.next, temp) |
| 99 | + temp.next.prev = node |
| 100 | + temp.next = node |
| 101 | + break |
| 102 | + temp = temp.next |
| 103 | + count += 1 |
| 104 | + |
| 105 | + #print in reverse |
| 106 | + def pbackward(self): |
| 107 | + if self.head is None: |
| 108 | + print('empty') |
| 109 | + return |
| 110 | + temp = self.lastnode() |
| 111 | + st = '' |
| 112 | + while temp: |
| 113 | + st += '<-' + str(temp.data) |
| 114 | + temp = temp.prev |
| 115 | + print(st) |
| 116 | + |
| 117 | + # removing element at linkedlist with index |
| 118 | + def remove(self, index): |
| 119 | + |
| 120 | + if index < 0 or index >= self.lenn(): |
| 121 | + print('Invalid index') |
| 122 | + return |
| 123 | + if index == 0: |
| 124 | + self.head = self.head.next |
| 125 | + self.head.prev.next = None |
| 126 | + self.head.prev = None |
| 127 | + return |
| 128 | + |
| 129 | + if index == self.lenn() - 1: |
| 130 | + temp = self.head |
| 131 | + while temp.next: |
| 132 | + temp = temp.next |
| 133 | + temp.prev.next = None |
| 134 | + temp.prev = None |
| 135 | + return |
| 136 | + |
| 137 | + temp = self.head |
| 138 | + count = 0 |
| 139 | + while temp: |
| 140 | + if count == index: |
| 141 | + temp.prev.next = temp.next |
| 142 | + temp.next.prev = temp.prev |
| 143 | + temp.prev = None |
| 144 | + temp.next = None |
| 145 | + |
| 146 | + temp = temp.next |
| 147 | + count += 1 |
| 148 | + |
| 149 | + |
| 150 | +if __name__ == '__main__': |
| 151 | + li = DLinkedList() |
| 152 | + a = int(input('Enter the value to insert at beg: ')) |
| 153 | + li.addbeg(a) |
| 154 | + b = int(input('Enter the value to insert at beg: ')) |
| 155 | + li.addbeg(b) |
| 156 | + c = int(input('Enter the value to insert at end: ')) |
| 157 | + li.addend(c) |
| 158 | + d = int(input('Enter the value to insert at end: ')) |
| 159 | + li.addend(d) |
| 160 | + li.prinnt() |
| 161 | + print("Priting in forward: ", end="") |
| 162 | + li.pforward() |
| 163 | + print('Printing in backward: ', end="") |
| 164 | + li.pbackward() |
| 165 | + print('Len of linked list is ', end='') |
| 166 | + li.lenn() |
| 167 | + e = int(input('Enter the index to be removed: ')) |
| 168 | + li.remove(e) |
| 169 | + li.prinnt() |
| 170 | + f = int(input('Enter the index: ')) |
| 171 | + g = int(input('Enter the number: ')) |
| 172 | + li.addmid(f, g) |
| 173 | + li.prinnt() |
0 commit comments