| CSC 161 | Grinnell College | Spring, 2009 |
| Imperative Problem Solving and Data Structures | ||
This laboratory exercise introduces the concept of the queue abstract data type and provides experience with an array implementation of this ADT.
The reading describes a queue as an ADT that can store data and that has the following operations:
create
Create a new, empty queue object.
empty
Determine whether the queue is empty; return true if it is and
false if it is not.
full
Determine whether the queue is full; return true if it is and
false if it is not.
enqueue
Add a new element at the rear of a queue.
dequeue
Remove an element from the front of the queue and return it. (This
operation cannot be performed if the queue is empty.)
front
Return the element at the front of the queue (without removing it from the
queue). (Again, this operation cannot be performed if the queue is empty.)
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)
Write an implementation of a queue of strings by implementing these operations.
Use the queue operations within a main procedure to provide thorough testing of each operation.
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).
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)
This document is available on the World Wide Web as
http://www.cs.grinnell.edu/~walker/courses/161.sp09/labs/lab-queues-arrays.shtml
|
created 28 April 1997 last revised 4 May 2008 |
|
| For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |