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.

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

• initializeQueue
Sets count to 0, first to 0, and last to 0.
• 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.)

## 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:

• Code, with test runs, for parts 1-3 combined.
• Code, with test runs, for part 4.

This document is available on the World Wide Web as

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