What are Stacks in Programming?

Akiko Green
Nerd For Tech
Published in
8 min readFeb 18, 2023

--

images of a stack of pancakes

The best way to think about how to implement a Stack in programming is to visualize a stack of pancakes!

A stack is a linear data structure used for storing data. Visually speaking, a stack is Last In, First Out. Meaning, the last pancake on the plate will be stacked on top. Which will also be the first pancake to be eaten in the stack.

Now, before I get into explaining how to implement a Stack data structure. I would like to mention that this is for those who just started learning about data structures :).

How To Implement A Stack

There are two ways to implement a stack. We can either use an Array or a Linked List. Both are great to use but let’s see the pros & cons of using one or the other.

Array Advantages & Disadvantages

The advantages of implementing a stack with an array are:

  • Fast Access Time: Arrays provide constant time access to elements, O(1), which makes accessing the top element of the stack fast and efficient.
  • Cache locality: Since array elements are stored contiguously in memory, there is a high probability that elements that are close to each other in the array will also be close to each other in memory. This improves cache locality, which can lead to better…

--

--

Akiko Green
Nerd For Tech

Software Engineer, Novice Blogger, Indoor Climbing Cutie 🥰