CS AB Stack Part 1
CS AB Stack Part 1
We all know what a ‘stack’ is. Imagine a short pile of shoe boxes, one on top of another.
You could put things – shoes for example – in the boxes and pile them up, but not too
high. Of course, order matters because if the shoes you’ll want to wear next are not in the
top box, or at least one of the upper boxes, you’ll have to move all the boxes stacked on
top of the one you want before you can put your shoes on.
Pause here and make sure you understand the problem I've attempted to describe. Despite
this limitation, stacks are very useful – and fast.
A LIFO structure is exactly the opposite of a well-behaved line at the cafeteria – a first-
come, first-served structure, otherwise known as FIFO (first-in,, first – out).
Typically stacks have a fixed size. If you try to push a box onto the top of the stack and
the stack is already full, the bottom most box disappears. It just vanishes to make room
for the new box. If it did not do this, you’d have a ‘stack crash’ – imagine the stack of
shoe boxes collapsing under its own weight, shoes flying everywhere. This is stack
overflow.
You could pull boxes, one after another (you can only push or pull one box at a time, and
only from the top of the stack), but eventually you would take off the last box. If you then
tried pointlessly to pull one more box from the empty stack, you’d have a runtime error.
In the case of Java, that probably means (yes, you guessed it) an exception! We will see if
we can do a little better, but popping an empty stack is a definite no-no called stack
underflow.
This stack has five We push two pairs of We push three more
registers. At present the shoes pairs – the stack is now
stack is empty. full
sneakers Moccasins
Westcos Riding boots
Flip-Flops
sneakers
Westcos
This is perhaps the code to get the stack in the middle column
Now the stack is full (right hand column). The Westcos are at the bottom, the moccasins
(Last In, First Out) are on the top.
Note the last-out, first-in approach. Forgetting about the Flip-Flops on our feet, we Push
the Boots in first because (prior to the Flip-Flops) they were the last one’s out.
So let’s say that on the way back from the beach we found these really chic dress shoes
on sale. Since we’ll want to show these off when we next go out, we’ll put them at the top
of the stack (that is, push them last).
Flip-Flops Chic
Moccasins Flip-Flops
Riding boots Moccasins
sneakers Riding boots
Westcos sneakers
Oh no! We just lost our Westcos, a lovely pair, nicely broken-in and rather pricey too.
This is not so good. That’s what happens if you overfill your stack – you just lose the
contents of the bottom register. Unless the stack programmer was in a really vicious
mood, in which case your stack just overflowed and a crash is the most likely outcome.
Notice that if you look at the stack state just after we put away the Flip-Flops (and before
adding the new chic pair) we have effectively re-ordered the stack from where we had it
at the beginning. To move the Flip-Flops from the middle to the top, you have to pull
three pairs, put two back, and finally put back the entry you want on top.
Stacks are not intended to handle a lot of re-ordering. This is quite different from more
random-access containers (like lists, for example).
EXER: Sketch some code that will let you keep those Westcos before you push the
dress shoes in the shoe closet. This will take a little work, but believe me, for those boots
it’s worth it.
Please e-mail this EXER ([email protected]). Place in the Subject line
CS .AB Stack1 f i r s t l as t
// where first is your first name and last is your last name
Review
A stack is a LIFO (last – in, first – out) data structure. Usually a stack has some fixed
size, like basic Java arrays. The topmost register is the top of the stack; the last register is
the bottom.
TOP
Register 1
Register 2
Register 3
Register 4
BOTTOM
What are the basic operations on a stack?
Good programmers always want their variables initialized, so when ever a register is
cleared (for example because of a Pop()) they would assign values to any cells not having
one.
Q: If the stack contains JFrames, what value would you assign to empty cell (register) in
the stack?