Sitemap
Alok Dubey

Alok Dubey

A fitness freak, an avid reader, loves to play data science, badminton, volleyball, cycling, walking and movies.

Stack

3 min readOct 20, 2024

--

Stack is one of the data structure. This datastructure can be implemented using either arrays or the Linked List. If you would like to know how to implement the arrays or Linkedlist using python, then my previous posts will help you in doing so. The code for implementing the Stack using array and linkedlist can be found here.

Stack utilizes the Last In First Out (LIFO) method. Imagine, you are keeping your clothes in a basket. If you would like to wear a dress, then you will take out the upper most pair (top). We can call this as a stack of clothes. Have you observed that if you open any mail box, it will show the latest emails in the top slot. As we are interested in latest emails only. Occasionally, we will be scrolling down to see the older emails. This is one of the stack implementation.

Stack is closed at one end i.e.., at the bottom. You can add the elements from the top only. Each element refers to the item below it. Stack contains a pointer referring to the top most element referred as Top.

Stack representation

Operations performed on stacks :

Push :

Whenever a new item is inserted into the stack, this operation is known as Push. We always push the operations from top only.

Pop :

Removing an item from top is known as pop. This method returns the value of the element that is being popped. This is similar to the pop method in linked list or array. However, the only difference between the Linked list and array is that we can pop the elements from the middle as well. However, this is not allowed in stack.

Peek :

Peek method returns the value present in the top most element.

IsEmpty :

IsEmpty method is used to check whether the stack has no elements. If the stack does not contains no elements, then this method will return True otherwise it will return False.

Size :

This method returns the number of elements present in the stack. This method will return zero, if the stack is empty.

Get Alok Dubey’s stories in your inbox

Join Medium for free to get updates from this writer.

Please share your feedback or comments on this post in the comments section.

  1. Introduction to Algorithms :. Algorithms plays an important role in… | by Alok Dubey | Oct, 2024 | Medium

2. Best, Worst & Avg case analysis in Linear search and Binary Search algorithms | by Alok Dubey | Oct, 2024 | Medium

3. Asymptotic notations. Asymptotic notations are mathematical… | by Alok Dubey | Oct, 2024 | Medium

4. Introduction to Arrays. Arrays are one of the most commonly… | by Alok Dubey | Oct, 2024 | Medium

5. Implementing an Array and its properties using Python | by Alok Dubey | Oct, 2024 | Medium

6. Introduction to Linked List. Linked list is a linear datastructure… | by Alok Dubey | Oct, 2024 | Medium

7. Implementation of LinkedList using python | by Alok Dubey | Oct, 2024 | Medium

--

--

Alok Dubey
Alok Dubey

Written by Alok Dubey

A fitness freak, an avid reader, loves to play data science, badminton, volleyball, cycling, walking and movies.