Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

# Python Stack using a Doubly Linked List

Last Updated on December 13, 2022 by Prepbytes

In this blog, our aim is to build a stack using doubly linked list in Python. Python is a vast language which is used for multi purposes i.e. For web scraping, web automation, web development, game development, machine learning and data analysis and lots more. Building a stack using doubly linked list will enhance your skills in data structures like stack, linked list and using them to perform certain tasks. Let’s discuss our topic i.e. “To Build A Stack Using Doubly Linked List In Python”.

## How To Build A Stack Using Doubly Linked List

In this problem, we have to implement a stack, with the help of a doubly linked list.

Input:

Output
For the input 4 3 2 1 our doubly linked list, which will have all functionalities of a stack, will be

Let’s think about how a stack works.
It follows the LIFO(last in first out) principle. This means the element inserted at the last will be accessible to us.

Now, lets recall what a doubly-linked list is. A doubly linked list has two pointers for every node:

• prev, which points to the previous node in the linked list.
• next, which points to the node next to the current node in the linked list.

If we can amend the inserting new node operation in doubly linked list such a way that the current inserted element is always at the head of the linked list and is the first element accessible to us if we want to access an element from linked list, then we can see that our linked list is mimicking the behavior of stack.

Similarly, we will try to think of other operations such as pop(), top(), etc of stack and how we can implement these operations using doubly linked list.

If our input is 4 3 2 1, then our doubly linked list, which will have all functionalities of a stack, will be

Where the head will be pointing to 1 in the linked list.

If input is 5 6 7 8, the the doubly linked list having all the functionalities of the stack will be

Head will be pointing to 8.

Let us have a glance at the algorithm.

## Algorithm To Build A Stack Using Doubly Linked List

There are mainly 6 functions of a stack. We will learn how each function approaches one by one.

push()

In the push() function, we push the element into the stack, and make it the top.

So, to do the same with a doubly-linked list, we will check if the list is Null or not:

• If it is Null, then make the new data the head.
• If the list is not empty, then make the prev of head point to the new node, the next of new node point to the head and the prev of new node point to Null.

In the end, the new node will become the head.

pop()

In the pop() function, we pop the topmost element of the stack and print it.

So, to do the same with a doubly-linked list, first, we will check if the list is Null or not:

• If it is Null, then return None.
• If there is only one node in the list, then remove the head, and return None.
• If both the base cases fail, we will store the head.data . Now, we will increment the head, and make the prev of head point to Null.

In the end, we will return the head.data that we stored.

top()

In the top() function, we have to return the top element of the stack.

So, to do the same with a doubly-linked list, we will simply return the head.data.

size()

In the size() function, we have to return the size of the stack.

So, to do the same with a doubly-linked list, we will simply create a counter and initialize it with 0. Then we will traverse through the list and increment the counter by 1 till we reach the end.

In the end, we will return the counter, as that will be the size of the list.

isEmpty()

In the isEmpty() function, we have to return true if the stack is empty, else false.

So, to do the same with a doubly-linked list:

• If the head is Null, we will return true.
• Else we will return false.

printstack()

In the Printstack() function, we have to print the stack.

So, to do the same with a doubly-linked list, we will simply do a list traversal and print the elements one by one.

## Code Implementation To Build A Stack Using Doubly Linked List

```class Node:

def _init_(self, data):
self.data = data
self.next = None
self.prev = None

class Stack:

def _init_(self):

def push(self, data):

else:
new_node = Node(data)
new_node.prev = None

def pop(self):

return None
return temp
else:
return temp

def top(self):

def size(self):

count = 0
while temp is not None:
count = count + 1
temp = temp.next
return count

def isEmpty(self):

return True
else:
return False

def printstack(self):

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

if _name=='_main_':

stack = Stack()

stack.push(4)
stack.push(3)
stack.push(2)
stack.push(1)
stack.printstack()

print("\nTop element is ", stack.top())

print("Size of the stack is ", stack.size())

stack.pop()
stack.pop()

stack.printstack()

print("\nstack is empty:", stack.isEmpty())
```
``````Output
Stack elements are: 1->2->3->4->
Top element is  1
Size of the stack is 4
Stack elements are: 3->4->
Stack is empty: False``````

Time Complexity To Build A Stack Using Doubly Linked List: O(n), as list traversal is needed.

So, in this article, we have tried to explain the most efficient approach to implement a stack using doubly linked list in python. This is a must-do problem as it requires the basic concepts of the doubly linked list. If you want to solve more questions on Linked List, which are curated by our expert mentors at PrepBytes, you can follow this link Linked List.

## FAQ

1. Can you implement a stack using a doubly linked list?
Stack is a data structure that follows the LIFO technique and can be implemented using arrays or linked list data structures. Doubly linked list has the advantage that it can also traverse the previous node with the help of “previous” pointer.