Difference between Stack and Queue in programming

Jeremykhoo
2 min readJul 4, 2023

--

There are too many diagrams and over explainations about the differences. The use of too many analogy and visual aids always confused me. So here are the differences as I understand.

Queue: First In First Out.

Stack: First in Last out.

The main difference is who is going “out”.

How are they implemented?

First In:

We need to implement the concept “First in”. So what we need is to remember who came first. Easiest way is is make a list.

There are many ways to make this list in programming, array/tables/linked list/inheritance… But lets not concern ourselves with that. Let’s just say it is a pen-and-paper list

Every time something joins our stack/queue we append the thing on the list. The stuff at the top of the list is are the first things that came, the stuff at the bottom are the last things that came.

Out:

For queue — “first out” everytime we need to out something from our list we cross out the the thing at the top of our list

For stack — “last out”. when we out something we cross out the thing from the bottom of our list.

The explaination I’ve given is abstract. So it is easier to apply it to any context. Remember that we are always appending to a list, only difference is whether we cross out from the top or the bottom of the list.

--

--