Day 37 of 100DaysOfCode: Basic Data Structure, Stack
Tags: 100daysofcode, codenewbie, python, algorithms
Published: 2021-01-30 16:41:08

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
```python
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].
```
<blockquote class="twitter-tweet"><p lang="en" dir="ltr">DAY 37 of <a href="https://twitter.com/hashtag/100DaysOfCode?src=hash&amp;ref_src=twsrc%5Etfw">#100DaysOfCode</a>:<br>* Learning basic data structure: Stack<br>* Stack operates in Last In First Out way<a href="https://twitter.com/hashtag/CodeNewbie?src=hash&amp;ref_src=twsrc%5Etfw">#CodeNewbie</a> <a href="https://twitter.com/hashtag/WomenWhoCode?src=hash&amp;ref_src=twsrc%5Etfw">#WomenWhoCode</a> <a href="https://twitter.com/hashtag/womenintech?src=hash&amp;ref_src=twsrc%5Etfw">#womenintech</a> <a href="https://twitter.com/hashtag/pythonlearning?src=hash&amp;ref_src=twsrc%5Etfw">#pythonlearning</a> <a href="https://twitter.com/hashtag/DEVCommunity?src=hash&amp;ref_src=twsrc%5Etfw">#DEVCommunity</a> <a href="https://t.co/njuuh87acZ">pic.twitter.com/njuuh87acZ</a></p>&mdash; Durga Pokharel (@mathdurga) <a href="https://twitter.com/mathdurga/status/1355554458328068099?ref_src=twsrc%5Etfw">January 30, 2021</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>