What is a Stack?

An introduction 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. 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 can’t really do this with simple lists very efficiently for reasons we will explore 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.