Image Courtesy: https://unsplash.com/photos/ObpCE_X3j6U

Demonstration of Stack data structure with Python code

Vishvajit Bakarola
3 min readJan 23, 2023

A data structure is a way of organizing and organizing data in a computer’s memory such that it can be easily accessed, modified, and processed. The data can be stored in a variety of formats, such as an array, a list, a tree, a graph, and each data structure has its own advantages and disadvantages. The choice of which data structure to use depends on the specific problem that needs to be solved and the type of operation that needs to be performed on the data. Data structures are fundamental to computer science as they enable efficient algorithms for processing and manipulating data.

A stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle. It is an abstract data type that consists of a collection of elements, with two main operations:

  • push: which adds an element to the top of the stack
  • pop: which removes the element from the top of the stack

The element that is added last is the first one to be removed, which means that the element at the top of the stack is the one that was most recently added.

Stacks are useful in a variety of tasks such as maintaining function call history, undo/redo functionality, parsing expressions, keeping track of function calls and returns, backtracking, and many other applications like Memory management, expression evaluation, and parsing, among others. They can be implemented using arrays, linked lists, or other data structures.

Python is a popular choice for implementing stack data structures because it provides several built-in data structures and libraries that make it easy to implement and work with stacks. Python lists, for example, can be used as a stack by using the append() and pop() methods. The append() method can be used to add elements to the stack, and the pop() method can be used to remove elements from the top of the stack. Python also has a deque class in the collections module that can be used as a stack. It provides a set of methods such as append() and pop() that can be used to add and remove elements from the stack.

Additionally, Python has a built-in list comprehension and generator expressions feature that makes it easy to create and manipulate lists, which can be used as stack. Python also has a rich set of libraries, modules and packages available, that makes it easy to perform complex operations on stack data structure. All these features make Python an excellent choice for implementing stack data structures, as it is simple and easy to use, yet powerful and efficient.

Python code for implementing Stack

stack = []

# Push elements to the stack
stack.append(1)
stack.append(2)
stack.append(3)

# Print the stack
print(stack) # Output: [1, 2, 3]

# Pop an element from the stack
print(stack.pop()) # Output: 3

# Print the stack again
print(stack) # Output: [1, 2]

We can also use the deque class from the collections module to implement a stack in python:

from collections import deque

stack = deque()

# Push elements to the stack
stack.append(1)
stack.append(2)
stack.append(3)

# Print the stack
print(stack) # Output: deque([1, 2, 3])

# Pop an element from the stack
print(stack.pop()) # Output: 3

# Print the stack again
print(stack) # Output: deque([1, 2])

We can also create a class for stack, that will have methods for push, pop and check empty stack.

class Stack:
def __init__(self):
self.stack = []

def push(self, data):
self.stack.append(data)

def pop(self):
if len(self.stack)<=0:
return "Stack is empty"
return self.stack.pop()

def is_empty(self):
return len(self.stack)<=0

stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)

print(stack.pop()) # Output: 3
print(stack.pop()) # Output: 2
print(stack.pop()) # Output: 1

Hope you enjoyed reading.

--

--

No responses yet