Durga Pokharel

Posted on

# Day 37 of 100DaysOfCode: Basic Data Structure, Stack

This is my 37th day of 100 Days of Code and as yesterday, I tried to revise my older exercise but Power Cut caused me lot of problem. At the end of the day, I ended up learning about Stack. As non tech person, it was hard to understand at first but Stack is like a pack of biscuits and only way to take pieces out is in order of Last In First Out. If we want to insert a data in Stack, then it has to be added on the top of the current pointer of stack and in order to take data, it has to be the top most item.

## Few concepts I learned

• Stack operates in LIFO(Last In First Out).
• Stack has certain size.
• Stack has some operations like:
• Push: insert data in top of the stack.
• Pop: remove data from top of stack.
• StackOverlow: When stack is full i.e all of its empty spaces are filled or number of data in stack equal to size of stack and the push operation is done.
• StackUnderflow: When stack is empty and we still try to pop data from the stack.

## Python Code

``````class Stack:
def __init__(self, size=5):
self.size = size
self.stack = [None] * size
self.pointer = -1
print(f"Stack Initialized with size: {size}.")

def push(self, data):
self.pointer+=1
if self.pointer >= self.size:
self.pointer-=1
print(f"Overflow occured. Hence {data} couldn't be inserted.")
else:
self.stack[self.pointer] = data
print(f"{data} pushed to {self.pointer} position of stack. New Stack is {self.stack}.")

def pop(self):
if self.pointer < 0:
self.pointer = -1
print("Underflow occured. Nothing to pop.")
else:
p = self.stack[self.pointer]
self.stack[self.pointer] = None
print(f"{p} popped from {self.pointer} position of stack. New Stack is {self.stack}.")
self.pointer -= 1

stack = Stack(3)
stack.push(2)
stack.push(1)
stack.push(0)
stack.pop()
stack.pop()
stack.push(5)
stack.push(6)
stack.push(3)
stack.pop()
stack.pop()
stack.pop()
stack.pop()
stack.push(11)
``````

I was stuck for hours to figure out how to do this simple operation and there still might some errors too but the output of above code was clear and given below.

``````Stack Initialized with size: 3.
2 pushed to 0 position of stack. New Stack is [2, None, None].
1 pushed to 1 position of stack. New Stack is [2, 1, None].
0 pushed to 2 position of stack. New Stack is [2, 1, 0].
0 popped from 2 position of stack. New Stack is [2, 1, None].
1 popped from 1 position of stack. New Stack is [2, None, None].
5 pushed to 1 position of stack. New Stack is [2, 5, None].
6 pushed to 2 position of stack. New Stack is [2, 5, 6].
Overflow occured. Hence 3 couldn't be inserted.
6 popped from 2 position of stack. New Stack is [2, 5, None].
5 popped from 1 position of stack. New Stack is [2, None, None].
2 popped from 0 position of stack. New Stack is [None, None, None].
Underflow occured. Nothing to pop.
11 pushed to 0 position of stack. New Stack is [11, None, None].
``````

DAY 37 of #100DaysOfCode:
* Learning basic data structure: Stack
* Stack operates in Last In First Out way#CodeNewbie #WomenWhoCode #womenintech #pythonlearning #DEVCommunity pic.twitter.com/njuuh87acZ

— Durga Pokharel (@mathdurga) January 30, 2021