Lab 15: Experiment 2

Working with a queue

The Problem

Here's the problem that looks a lot like the one in Experiment #1: what happens if we read in a bunch of words, push them all onto a queue, and then, as we pop them off, display them?

The Prediction

Try this out on paper first:

Question #15.2.1: Suppose the words in the sentence "Madam, I'm Adam." were pushed one after the other onto the same queue, and then they were printed as they were popped off. What would be printed?

Now, granted, this is only one example, but perhaps it's enough to make a prediction:

Question #15.2.2: What will the (eventual) program do in general to any sequence of words?


This is so similar to the stack problem, you're going to do all of the design for this one. You'll find that copying the design for Experiment #1 and changing a few words is all you need to do.

Question #15.2.3: What is the behavior of this program?

Question #15.2.4: What are the objects needed in the program?

Question #15.2.5: What are the operations needed in the program? Similar to Experiment #1, you can ignore the old, familiar input/output operations.

Question #15.2.6: What is the algorithm for this program?

Write the program, compile it, and run it.

The Analysis

Compare your prediction with the actual outcome.

Question #15.2.7: How closely did your prediction match the actual outcome?

A queue is sometimes known as a FIFO container---First In, First Out. The first item you push onto a queue will be the first one out.

Question #15.2.8: How does FIFO explain the behavior you see from your program?


Back to the Lab Exercise
© 2003 by Prentice Hall. All rights reserved.
Report all errors to Jeremy D. Frens.