What is a Stack?

In this lesson, we will be introduced to the Stack data structure, its uses, and functionality.

Introduction #

We are all familiar with the famous Undo option, which exists in almost all popular applications. Have you ever wondered how that works? Well, you store the previous states of your work (which are limited to a specific number), in the memory in such an order that the last one appears first. You cannot really do this very efficiently with simple arrays; we will explore its reasons in the coming chapters. So, this is where the ‘Stack’ data structure comes in handy.

Stacks follow the Last in First Out (LIFO) ordering. This means that the last element added is the element on the top, and the first element added is at the bottom.

A real-life example of Stack could be a stack of books. So, in order to get the book that’s somewhere in the middle, you will have to remove all the books placed at the top of it. Also, the last book you added to the Stack of books is at the top!

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.