Held Monday, April 10, 2000
Overview
Today we move on to a new class of data structures, the
so-called linear structures.
Notes
- For tomorrow, do your best to get something close to working for
your part of the project.
- I don't expect it to be perfect, but I want to see some progress.
- Let me know if you have questions.
- For Friday, finish assignment
2.
- Are there questions on that assignment?
Contents
Summary
- Linear Structures
- Three types of linear structures
- Stacks
- Queues
- Priority queues
- A problem with some kinds of lists is that they provide just too
many options.
- In ordered lists, you might put at and retrieve elements from the
front, back, or even middle of the list.
- Should you use simple lists, sequenced lists, sorted lists,
indexed lists, vectors, or what?
- This multiplicity leads to some difficulties.
- Implementation can be more difficult, as we've already seen.
- Clients may find it difficult to use lists, as they must
consider the interrelationship of the different functions.
- Many uses of lists can be simplified to three or four basic operations:
- add an element
- get an element, removing it from the structure
- check if the structure is empty
- [check if the structure is full]
- Some add a peek method which looks at the next element
to be removed.
- Structures that support these three or four operations are often called
linear structures.
- We can think of this in terms of a typical set of stuff to do.
You add tasks as they come up. Whenever you have free time, you
pick another task to do.
- 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
select objects.
- We call our basic methods
add
and get
.
- Good design would suggest that every linear structure use the same
names for these basic operations.
- Unfortunately, when these structures were first designed, no one
had thought about the commonality. Hence, the ``standard'' names
for operations in linear structures differ from structure to structure.
- 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.
- They are also similar to the stacks used to implement function calls
in typical languages.
- 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
stack
-
boolean isEmpty()
-- 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
- Computation
- One interesting aspect of stacks is that push and pop are clear inverses.
If you push an element and then pop, the stack ends up the same. In
other policies, this may not be the case.
- There are, of course, many ways to implement stacks. We can use
arrays, lists, nodes, ....
- 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
the queue
-
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.
- We might further refine our sense of ``queue'' by adding a
priority to the elements of the queue. Rather than
using FIFO, we'll now use ``lowest/highest priority first''.
- Linear structures that use priority in choosing the next element
are priority queues.
- Are priority queues queues?
- No, in the sense that many elements do not follow a FIFO ordering.
- Yes, in the sense that all elements of equal priority are expected
to be returned in a FIFO ordering.
- It is likely the second observation that led to the naming.
- 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 a kind of ordered list, but without cursors.
- Note that we cannot have dequeues match the three/four basic
operations of linear structures.