Fundamentals of Computer Science I (CS151 2003F)

Lab

Summary: In this lab, you will investigate some aspects of our implementation of the Queue ADT. You will also compare how an algorithm differs depending on whether it uses queues or stacks.

Resources:

Contents:

Exercises

Exercise 0: Preparation

a. Make your own copy of stacks.scm and queues.scm.

b. Start DrScheme.

c. Open each of the preceding files in DrScheme.

d. Create a new window with the following two lines

(load "stacks.scm")
(load "queues.scm")

Exercise 1: Comparing Stacks and Queues

Consider the following set of operations:

a. What do you expect to happen if the add is treated as a stack-based push and the get as a stack-based pop?

b. Verify your answer experimentally.

c. What do you expect to happen if the add is treated as a queue-based enqueue and the get as a queue-based dequeue?

d. Verify your answer experimentally.

Exercise 2: Observing the Implementation

a. Add a dump message to the queue implementation. When the queue receives this message, it should display both lists in whatever way you find most useful. For example

> (myqueue ':dump)

Front: (b c)
Back: (f e d)

b. Redo the enqueue and dequeue operations given above, observing the state of the queue after each step.

c. What, if anything, do your results tell you about our implementation?

Exercise 3: Printing Symbol Trees

As you may recall, a symbol tree is either (1) a symbol or (2) a pair of two symbol trees. Here's one technique for printing all the symbols in a tree, based on the concept fo deep recursion:

(define print-symbols
  (lambda (tree)
    (if (pair? tree)
        (begin
          (print-symbols (car tree))
          (print-symbols (cdr tree)))
        (begin
          (display tree)
          (newline)))))

a. What do you expect the output to be for the following tree?

(define animals
  (cons 
   (cons 
    (cons "Aardvark" "Baboon")
    "Chimp")
   (cons
    "Dingo"
    (cons
     (cons "Emu" "Frog")
     (cons "Gnu" "Hippo")))))

b. Confirm your results experimentally.

c. It is also possible to write a flat-recursive procedure to print the tree by storing the subtrees to print in a queue. At each step, you get something from the queue. If it's a pair, you enqueue both subtrees. Otherwise, you print it out. You stop when the queue is empty.

Implement this procedure.

d. What do you expect the output to be given the tree above?

e. Confirm your results experimentally.

f. Instead of using a queue for step c, you could use a stack. Create a variant of the procedure that uses stacks instead of queues.

g. What do you expect the output of your procedure to be given the tree above?

h. Confirm your results experimentally.

i. How can you replicate the results of the original procedure using only a stack or a queue?

 

History

Thursday, 11 December 2003 [Samuel A. Rebelsky]

 

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Thu Dec 11 13:51:58 2003.
The source to the document was last modified on Thu Dec 11 13:51:56 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003F/Labs/queues.html.

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu