Stacks

The stack is somewhat similar with how it works to the queue, the only issue is that there are no longer two given elements that are accessible, instead there is only one. That is the top of the stack, and this means that you can only ever access or add to the top of the stack

A queue follows the FIFO (First-In First-Out) principle, meaning that the first item that was to get put in will be the first to come out. This is unlike a stack, which follows the LIFO (Last-In First-Out) pattern instead, this means that the last item to go into the stack, will be the first one to come out of it.

It is nice to think of it as a stack of plates, if you wanted to take a plate, you would generally take it from the top of the plate. If you wanted to add in a new plate to the stack of them, you would put it onto the top of the stack.

I like to think of it as the container with the tennis balls, the can. If you want to take out a tennis ball, then the one that you have to grab is the top one, and if you want to grab one below that, you first have to take out the balls that come before it in the can. This and many other different forms of items are good for stacks.


Stacks are really good for quick storage and quick access of data, if you have live data that you know you will need to access very quickly, or data that would have a matching counter part or whatever like that. You can think of the reverse polish notation leetcode, or how that was implemented inside of the Oli-Nat programming language, or even how HTML tags can be matched together using a stack.

Pasted image 20261028203908.png

These are the different things you can do with a stack, you can generally push in a value onto the stack, you can then check the top object in the stack, if it has one, you can also then check to see if the stack is empty first before checking, and lastly you can pop the last element out of a stack as well.

Decimal Values to Binary

You can actually use the stack in order to convert a given inter n in a decimal form into a binary representation using a stack. You can have things like this for example:

Pasted image 20261028204215.png

This is an example where N equals 13, and you have an O(n) method of making your decimal value into a binary value. Basically, what you do is you divide by 2, rounding down if the number is ever a decimal, this becomes your new value, and this is sort of how the powers of two end up being simulated, each division of 2 is like another power of two.

You then run the mod 2 operator on this value that you are about to divide, if it is odd, then you toss in a 1 as the binary representation, and then if it is not you toss in a zero. This then actually can be popped in order from the stack and appended to an array as a binary representation.


Book Reading

Please read the Introduction of Section III Data Structures of:

Cormen, T.H., C.E. Leierson, R.L. Rivest and C. Stein Introduction to algorithms. (Cambridge, MA: MIT Press, 2009) 3rd edition.

This is a brief introduction to the general idea of having a dynamic collection of data where the collection has a set of operations one would want from the collection. You can see how the abstract data structures studied so far can exhibit some of the operations of a dynamic set. How these dynamic sets are implemented is using a concrete data structure, which we will discuss soon.

Remember that all the Essential reading for this programme is provided for you. Click the link which will take you to the Online Library where you can access the ebook via E-Book Central (ProQuest).

Alternatively, log into E-Book Central (ProQuest) here to go directly to the ebook.