Python | Stack using Doubly Linked List

A stack is a collection of objects that are inserted and removed using Last in First out Principle(LIFO). User can insert elements into the stack, and can only access or remove the recently inserted object on top of the stack. The main advantage of using LinkedList over array for implementing stack is the dynamic allocation of data whereas in the array, the size of the stack is restricted and there is a chance of stack overflow error when the size of the stack is exceeded the maximum size.

Stack Operations:

1. push() : Insert the element into Stack and assign the top pointer to the element.
2. pop() : Return top element from the Stack and move the top pointer to the
second element of the Stack.
3. top() : Return the top element.
4. size() : Return the Size of the Stack.
5. isEmpty() : Return True if Stack is Empty else return False.
6. printstack() : Print all elements of the stack.

Below is the implementation of the above-mentioned stack operations using Doubly LinkedList in Python:





# A complete working Python program to demonstrate all 
# stack operations using a doubly linked list 
# Node class 
class Node:
# Function to initialise the node object
    def __init__(self, data): = data # Assign data = None # Initialize next as null
        self.prev = None # Initialize prev as null        
# Stack class contains a Node object
class Stack:
    # Function to initialize head 
    def __init__(self):
        self.head = None
# Function to add an element data in the stack 
    def push(self, data):
        if self.head is None:
            self.head = Node(data)
            new_node = Node(data)
            self.head.prev = new_node
   = self.head
            new_node.prev = None
            self.head = new_node
# Function to pop top element and return the element from the stack 
    def pop(self):
        if self.head is None:
            return None
        elif is None:
            temp =
            self.head = None
            return temp
            temp =
            self.head =
            self.head.prev = None
            return temp
# Function to return top element in the stack 
    def top(self):
# Function to return the size of the stack 
    def size(self):
        temp = self.head
        count = 0
        while temp is not None:
            count = count + 1
            temp =
        return count
# Function to check if the stack is empty or not  
    def isEmpty(self):
        if self.head is None:
           return True
           return False
# Function to print the stack
    def printstack(self):
        print("stack elements are:")
        temp = self.head
        while temp is not None:
            print(, end ="->")
            temp =           
# Code execution starts here         
if __name__=='__main__'
# Start with the empty stack
  stack = Stack()
# Insert 4 at the beginning. So stack becomes 4->None 
  print("Stack operations using Doubly LinkedList")
# 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("\nTop element is ",
# Print the stack size
  print("Size of the stack is ", stack.size())
# pop the top element
# pop the top element
# two elements are popped
# Print the stack
# Print True if the stack is empty else False
  print("\nstack is empty:", stack.isEmpty())
#This code is added by Suparna Raut



Stack operations using Doubly LinkedList
stack elements are:
Top element is  7
Size of the stack is  4
stack elements are:
stack is empty: False

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : SuparnaRaut

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.