Outline of Class 37: Stacks and Queues
Held: Wednesday, April 8, 1998
- One problem with lists is that they provide just too many options.
You can put at and retrieve elements from the front, back, or even
middle of the list.
- This can make implementation more difficult, as we've already seen.
- This can also make usage of lists more difficult, as a user needs
to consider the interrelationship of the different functions.
- Many uses of lists can be simplified to four basic operations:
- Add an element to the list (or related structure).
- Remove an element from the list (or related structure).
- Peek at the next element to be removed.
- Check if there are any elements remaning.
- Structures that support these three operations are often called
- A policy determines the relationship between objects being
added and objects being removed.
- A first-in, first-out (FIFO) policy says that objects are removed in
the order that they're put in.
- A last-in, first-out (LIFO) policy says that objects are removed in
the opposite order that they're put in (so the object most recently
added is the next one to be removed).
- Other policies might place priorities on the objects, or randomly
- Stacks are linear structures that use the LIFO policy.
- They are similar to the stacks of dishes that you may see in cafeterias.
As each dish is cleaned, it is added to the top of the stack.
As customers need dishes, they remove them from the top of the stack.
- The standard names for stack operations are
push(Object o) -- add an element to the top of the stack
Object pop() -- remove an element from the top of the stack
Object peek() -- look at the element on the top of the
boolean empty() -- check if the stack is empty
- There are many uses for stacks, and we'll keep coming back to them
as the term progresses. These uses include:
- Providing support for recursive procedure calls
- Searching structures
- As you might guess from our multiple implementations of lists,
there are also multiple implementations of stacks.
- The implementation we choose may depend on our knowledge of the
use of the stack (e.g., are we going to have a limited or unlimited size?)
and the limitations of our programming language (e.g., does it support
dynamic data allocation).
- The two most basic ways to implement stacks are with vectors and
- A list-based implementation is relatively straightforward, as
many list implementations include all the standard stack operations.
push(o) is just another name for
pop() is just another name for
peek() is just another name for
- Array-based (or vector-based) implementations are nearly as easy. We
simply add elements to the end of the array, and remove elements
from the end of the array.
- We may need to keep track of the position of the last element, but
we can also use the
- Note that this is much easier than general array-based representation of
lists, in which we must also keep track of the front (and potentially
move it). When representing stacks, the bottom (front) of the stack is
always at position 0, since it's the first element added and the last
- Many people, from mathematicians to primary school students, have
criticized our standard infix representation of expressions
as being ambiguous, or using a complex system for resolving
- Consider the expression 2 + 3 * 5. Which operation do we do first?
Left-to-right makes sense, especially to beginners, but we use a
complex system of priorities to determine which operation is done first.
- This is also complex to compute, as you need to look ahead to determine
whether or not it is safe to do an operation (hmmm ... how far do you
need to look ahead).
- For this reason, alternate notations have been developed. One of the
easiest is reverse polish notation (RPN) in which the operations
follows the operands.
- This is also called postfix notation.
- RPN is traditionally implemented using stacks and two basic rules.
- When you see a number, push it on the stack.
- When you see an operation, pop the operands off the stack, do the
operation, and push the result back on the stack.
- For example, to add two to three and then multiply by five, we'd use
2 3 + 5 *.
- Similarly, to multiply three and five and then add 2, we might write
2 3 5 * +
- RPN is unambiguous and requires no parenthesization. It is used by
a number of calculators (particularly HP's) and is supported by the
- Often, linear structures are used to organize tasks to be done
(e.g., places in a maze to look at, potential solutions to a
programming problem, ...).
- One problem with stacks is that the first things that are added
to the stack are the last removed, so if we keep finding new
things to try, we'll never try the first ones.
- To provide a sense of fairness, we might instead require that the
earlier tasks are done first.
- Queues are first-in first-out (FIFO) linear structures. They are similar
to lines at a bank teller or elsewhere.
- The main methods in queues are
enqueue(Object o) -- add an object to the back of the queue
Object dequeue() -- remove an object from the front of
Object front() -- determine what's at the front of the
queue, but don't remove it.
boolean empty() -- are there any elements in the queue?
- Like stacks, queues are used for a variety of purposes.
- Like stacks, queues can be implemented in multiple ways.
- In some odd cases, it may make sense to add elements to or remove
elements from both the front and back of the queue.
- Queues that support this extension are called
doubly-ended queues or simply dequeues (isn't
that a wonderful name conflict with the operation?)
- In effect, these are generalized lists without cursors.