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 know more about us, visit https://www.nerdfortech.org/.

Member-only story

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…

--

--

Nerd For Tech
Nerd For Tech

Published in 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 know more about us, visit https://www.nerdfortech.org/.

Akiko Green
Akiko Green

Written by Akiko Green

Software Engineer, Novice Blogger, Indoor Climber and Dog sitting expert 🥰

No responses yet