Quick Index
What and Why


The concept of a stack is simple.

It behaves like a stack of books or pancakes.

The last book in (top) is the first out.

This is called LIFO (last in first out).
📚
🥞
It's a mechanically simple structure isn't it.

The essence is adding and removing from one end -- which we call the top of the stack. Note that "top" has no physical meaning -- it is just a term used by convention.
A stack is used to perform some hefty computer tasks.

Here are a few examples:

  • Language Processing (Compilers)
  • Expression Stacks
  • Function Calls
  • Infix to Postfix
  • Tree Searches



Fundamental Operations


A stack has two fundamental operations which are "push" and "pop".

The method "push" pushes (adds) an element onto the top of the stack.

And we say the element is pushed to the top of the stack.
The method "pop" removes (and returns) the element from the top of the stack.


LIFO


LIFO - Last in first out

Example
Here we push in "70".

Then we do a pop, which removes the "70".

We call this "LIFO" which means last in first out.

The last in was the "70" and it is also the first out.


Traversal (Iteration)


The following shows the default traversal order. The order follows what would happen on successive pops.

Note that if we traverse (iterate) through the structure we are generally not removing elements.

Any of these terms might be used: traverse, iterate, navigate, loop.

Example
The default traversal for a stack is from top to bottom.

We say we visit (in this order):
70, 60, 50, 40, 30, 20, 10


Top and Bottom Explained


"Top" and "Bottom" are terms used by convention but have no special meaning.

"Under the hood" you could have any (actual) linear structure. The only thing that matters is that when you push 40, 50, 60, 70 (in this order). Then on the next four pop operations your stack should pop 70, 60, 50, 40 (in this order).

"Under the hood" you could also choose either end of your linear structure for adds and removes -- but it must be only one end, and must not change.


ADT


See the ADT here...

References




Navigation