2019-12-26 11:50:12 +00:00
|
|
|
# A complete working Python program to demonstrate all
|
|
|
|
# stack operations using a doubly linked list
|
|
|
|
|
2021-10-26 19:12:46 +00:00
|
|
|
from __future__ import annotations
|
2019-12-26 11:50:12 +00:00
|
|
|
|
2021-10-26 19:12:46 +00:00
|
|
|
from typing import Generic, TypeVar
|
|
|
|
|
|
|
|
T = TypeVar("T")
|
|
|
|
|
|
|
|
|
|
|
|
class Node(Generic[T]):
|
|
|
|
def __init__(self, data: T):
|
2019-12-26 11:50:12 +00:00
|
|
|
self.data = data # Assign data
|
2021-10-26 19:12:46 +00:00
|
|
|
self.next: Node[T] | None = None # Initialize next as null
|
|
|
|
self.prev: Node[T] | None = None # Initialize prev as null
|
2019-12-26 11:50:12 +00:00
|
|
|
|
|
|
|
|
2021-10-26 19:12:46 +00:00
|
|
|
class Stack(Generic[T]):
|
2019-12-21 01:46:49 +00:00
|
|
|
"""
|
|
|
|
>>> 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->
|
|
|
|
"""
|
2019-12-26 11:50:12 +00:00
|
|
|
|
2021-10-26 19:12:46 +00:00
|
|
|
def __init__(self) -> None:
|
|
|
|
self.head: Node[T] | None = None
|
2019-12-26 11:50:12 +00:00
|
|
|
|
2021-10-26 19:12:46 +00:00
|
|
|
def push(self, data: T) -> None:
|
2019-12-21 01:46:49 +00:00
|
|
|
"""add a Node to the stack"""
|
2019-12-26 11:50:12 +00:00
|
|
|
if self.head is None:
|
|
|
|
self.head = Node(data)
|
|
|
|
else:
|
|
|
|
new_node = Node(data)
|
|
|
|
self.head.prev = new_node
|
|
|
|
new_node.next = self.head
|
2019-12-21 01:46:49 +00:00
|
|
|
new_node.prev = None
|
2019-12-26 11:50:12 +00:00
|
|
|
self.head = new_node
|
|
|
|
|
2021-10-26 19:12:46 +00:00
|
|
|
def pop(self) -> T | None:
|
2019-12-21 01:46:49 +00:00
|
|
|
"""pop the top element off the stack"""
|
2019-12-26 11:50:12 +00:00
|
|
|
if self.head is None:
|
2019-12-21 01:46:49 +00:00
|
|
|
return None
|
2019-12-26 11:50:12 +00:00
|
|
|
else:
|
2021-10-26 19:12:46 +00:00
|
|
|
assert self.head is not None
|
2019-12-26 11:50:12 +00:00
|
|
|
temp = self.head.data
|
2019-12-21 01:46:49 +00:00
|
|
|
self.head = self.head.next
|
2021-10-26 19:12:46 +00:00
|
|
|
if self.head is not None:
|
|
|
|
self.head.prev = None
|
2019-12-26 11:50:12 +00:00
|
|
|
return temp
|
|
|
|
|
2021-10-26 19:12:46 +00:00
|
|
|
def top(self) -> T | None:
|
2019-12-21 01:46:49 +00:00
|
|
|
"""return the top element of the stack"""
|
2021-10-26 19:12:46 +00:00
|
|
|
return self.head.data if self.head is not None else None
|
2019-12-21 01:46:49 +00:00
|
|
|
|
2021-10-26 19:12:46 +00:00
|
|
|
def __len__(self) -> int:
|
2019-12-26 11:50:12 +00:00
|
|
|
temp = self.head
|
2019-12-21 01:46:49 +00:00
|
|
|
count = 0
|
2019-12-26 11:50:12 +00:00
|
|
|
while temp is not None:
|
2019-12-21 01:46:49 +00:00
|
|
|
count += 1
|
|
|
|
temp = temp.next
|
2019-12-26 11:50:12 +00:00
|
|
|
return count
|
2019-12-21 01:46:49 +00:00
|
|
|
|
2021-10-26 19:12:46 +00:00
|
|
|
def is_empty(self) -> bool:
|
2019-12-21 01:46:49 +00:00
|
|
|
return self.head is None
|
|
|
|
|
2021-10-26 19:12:46 +00:00
|
|
|
def print_stack(self) -> None:
|
2019-12-26 11:50:12 +00:00
|
|
|
print("stack elements are:")
|
|
|
|
temp = self.head
|
|
|
|
while temp is not None:
|
|
|
|
print(temp.data, end="->")
|
|
|
|
temp = temp.next
|
|
|
|
|
|
|
|
|
|
|
|
# Code execution starts here
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
|
|
# Start with the empty stack
|
2021-10-26 19:12:46 +00:00
|
|
|
stack: Stack[int] = Stack()
|
2019-12-26 11:50:12 +00:00
|
|
|
|
|
|
|
# Insert 4 at the beginning. So stack becomes 4->None
|
|
|
|
print("Stack operations using Doubly LinkedList")
|
|
|
|
stack.push(4)
|
|
|
|
|
|
|
|
# Insert 5 at the beginning. So stack becomes 4->5->None
|
|
|
|
stack.push(5)
|
|
|
|
|
|
|
|
# Insert 6 at the beginning. So stack becomes 4->5->6->None
|
|
|
|
stack.push(6)
|
|
|
|
|
|
|
|
# Insert 7 at the beginning. So stack becomes 4->5->6->7->None
|
|
|
|
stack.push(7)
|
|
|
|
|
|
|
|
# Print the stack
|
|
|
|
stack.print_stack()
|
|
|
|
|
|
|
|
# Print the top element
|
|
|
|
print("\nTop element is ", stack.top())
|
|
|
|
|
|
|
|
# Print the stack size
|
|
|
|
print("Size of the stack is ", len(stack))
|
|
|
|
|
|
|
|
# pop the top element
|
|
|
|
stack.pop()
|
|
|
|
|
|
|
|
# pop the top element
|
|
|
|
stack.pop()
|
|
|
|
|
2019-12-21 01:46:49 +00:00
|
|
|
# two elements have now been popped off
|
2019-12-26 11:50:12 +00:00
|
|
|
stack.print_stack()
|
|
|
|
|
|
|
|
# Print True if the stack is empty else False
|
|
|
|
print("\nstack is empty:", stack.is_empty())
|