The LIFO Stack - A Gentle Guide
Written by Harry Fairhead   
Thursday, 21 March 2019
Article Index
The LIFO Stack - A Gentle Guide
Last In First Out
The Function Stack
RPN

The stack is a very simple idea. It is a data structure that has only two simple operations and yet not only is it powerful, it is at the heart of modern computing, both theory and practice. Let's find out more about it.

What Programmers Know

knowcover

Contents

  1. The Computer - What's The Big Idea?*
  2. The Memory Principle - Computer Memory and Pigeonholes*
  3. Principles of Execution - The CPU
  4. The Essence Of Programming
  5. Variables - Scope, Lifetime And More*
  6. Binary Arithmetic
  7. Hexadecimal*
  8. Binary - Negative Numbers*
  9. Floating Point Numbers*
  10. Inside the Computer - Addressing
  11. The Mod Function
  12. Recursion
  13. The Lost Art Of The Storage Mapping Function *
  14. Hashing - The Greatest Idea In Programming
  15. Advanced Hashing
  16. XOR - The Magic Swap*
  17. Programmer's Introduction to XML
  18. From Data To Objects*
  19. What Exactly Is A First Class Function - And Why You Should Care*
  20. Stacks And Trees*
  21. The LIFO Stack - A Gentle Guide*
  22. Data Structures - Trees
  23. Inside Random Numbers
  24. The Monte Carlo Method
  25. Cache Memory And The Caching Principle
  26. Data Compression The Dictionary Way
  27. Dates Are Difficult*
  28. Sequential Storage*
  29. Magic of Merging*
  30. Power of Operators
  31. The Heart Of A Compiler*
  32. The Fundamentals of Pointers
  33. Functional And Dysfunctional Programming*

* Recently revised

There are few more wonderfully simple yet powerful ideas in computing than the stack.

I don't know when the stack was invented but it could be an idea that predates the computer. There were and are lots of mechanical examples of a stack that make it a relatively obvious concept. However it is one thing just to notice that there are such things in nature and quite another to put them to good use. When ever the stack was first invented it certainly didn't reach its true potential until the computer came along.

So let’s take a close look at the stack - where it originates, its basic operation and what it can do for us.

Banner

Push and Pop

A stack has two fundamental operations - Push and Pop.

The Push operation stores something on the top of the stack and the Pop operation retrieves something from the top of the stack.

If you want a physical representation of a stack all you have to think of is a self-service café – complete with a spring-loaded plate delivery system. The clean plates are pushed on top of the stack of plates against a spring that keeps the top plate at the same level.

plates

Push a plate

 

When a customer comes along and takes a plate another one pops to the top to replace it.

 

platespop.

Pop a plate

 

You can see a similar mechanism in action in one of those coin storage devices where you push a coin in at the top and take one out from the same slot.

These mechanical devices share that same basic principle of operation of a stack. They may not have the same internal workings but things are added to the stack at the top and taken from the top – the push and pop operations are the same.

And this is the key idea about stack storage - you can only access and work with the top of the stack.

The Hardware Stack

The plate stacker is a really good way to imagine how a stack operates and what you can do with it but in software it looks a little different - no plates for example.

We have already encountered the idea of a  stack while looking at the fundamental design of the CPU in another article.

The “stack pointer” is a hardware register that holds the address of a memory location.

As you would expect there are only two basic operations on a stack pointer, push and pop.

The push operation stores the contents of another register on the stack and then moves the stack pointer on by one. The pop, sometimes called “pull”, operation just reverses the push operation – it moves the pointer back by one and then retrieves the value from the stack.

This is all there is to a stack, it is entirely simple and yet it has hidden depths.

There are lots of variations on how a stack can be implemented.

Some stacks are made so that they start at a low address and grow upward. Some start high and grow down. In other words some add to the stack pointer on a pop and some subtract.

Others change the stack pointer before storing a value; others change it after storing a value. The difference being that in the former case the stack pointer always points to the current item on the top of the stack and in the latter it points to the next free stack location.

These differences are minor and don't alter the basic operation of the stack. but they can be confusing.

 

Banner



Last Updated ( Thursday, 21 March 2019 )