1- # A complete working Python program to demonstrate all
2- # stack operations using a doubly linked list
3-
1+ # Complete Python program demonstrating stack operations using a doubly linked list
42from __future__ import annotations
53
6- from typing import Generic , TypeVar
7-
8- T = TypeVar ("T" )
9-
10-
11- class Node (Generic [T ]):
4+ class Node [T ]:
5+ """Node class for doubly linked list"""
126 def __init__ (self , data : T ):
13- self .data = data # Assign data
14- self .next : Node [T ] | None = None # Initialize next as null
15- self .prev : Node [T ] | None = None # Initialize prev as null
7+ self .data = data # Node data
8+ self .next : Node [T ] | None = None # Reference to next node
9+ self .prev : Node [T ] | None = None # Reference to previous node
1610
17-
18- class Stack (Generic [T ]):
11+ class Stack [T ]:
1912 """
13+ Stack implementation using doubly linked list
14+
2015 >>> stack = Stack()
2116 >>> stack.is_empty()
2217 True
@@ -42,89 +37,75 @@ class Stack(Generic[T]):
4237 """
4338
4439 def __init__ (self ) -> None :
45- self .head : Node [T ] | None = None
40+ self .head : Node [T ] | None = None # Top of stack
4641
4742 def push (self , data : T ) -> None :
48- """add a Node to the stack"""
43+ """Push element onto stack"""
4944 if self .head is None :
5045 self .head = Node (data )
5146 else :
5247 new_node = Node (data )
48+ # Insert new node at head
5349 self .head .prev = new_node
5450 new_node .next = self .head
55- new_node .prev = None
5651 self .head = new_node
5752
5853 def pop (self ) -> T | None :
59- """pop the top element off the stack"""
54+ """Pop element from top of stack"""
6055 if self .head is None :
6156 return None
62- else :
63- assert self . head is not None
64- temp = self .head .data
65- self .head = self .head .next
66- if self .head is not None :
67- self .head .prev = None
68- return temp
57+
58+ # Remove and return head node data
59+ temp = self .head .data
60+ self .head = self .head .next
61+ if self .head is not None :
62+ self .head .prev = None # Clear prev reference for new head
63+ return temp
6964
7065 def top (self ) -> T | None :
71- """return the top element of the stack """
66+ """Peek at top element without removing """
7267 return self .head .data if self .head is not None else None
7368
7469 def __len__ (self ) -> int :
75- temp = self . head
70+ """Return number of elements in stack"""
7671 count = 0
77- while temp is not None :
72+ current = self .head
73+ while current :
7874 count += 1
79- temp = temp .next
75+ current = current .next
8076 return count
8177
8278 def is_empty (self ) -> bool :
79+ """Check if stack is empty"""
8380 return self .head is None
8481
8582 def print_stack (self ) -> None :
83+ """Print all stack elements"""
8684 print ("stack elements are:" )
87- temp = self .head
88- while temp is not None :
89- print (temp .data , end = "->" )
90- temp = temp .next
91-
85+ current = self .head
86+ while current :
87+ print (current .data , end = "->" )
88+ current = current .next
9289
93- # Code execution starts here
90+ # Program entry point
9491if __name__ == "__main__" :
95- # Start with the empty stack
96- stack : Stack [int ] = Stack ()
92+ stack : Stack [int ] = Stack () # Create integer stack
9793
98- # Insert 4 at the beginning. So stack becomes 4->None
9994 print ("Stack operations using Doubly LinkedList" )
95+ # Push elements onto stack
10096 stack .push (4 )
101-
102- # Insert 5 at the beginning. So stack becomes 4->5->None
10397 stack .push (5 )
104-
105- # Insert 6 at the beginning. So stack becomes 4->5->6->None
10698 stack .push (6 )
107-
108- # Insert 7 at the beginning. So stack becomes 4->5->6->7->None
10999 stack .push (7 )
110100
111- # Print the stack
112- stack .print_stack ()
101+ stack .print_stack () # Print current stack
113102
114- # Print the top element
115- print ("\n Top element is " , stack . top ())
103+ print ( " \n Top element is" , stack . top ()) # Show top element
104+ print ("Size of stack is " , len ( stack )) # Show size
116105
117- # Print the stack size
118- print ("Size of the stack is " , len (stack ))
119-
120- # pop the top element
106+ # Pop two elements
121107 stack .pop ()
122-
123- # pop the top element
124108 stack .pop ()
125109
126- # two elements have now been popped off
127- stack .print_stack ()
128-
129- # Print True if the stack is empty else False
130- print ("\n stack is empty:" , stack .is_empty ())
110+ stack .print_stack () # Print modified stack
111+ print ("\n Stack is empty:" , stack .is_empty ()) # Check emptiness
0 commit comments