DSA with Python. Day-2
So continuing from Day-1 of this series we have already covered Arrays/Lists in Python and now what all data structures which we can implement using arrays firstly we will start Implementing them from scratch.
Implementing Stack?
Stack Data Structure is a data structure that follows Last In First Out (LIFO) Principle so the last element inserted is the first to be popped out. Below, we will cover all the basics of Stack, Operations on Stack, its implementation, advantages and disadvantages using Python.

As you can see in the diagram above Both Insertion or removal of the element can be done from the top-end of the stack only which is Open for Pushing or Popping the elements.
Types of Stacks?
Fixed Size Stack: A fixed size stack as the name says is a stack with fixed size. If the stack is empty and an attempt is made to remove an element from it, an Underflow error occurs. If the stack is full and an attempt is made to add an element to it, an Overflow error occurs. It can not grow its size dynamically or reduce it size on its own.
Dynamic Size Stack: A dynamic size stack can grow or shrink dynamically. When the stack is full, it automatically increases its size to accommodate the new element and when the stack is empty it decreases its size.
How Stacks actually Work?
So as already discussed a stack is a data structure that follows the Last In First Out (LIFO) principle. This means that the last element added to the stack will be the first one to be removed.
Here’s a breakdown of how it works:
Push Operation: This adds an element to the top of the stack. For example, if you push the elements 1, 2, and 3 onto the stack (in that order), the stack will look like this: [1, 2, 3].
Pop Operation: This removes the top element from the stack. Using the previous example, if you pop an element, the stack will now look like this: [1, 2], and the element 3 will be returned.
Peek Operation: This allows you to look at the top element of the stack without removing it. So, if you peek at the stack [1, 2], it will return 2, but the stack remains unchanged.
Is Empty Operation: This checks whether the stack is empty. If the stack is [1, 2], it will return `False`. If the stack is [], it will return `True`.
Size Operation: This returns the number of elements in the stack. For the stack [1, 2], it will return 2.
Here’s a visual representation & results of the operations:
-> Initial Stack: []
-> Push 1:
-> Push 2: [1, 2]
-> Push 3: [1, 2, 3]
-> Pop: [1, 2] (returns 3)
-> Peek: [1, 2] (returns 2)
-> Is Empty: [1, 2] (returns False)
-> Size: [1, 2] (returns 2)
Implementation of Stack:
class Stack:
def __init__(self):
self.items = [] #Initialization of Empty Stack
def is_empty(self):
return len(self.items) == 0 #Is_empty() func to check if length is 0
def push(self, item):
self.items.append(item) #Push() func to append at last index
def pop(self):
if not self.is_empty(): #Pop() func to remove from last index
return self.items.pop() #provided if stack is not empty.
else:
return None
def peek(self):
if not self.is_empty(): #Peek() func to check for last index
return self.items[-1] #element but not removing it.
else:
return None
def size(self): #Checking the size of the Stack.
return len(self.items)
# Example usage:
stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
print(stack.pop()) # Output: 3
print(stack.peek()) # Output: 2
print(stack.size()) # Output: 2
Applications of Stack:
Stacks are used in various applications such as:
-> Undo mechanisms in text editors.
-> Backtracking algorithms (e.g., navigating mazes).
-> Expression evaluation and syntax parsing.
So I hope something you must have learnt from this blog. Next in my blog Queues are actually in the QUEUEEEEEE!!!!!.
Please do comment for any things to add-on and till then Happy Learning!!