0% found this document useful (0 votes)
8 views7 pages

Lesson 42

Uploaded by

kdudeteam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

Lesson 42

Uploaded by

kdudeteam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

COMPUTER SCIENCE - C++

LESSON 42: QUEUES

OBJECTIVES: The student will use the apqueue class to solve problems.

ACTIVITIES/TIME: Three Days


Day one: Presentation - 30 minutes
Lab Exercise - 20 minutes
Days two - three: Lab Exercise

MATERIALS: Student Outline O.A.42.1


Transparency T.A.42.1, Linked-List Representation of a Queue
Handout H.A.42.1, apqueue
Lab Exercise L.A.42.1, Printbylevel
Answer Sheet L.A.42.1
Lab Exercise L.A.42.2, RPN
Answer Sheet L.A.42.2

REFERENCES: Deitel, Harvey M. and Paul J. Deitel. C++ How to Program.


Pages 713-716.

INSTRUCTOR
NOTES: After presenting queues as an abstraction, two lab exercises will
focus on using queues. The second lab requires both stacks and
queues.

You may choose to have students change the implementation of


the apqueue class to a linked list. As another lab exercise, if time
permits, have students code a templated queue class using linked
lists . As background, the implementation option of queues as
linked lists is discussed in the student outline. The transparency
T.A.42.1, Linked-List Representation of a Queue will illustrate the
best approach for implementation.

When working through the linked list implementation of the


queue, use the transparency T.A.42.1, to illustrate the enqueue and
dequeue problem. When using the transparency, cover the correct
version and discuss why the incorrect version will not work for the

APCS - C++, Lesson 42 ©1998, ICT


dequeue problem. Some students may suggest a doubly-linked list
which will work, but is not necessary. A queue can be
implemented as a singly-linked list with some minor adjustments
to the incorrect version. Using the transparency, demonstrate how
the correct version uses the external pointers myFront and myEnd
to effectively solve the enqueue and dequeue problems.

APCS - C++, Lesson 42 ©1998, ICT


STUDENT OUTLINE

Lesson 42: 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.

The key topics for this lesson are:

A. Queues
B. Operations on Queues
C. Implementing Queues as a Linked List

VOCABULARY: QUEUE ENQUEUE


DEQUEUE

DISCUSSION: A. Queues

1. A queue is a linear data structure which simulates waiting in


line. A queue has two ends, a front and a (rear) end.

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).

3. All the data stored in a queue must be of the same type.

4. A queue is the appropriate data structure when simulating


waiting in line. A printer which is part of a multi-user network
must process print commands on a first-come, first-served
basis. A queue would be used to store the jobs in a line.

B. Operations on Queues

1. The following operations are supported by the apqueue class:

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

2. Using the templated apqueue class is very similar to using the


apstack class. Here is a sample program which uses some of
the key operations provided by the apqueue class.

Program 42-1

#include <iostream.h>
#include <apqueue.cpp>

main()
{
apqueue <int> queue;
int k;

for (k=1; k<=5; k++)


queue.enqueue(k);
while (!queue.isEmpty())
{
queue.dequeue(k);
cout << k << " ";
}
return 0;
}

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.

C. Implementing Queues as a Linked List


1. A queue can be implemented as a vector or a linked list. The
author of the apqueue class chose to use an apvector. If we use
a linked list to implement a queue we must deal with the
following issues.
2. Two external pointers must be used to keep track of the front
and end of the queue. When discussing a queue it is traditional
to add new data at the end of the queue, as in "get in at the end
of the line." The term enqueue is used to describe this
operation.

3. Data will be removed from the "front" of the queue. This


operation is called dequeue.

See Transparency 4. A queue can be implemented as a singly-linked list if


T.A.42.1, Linked-List
Representation of a Queue.
the two external pointers are appropriately placed.
5. When a new piece of data is added to the tail of the queue
(enqueue), the external pointer myEnd must be moved to point
to the new node added to the list.

6. When an old piece of data is extracted from the queue


(dequeue), the external pointer myFront must be advanced to
the appropriate node after the data has been removed. The
delete operation should be applied to the unwanted node of
information.

SUMMARY/REVIEW: Queues serve a very useful function whenever a program needs to


simulate waiting in line. One of the lab exercises will require
queues to solve an intriguing problem regarding binary trees.

ASSIGNMENT: Lab Exercise L.A.42.1, Printbylevel


Lab Exercise L.A.42.2, RPN

You might also like