|
| 1 | +# A complete working Python program to demonstrate all |
| 2 | +# stack operations using a doubly linked list |
| 3 | + |
| 4 | +class Node: |
| 5 | + def __init__(self, data): |
| 6 | + self.data = data # Assign data |
| 7 | + self.next = None # Initialize next as null |
| 8 | + self.prev = None # Initialize prev as null |
| 9 | + |
| 10 | +class Stack: |
| 11 | + """ |
| 12 | + >>> stack = Stack() |
| 13 | + >>> stack.is_empty() |
| 14 | + True |
| 15 | + >>> stack.print_stack() |
| 16 | + stack elements are: |
| 17 | + >>> for i in range(4): |
| 18 | + ... stack.push(i) |
| 19 | + ... |
| 20 | + >>> stack.is_empty() |
| 21 | + False |
| 22 | + >>> stack.print_stack() |
| 23 | + stack elements are: |
| 24 | + 3->2->1->0-> |
| 25 | + >>> stack.top() |
| 26 | + 3 |
| 27 | + >>> len(stack) |
| 28 | + 4 |
| 29 | + >>> stack.pop() |
| 30 | + 3 |
| 31 | + >>> stack.print_stack() |
| 32 | + stack elements are: |
| 33 | + 2->1->0-> |
| 34 | + """ |
| 35 | + def __init__(self): |
| 36 | + self.head = None |
| 37 | + |
| 38 | + def push(self, data): |
| 39 | + """add a Node to the stack""" |
| 40 | + if self.head is None: |
| 41 | + self.head = Node(data) |
| 42 | + else: |
| 43 | + new_node = Node(data) |
| 44 | + self.head.prev = new_node |
| 45 | + new_node.next = self.head |
| 46 | + new_node.prev = None |
| 47 | + self.head = new_node |
| 48 | + |
| 49 | + def pop(self): |
| 50 | + """pop the top element off the stack""" |
| 51 | + if self.head is None: |
| 52 | + return None |
| 53 | + else: |
| 54 | + temp = self.head.data |
| 55 | + self.head = self.head.next |
| 56 | + self.head.prev = None |
| 57 | + return temp |
| 58 | + |
| 59 | + def top(self): |
| 60 | + """return the top element of the stack""" |
| 61 | + return self.head.data |
| 62 | + |
| 63 | + def __len__(self): |
| 64 | + temp = self.head |
| 65 | + count = 0 |
| 66 | + while temp is not None: |
| 67 | + count += 1 |
| 68 | + temp = temp.next |
| 69 | + return count |
| 70 | + |
| 71 | + def is_empty(self): |
| 72 | + return self.head is None |
| 73 | + |
| 74 | + def print_stack(self): |
| 75 | + print("stack elements are:") |
| 76 | + temp = self.head |
| 77 | + while temp is not None: |
| 78 | + print(temp.data, end ="->") |
| 79 | + temp = temp.next |
| 80 | + |
| 81 | + |
| 82 | +# Code execution starts here |
| 83 | +if __name__=='__main__': |
| 84 | + |
| 85 | + # Start with the empty stack |
| 86 | + stack = Stack() |
| 87 | + |
| 88 | + # Insert 4 at the beginning. So stack becomes 4->None |
| 89 | + print("Stack operations using Doubly LinkedList") |
| 90 | + stack.push(4) |
| 91 | + |
| 92 | + # Insert 5 at the beginning. So stack becomes 4->5->None |
| 93 | + stack.push(5) |
| 94 | + |
| 95 | + # Insert 6 at the beginning. So stack becomes 4->5->6->None |
| 96 | + stack.push(6) |
| 97 | + |
| 98 | + # Insert 7 at the beginning. So stack becomes 4->5->6->7->None |
| 99 | + stack.push(7) |
| 100 | + |
| 101 | + # Print the stack |
| 102 | + stack.print_stack() |
| 103 | + |
| 104 | + # Print the top element |
| 105 | + print("\nTop element is ", stack.top()) |
| 106 | + |
| 107 | + # Print the stack size |
| 108 | + print("Size of the stack is ", len(stack)) |
| 109 | + |
| 110 | + # pop the top element |
| 111 | + stack.pop() |
| 112 | + |
| 113 | + # pop the top element |
| 114 | + stack.pop() |
| 115 | + |
| 116 | + # two elements have now been popped off |
| 117 | + stack.print_stack() |
| 118 | + |
| 119 | + # Print True if the stack is empty else False |
| 120 | + print("\nstack is empty:", stack.is_empty()) |
0 commit comments