Understanding Data Structures: Stack

Β·

2 min read

A stack is a linear data structure which stores nodes in a sequential collection. A stack is modified by its two operations Pop and Push which are subject to the LIFO(Last In, First Out) principle.

The Last in, First out principle can be illustrated with a stack of plates; where the most recent plate added to the stack is the first to go out.

Set of 8 Logan Stacking Dinner Plates | Crate and Barrel

The most recent node added to a stack is the first one to go out.

Uses Of Stacks

  • They are used in creating efficient Algorithms: Many efficient algorithms such as backtracking algorithms and depth-first algorithms need the stack data structure for them to easily solve problems.

  • Function Calls And Recursion: During Function calls and recursive operations, the current state of the program is pushed into the stack, and when the current function called returns it is popped(removed) from the stack. The stack returns to the previous operation it was handling before the function call.

  • Expression Evaluation: In the arithmetic evaluation of expressions, stacks are used to convert infix notational expression to postfix notational or prefix notational expressions.

Implementation

In this is implementation we would be using Python, but you can use this concept for implementation in other programming languages. The implementation of stacks is very easy 😢

  • Firstly we create a class to handle the Stack

    At instantiation we want the stack to create an empty array which would hold all the nodes in the stack later.

      class Stack:
          def __init__(self):
              self.stack=[]
    
  • Secondly, we add the first modification method: Push

    This method adds nodes to the stack collection. We can rather say that it stacks nodes on the stackπŸ˜‰

    This operation is carried out in real life when we stack plates after washing them πŸ˜‚

      def push(self,value):
              self.stack.append(value)
    
  • We create the next modification method PULL:

    This method removes the most recent node added to the stack. Just like how we collect plates from its stack when we want to use them; meal time!😁

    The Pull method destacks the stack.

          def pull(self):
              self.stack.pop()
              self.length-=1
    

    The Final Result:

  •     class Stack:
    
            def __init__(self):
                self.stack=[]
                self.length=0
    
            def push(self,value):
                self.stack.append(value)
                self.length+=1
    
            def pull(self):
                self.stack.pop()
                self.length-=1
    

Let's test it out.

test= Stack()
test.push(34)
test.push(45)
test.push(23)

print(test.stack) #Returns [34,45,23]
test.pull()
print(test.stack)#Returns [34,45]

You can create your own PRINT method for the Stack,
 take this as an excercise πŸ’‘

You can watch the live implementation of the stack:

Thanks for reading 😁

Β