|
| 1 | + |
| 2 | +# A linked list node |
| 3 | +class Node: |
| 4 | + |
| 5 | + # Constructor to create a new node |
| 6 | + def __init__(self, data): |
| 7 | + self.data = data |
| 8 | + self.next = None |
| 9 | + self.prev = None |
| 10 | + |
| 11 | +# Class to create a Doubly Linked List |
| 12 | +class DoublyLinkedList: |
| 13 | + |
| 14 | + # Constructor for empty Doubly Linked List |
| 15 | + def __init__(self): |
| 16 | + self.head = None |
| 17 | + |
| 18 | + # Given a reference to the head of a list and an |
| 19 | + # integer, inserts a new node on the front of list |
| 20 | + def push(self, new_data): |
| 21 | + |
| 22 | + # 1. Allocates node |
| 23 | + # 2. Put the data in it |
| 24 | + new_node = Node(new_data) |
| 25 | + |
| 26 | + # 3. Make next of new node as head and |
| 27 | + # previous as None (already None) |
| 28 | + new_node.next = self.head |
| 29 | + |
| 30 | + # 4. change prev of head node to new_node |
| 31 | + if self.head is not None: |
| 32 | + self.head.prev = new_node |
| 33 | + |
| 34 | + # 5. move the head to point to the new node |
| 35 | + self.head = new_node |
| 36 | + |
| 37 | + # Given a node as prev_node, insert a new node after |
| 38 | + # the given node |
| 39 | + def insertAfter(self, prev_node, new_data): |
| 40 | + |
| 41 | + # 1. Check if the given prev_node is None |
| 42 | + if prev_node is None: |
| 43 | + print "the given previous node cannot be NULL" |
| 44 | + return |
| 45 | + |
| 46 | + # 2. allocate new node |
| 47 | + # 3. put in the data |
| 48 | + new_node = Node(new_data) |
| 49 | + |
| 50 | + # 4. Make net of new node as next of prev node |
| 51 | + new_node.next = prev_node.next |
| 52 | + |
| 53 | + # 5. Make prev_node as previous of new_node |
| 54 | + prev_node.next = new_node |
| 55 | + |
| 56 | + # 6. Make prev_node ass previous of new_node |
| 57 | + new_node.prev = prev_node |
| 58 | + |
| 59 | + # 7. Change previous of new_nodes's next node |
| 60 | + if new_node.next: |
| 61 | + new_node.next.prev = new_node |
| 62 | + |
| 63 | + # Given a reference to the head of DLL and integer, |
| 64 | + # appends a new node at the end |
| 65 | + def append(self, new_data): |
| 66 | + |
| 67 | + # 1. Allocates node |
| 68 | + # 2. Put in the data |
| 69 | + new_node = Node(new_data) |
| 70 | + |
| 71 | + # 3. This new node is going to be the last node, |
| 72 | + # so make next of it as None |
| 73 | + # (It already is initialized as None) |
| 74 | + |
| 75 | + # 4. If the Linked List is empty, then make the |
| 76 | + # new node as head |
| 77 | + if self.head is None: |
| 78 | + self.head = new_node |
| 79 | + return |
| 80 | + |
| 81 | + # 5. Else traverse till the last node |
| 82 | + last = self.head |
| 83 | + while last.next: |
| 84 | + last = last.next |
| 85 | + |
| 86 | + # 6. Change the next of last node |
| 87 | + last.next = new_node |
| 88 | + |
| 89 | + # 7. Make last node as previous of new node |
| 90 | + new_node.prev = last |
| 91 | + |
| 92 | + return |
| 93 | + |
| 94 | + # This function prints contents of linked list |
| 95 | + # starting from the given node |
| 96 | + def printList(self, node): |
| 97 | + |
| 98 | + print "\nTraversal in forward direction" |
| 99 | + while node: |
| 100 | + print " % d" % (node.data), |
| 101 | + last = node |
| 102 | + node = node.next |
| 103 | + |
| 104 | + print "\nTraversal in reverse direction" |
| 105 | + while last: |
| 106 | + print " % d" % (last.data), |
| 107 | + last = last.prev |
| 108 | + |
| 109 | +# Driver program to test above functions |
| 110 | + |
| 111 | + |
| 112 | +# Start with empty list |
| 113 | +llist = DoublyLinkedList() |
| 114 | + |
| 115 | +# Insert 6. So the list becomes 6->None |
| 116 | +llist.append(6) |
| 117 | + |
| 118 | +# Insert 7 at the beginning. |
| 119 | +# So linked list becomes 7->6->None |
| 120 | +llist.push(7) |
| 121 | + |
| 122 | +# Insert 1 at the beginning. |
| 123 | +# So linked list becomes 1->7->6->None |
| 124 | +llist.push(1) |
| 125 | + |
| 126 | +# Insert 4 at the end. |
| 127 | +# So linked list becomes 1->7->6->4->None |
| 128 | +llist.append(4) |
| 129 | + |
| 130 | +# Insert 8, after 7. |
| 131 | +# So linked list becomes 1->7->8->6->4->None |
| 132 | +llist.insertAfter(llist.head.next, 8) |
| 133 | + |
| 134 | +print "Created DLL is: ", |
| 135 | +llist.printList(llist.head) |
0 commit comments