DEV Community

Durga Pokharel
Durga Pokharel

Posted on

1

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)
Enter fullscreen mode Exit fullscreen mode

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].
Enter fullscreen mode Exit fullscreen mode

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

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay