Python Data Structure — Stack

Ashu
Ashu
Apr 14 · 2 min read

A stack is a useful data structure in programming. It is just like a pile of books kept on top of each other.

Image: codingtutorial4u

Think about the things you can do with such a pile of plates

  • Put a new plate on top
  • Remove the top plate

If you want the plate at the bottom, you must first remove all the plates on top. Such an arrangement is called Last In First Out — the last item that is the first item to go out.

Image: Programiz/dsa

Basic Operations of Stack

A stack is an object (an abstract data type — ADT) that allows the following operations:

  • Push: Add an element to the top of a stack
  • Pop: Remove an element from the top of a stack
  • IsEmpty: Check if the stack is empty
  • GetStack: Returns the entire stack
  • Peek: Get the value of the top element without removing it

Stack Implementations in Python

Image: Atom/AshwinPC

Stack Time Complexity

For the array-based implementation of a stack, the push and pop operations take constant time, i.e. O(1)

Applications of Stack Data Structure

Although stack is a simple data structure to implement, it is very powerful. The most common uses of a stack are:

  • To reverse a word — Put all the letters in a stack and pop them out. Because of the LIFO order of stack, you will get the letters in reverse order.
  • In compilers — Compilers use the stack to calculate the value of expressions like 2 + 4 / 5 * (7 - 9) by converting the expression to prefix or postfix form.
  • In browsers — The back button in a browser saves all the URLs you have visited previously in a stack. Each time you visit a new page, it is added on top of the stack. When you press the back button, the current URL is removed from the stack, and the previous URL is accessed.

Nerd For Tech

From Confusion to Clarification

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To stay up to date on other topics, follow us on LinkedIn. https://www.linkedin.com/company/nerdfortech

Ashu

Written by

Ashu

Tech Content Creator

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To stay up to date on other topics, follow us on LinkedIn. https://www.linkedin.com/company/nerdfortech

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store