Lesson 42
Lesson 42
OBJECTIVES: The student will use the apqueue class to solve problems.
INSTRUCTOR
NOTES: After presenting queues as an abstraction, two lab exercises will
focus on using queues. The second lab requires both stacks and
queues.
INTRODUCTION: Queues are another restricted access data structure, much like
stacks. A queue is like a one-way line where data enters the end of
the line and exits from the front of the line. The apqueue class
will support operations similar to but different from stacks.
A. Queues
B. Operations on Queues
C. Implementing Queues as a Linked List
DISCUSSION: A. Queues
2. Data must always enter the queue at the end and leave from the
front of the line. This type of action can be summarized as
FIFO (first-in, first-out).
B. Operations on Queues
Constructors
apqueue(); // default constructor
apqueue (const apqueue & q); // copy constructor
~apqueue (); // destructor
Assignment
const apqueue & operator= (const apqueue &rhs);
Accessors
const itemType & front() const; // return front, no dequeue
bool isEmpty() const; // return true if empty, else false
int length() const; // return number of elements in
queue
Modifiers
void enqueue (const itemType &item); // insert item at rear
void dequeue (); // remove first element
void dequeue (itemType & item); // combine front and
dequeue
void makeEmpty (); // make queue empty
Program 42-1
#include <iostream.h>
#include <apqueue.cpp>
main()
{
apqueue <int> queue;
int k;
Run output:
1 2 3 4 5
See Handout H.A.42.1, 3. See Handout H.A.42.1, apqueue for the full specifications
apqueue.
of the apqueue class.