a complete working python program to demonstrate all stack operations using a doubly linked list stack stack stack isempty true stack printstack stack elements are for i in range4 stack pushi stack isempty false stack printstack stack elements are 3210 stack top 3 lenstack 4 stack pop 3 stack printstack stack elements are 210 add a node to the stack if self head is none self head nodedata else newnode nodedata self head prev newnode newnode next self head newnode prev none self head newnode def popself t none return the top element of the stack return self head data if self head is not none else none def lenself int temp self head count 0 while temp is not none count 1 temp temp next return count def isemptyself bool return self head is none def printstackself none printstack elements are temp self head while temp is not none printtemp data end temp temp next code execution starts here if name main start with the empty stack stack stackint stack insert 4 at the beginning so stack becomes 4none printstack operations using doubly linkedlist stack push4 insert 5 at the beginning so stack becomes 45none stack push5 insert 6 at the beginning so stack becomes 456none stack push6 insert 7 at the beginning so stack becomes 4567none stack push7 print the stack stack printstack print the top element printntop element is stack top print the stack size printsize of the stack is lenstack pop the top element stack pop pop the top element stack pop two elements have now been popped off stack printstack print true if the stack is empty else false printnstack is empty stack isempty a complete working python program to demonstrate all stack operations using a doubly linked list assign data initialize next as null initialize prev as null 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 add a node to the stack pop the top element off the stack return the top element of the stack code execution starts here start with the empty stack insert 4 at the beginning so stack becomes 4 none insert 5 at the beginning so stack becomes 4 5 none insert 6 at the beginning so stack becomes 4 5 6 none insert 7 at the beginning so stack becomes 4 5 6 7 none print the stack print the top element print the stack size pop the top element pop the top element two elements have now been popped off print true if the stack is empty else false
from __future__ import annotations

from typing import Generic, TypeVar

T = TypeVar("T")


class Node(Generic[T]):
    def __init__(self, data: T):
        self.data = data  
        self.next: Node[T] | None = None  
        self.prev: Node[T] | None = None  


class Stack(Generic[T]):
    

    def __init__(self) -> None:
        self.head: Node[T] | None = None

    def push(self, data: T) -> None:
        
        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) -> T | None:
        
        if self.head is None:
            return None
        else:
            assert self.head is not None
            temp = self.head.data
            self.head = self.head.next
            if self.head is not None:
                self.head.prev = None
            return temp

    def top(self) -> T | None:
        
        return self.head.data if self.head is not None else None

    def __len__(self) -> int:
        temp = self.head
        count = 0
        while temp is not None:
            count += 1
            temp = temp.next
        return count

    def is_empty(self) -> bool:
        return self.head is None

    def print_stack(self) -> None:
        print("stack elements are:")
        temp = self.head
        while temp is not None:
            print(temp.data, end="->")
            temp = temp.next



if __name__ == "__main__":
    
    stack: Stack[int] = 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())