What is a Stack?

An introduction to the stack data structure, its uses, and its functionality.

Introduction

Everyone is familiar with the famous undo option, which exists in almost all popular applications. But have you ever wondered how that works? Well, 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 efficiently do this with simple arrays for reasons explored in the coming chapters. 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. To get the book that’s somewhere in the middle, you will 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!

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy