class Node:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
class Stack:
"""
>>> stack = Stack()
>>> stack.is_empty()
True
>>> stack.print_stack()
stack elements are:
>>> for i in range(4):
... stack.push(i)
...
>>> stack.is_empty()
False
>>> stack.print_stack()
stack elements are:
3->2->1->0->
>>> stack.top()
3
>>> len(stack)
4
>>> stack.pop()
3
>>> stack.print_stack()
stack elements are:
2->1->0->
"""
def __init__(self):
self.head = None
def push(self, data):
"""add a Node to the stack"""
if self.head is None:
self.head = Node(data)
else:
new_node = Node(data)
self.head.prev = new_node
new_node.next = self.head
new_node.prev = None
self.head = new_node
def pop(self):
"""pop the top element off the stack"""
if self.head is None:
return None
else:
temp = self.head.data
self.head = self.head.next
self.head.prev = None
return temp
def top(self):
"""return the top element of the stack"""
return self.head.data
def __len__(self):
temp = self.head
count = 0
while temp is not None:
count += 1
temp = temp.next
return count
def is_empty(self):
return self.head is None
def print_stack(self):
print("stack elements are:")
temp = self.head
while temp is not None:
print(temp.data, end="->")
temp = temp.next
if __name__ == "__main__":
stack = Stack()
print("Stack operations using Doubly LinkedList")
stack.push(4)
stack.push(5)
stack.push(6)
stack.push(7)
stack.print_stack()
print("\nTop element is ", stack.top())
print("Size of the stack is ", len(stack))
stack.pop()
stack.pop()
stack.print_stack()
print("\nstack is empty:", stack.is_empty())