The ice cream Stack

In this blog post, we will investigate the use of a Stack data structure to store the different flavours of the different scoops of an ice cream!

A stack is a FILO data structure: First In Last Out and seems to be a suitable data structure for our ice cream, where the first scoop of ice-cream being added on the cone, will be the last one to be eaten!

With a Stack data structure you can either:

  • Push new data at the end of the stack.
  • Pop the last value of the stack.


In order to implement our stack will use a 1D array of 5 values, assuming that you cannot make an ice cream with more than 5 scoops! We will use a pointer to store the index of the next empty cell of our array where data can be added.

Using OOP programming, we will create an IceCream class to implement our ice cream as a stack. We will encapsulate both the array data structure and the stack pointer as private properties of the IceCream class and we will use two public methods to push() and pop() scoops of different flavours to/from our IceCream!

Let’s look at the code of our class:

Now that we have an IceCream class, we can instantiate any ice cream object and construct each ice cream by pushing (and popping/eating) different flavours!

Full Python Code

Let’s test this code… You can customise this code to create any ice cream based on your favourite flavours!!!