Array-Based Queues
Summary: This laboratory exercise introduces the concept of the queue abstract data type and provides experience implementing this ADT with an array.
Queues ADT
The reading on queues describes the queue ADT with the following operations:
-
initialize
Initializescountas 0,firstas 0, andlastasMaxQueue-1. -
create
Create a new, empty queue object. -
isEmpty
Determine whether the queue is empty; return true if it is and false if it is not. -
isFull
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.)
In this lab, we will focus on arrays of strings, and this leads to the following declarations for queues:
/* size of all queue arrays */
#define MaxQueue 50
typedef struct {
int first;
int last;
int count;
char* items[MaxQueue];
} stringQueue;
With this framework, the signatures for the various queue operations might be as follows:
void
initialize(stringQueue* queue);
int
isEmpty(const stringQueue* queue);
int
isFull(const 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 */
char*
front(const stringQueue* queue);
Additional implementation notes are found in the reading on queues.
Exercises
-
Create a directory for this lab in your directory for this class, and
move to the lab directory. For example:
mkdir queues cd queues
-
Copy the declarations the array-based
queue ADT given above to a header file called
arrayQueue.h.- Remember to cite the origin of your code.
-
Add an
#ifndefinclude "guard" to avoid repeated declarations
-
Write an implementation of a queue of strings by implementing
these operations in a program file called
arrayQueue.c. -
Perhaps using the
Makefilefrom the previous reading on program management as an example, create aMakefilewith a rule for compiling an object file (.o) of your array-based queue. Be sure to include all the relevant file dependencies. -
After writing functions, it is important to test your code to
ensure that it works as you expected and accounts for unusual
cases.
-
Create a test driver program
called
testArrayQueue.cwith amainfunction. -
Add two more rules to your
Makefile: one to compile an object file for your driver, and another to link the two object files. Test thatmakeproperly compiles and links all three targets. - Complete thorough tests of each operation in your code.
-
Create a test driver program
called
-
Add to your queue ADT an additional function
printthat displays each of the elements of the queue on a separate line (without actually removing any of them from the queue). Add code to your driver program that tests the functionality of the new function. -
When calling
dequeueIn our current formulation, we cannot distinguish an empty queue from an null string pointer (which may have been enqueued). An alternative approach for thedequeuefunction would add a parameteritemto the list of parameters and change the return type to aninttype. The idea is that the string would be returned as a parameterchar* itemand the procedure would return the length of the stringitemor -1 if the queue was empty. The relevant procedure signature might be:
and this procedure would be called within the context:int dequeue(stringQueue* queue, char** item)char* frontItem; int returnValue; stringQueue myQueue; ... returnValue = dequeue (&myQueue, &frontItem)-
The parameter
itemhas typechar**, and the call toqueueincludes&frontItem. Explain why**and&are needed here. -
Write this alternative version of the
dequeueoperation.
-
The parameter
