Thought exercises
Queues
- Queues allow you to insert at one end and remove (dequeue)
at the other. A deque is a double-ended queue: it
allows you to insert or remove at either end. What are the
merits of representing a deque as:
- Fixed array
- Singly linked list
- Doubly linked list
Can you draw a picture of how the addToFront(),
removeFromFront(), addToBack(), and removeFromBack() would
be implemented for each representation?
Stacks
- Obviously, it's possible to implement arbitrarily-sized
stacks using lists (linked lists or dynamic arrays). However,
lower-level forms are sometimes acceptable if we know the
stack will remain less than an upper bound. Can you think of
a way to implement two stacks, using a single array, so
that the stacks will only be full if the total number of items
in both stacks is equal to the total size of the array? Draw
a picture.
- Imagine that we want to simulate a data tape with a single
tape head. We can only read from, or write to, the location
directly under the head. How can we use two stacks to
simulate this tape? Draw a picture of the result. How do we
move the tape head left or right?
Last modified: Wed Jul 19 19:30:10 PDT 2000