CSC 161 Grinnell College Spring, 2010
 
Imperative Problem Solving and Data Structures
 

Queues with Arrays

Goals

This laboratory exercise introduces the concept of the queue abstract data type and provides experience with an array implementation of this ADT.

Queues as ADTs

The reading describes a queue as an ADT that can store data and that has the following operations:

Implementing Queues

For this lab, we will focus on arrays of strings, and this leads to the following declarations for queues:

#define MaxQueue 50  /* size of all queue arrays */

typedef struct {
   int first;
   int last;
   int count;
   char * queueArray [MaxQueue];
} stringQueue;

With this framework, the signatures for the various queue operations might be as follows:

   void initializeQueue (stringQueue * queue)
   int empty (stringQueue queue)
   int full (stringQueue queue)
   int enqueue (stringQueue * queue, char* item) 
      (returns length of string added or -1 if queue is full)
   char * dequeue (stringQueue * queue)
      (returns string removed from queue)

Additional implementation notes may be found in today's reading on Queues with Arrays.

  1. Write an implementation of a queue of strings by implementing these operations.

  2. Use the queue operations within a main procedure to provide thorough testing of each operation.

  3. Add to the queue ADT an additional procedure print that displays each of the elements of the queue on a separate line (without actually removing any of them from the queue).

  4. An alternative approach for the dequeue procedure would add a parameter item to the list of parameters and change the return type to an int type. The idea is that the string would be returned as a parameter char * item and the procedure would return the length of the string item or -1 if the queue was empty. The relevant procedure signature might be:

       int dequeue (stringQueue * queue, char ** item)
    

    and this procedure would be called within the context:

      char * frontItem;
      int returnValue;
      stringQueue myQueue;
      ...
      returnValue = dequeue (&myQueue, &frontItem)
    
    1. The parameter item has type char **, and the call to queue includes &frontItem. Explain why ** and & are needed here.
    2. Write this alternative version of the dequeue operation.

Work to be turned in:


This document is available on the World Wide Web as

http://www.cs.grinnell.edu/~walker/courses/161.sp10/labs/lab-queues-arrays.shtml

created 28 April 1997
last revised 4 April 2010
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.