|
| 1 | +#Node class |
| 2 | +class Node: |
| 3 | + # Function to initialize the node object |
| 4 | + def __init__(self, data) -> None: |
| 5 | + self.data = data # Assign data to instance variable |
| 6 | + self.next = None # initialize next with none/null |
| 7 | + self.prev = None # Initialize prev with None/null |
| 8 | + |
| 9 | +class DoublyLinkedList: |
| 10 | + # Function to initialize LinkedList object |
| 11 | + def __init__(self) -> None: |
| 12 | + self.head = None # Initialize linked list with None/null as empty list |
| 13 | + self.tail = None |
| 14 | + |
| 15 | + # Function to add element at the start of linkedlist |
| 16 | + def add(self, data): |
| 17 | + # Create new Node, which will have data and next pointer as null/None |
| 18 | + temp = Node(data) |
| 19 | + |
| 20 | + # if DoublyLinkedList itself is null/empty |
| 21 | + if (self.head == None): |
| 22 | + self.head = self.tail = temp |
| 23 | + return |
| 24 | + |
| 25 | + # Set next of newly created node's next pointer to tail, as we are adding at starting of linkedlist |
| 26 | + self.tail.next = temp |
| 27 | + # As new node next points to head, we have to create reverse link to temp as well |
| 28 | + temp.prev = self.tail |
| 29 | + # Now we have to reset our head to point to the first element of linkedlist again |
| 30 | + self.tail, self.tail.next = temp, None |
| 31 | + |
| 32 | + def insert(self, position, data): |
| 33 | + node = Node(data) |
| 34 | + if (position == 0): |
| 35 | + node.next = self.head |
| 36 | + self.head.prev = node |
| 37 | + self.head = node |
| 38 | + elif (position == 1): |
| 39 | + temp = self.head.next |
| 40 | + temp.prev = node |
| 41 | + node.next = temp |
| 42 | + self.head.next = node |
| 43 | + node.prev = self.head |
| 44 | + else: |
| 45 | + temp = self.head |
| 46 | + for i in range(0, position): |
| 47 | + temp = temp.next |
| 48 | + |
| 49 | + if (temp == None): |
| 50 | + self.add(data) |
| 51 | + else: |
| 52 | + prev = temp.prev |
| 53 | + temp.prev = node |
| 54 | + node.next = temp |
| 55 | + node.prev = prev |
| 56 | + prev.next = node |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | + def print(self): |
| 61 | + temp = self.head |
| 62 | + while (temp): |
| 63 | + print(temp.data, end = ' ') |
| 64 | + temp = temp.next |
| 65 | + print('\n') |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | + def delete(self, data): |
| 70 | + # reference head in temporary variable |
| 71 | + temp = self.head |
| 72 | + |
| 73 | + # best case, if head is the element that we want to delete |
| 74 | + if (temp): |
| 75 | + if (temp.data == data): |
| 76 | + self.head = temp.next |
| 77 | + return |
| 78 | + |
| 79 | + # condition for other element apart from head |
| 80 | + while (temp): |
| 81 | + if (temp.data == data): |
| 82 | + break |
| 83 | + else: |
| 84 | + temp = temp.next |
| 85 | + |
| 86 | + # check if no data found after full iteration then return, |
| 87 | + if (temp == None): |
| 88 | + return |
| 89 | + |
| 90 | + # if data is matched, then we have to dereference matched node and remove link, and move it to |
| 91 | + # next link immediate to temp next |
| 92 | + prev = temp.prev |
| 93 | + prev.next = temp.next |
| 94 | + |
| 95 | + |
| 96 | +# Testing portion |
| 97 | +if __name__=='__main__': |
| 98 | + |
| 99 | + llist = DoublyLinkedList() |
| 100 | + |
| 101 | + llist.add(1) |
| 102 | + llist.add(2) |
| 103 | + llist.add(3) |
| 104 | + llist.add(4) |
| 105 | + llist.add(5) |
| 106 | + llist.print() |
| 107 | + llist.delete(3) |
| 108 | + llist.print() |
| 109 | + llist.insert(0, 6) |
| 110 | + llist.print() |
| 111 | + llist.insert(2, 8) |
| 112 | + llist.print() |
| 113 | + |
| 114 | + |
0 commit comments