Professional Documents
Culture Documents
Chapt 4 - II
Chapt 4 - II
Chapt 4 - II
Chap 4-II
Queues
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of 2
Computer Science
Queues
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of 3
Computer Science
• The queue abstract data type (ADT) supports the
following operations:
• enqueue(e): Insert element e at the rear of the
queue.
• dequeue(): Remove element at the front of the
queue; an error occurs if the queue is empty.
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of 4
Computer Science
front(): Return, but do not remove, a reference to the front
element in the queue; an error occurs if the queue is empty.
The queue ADT also includes the following supporting
member functions:
size(): Return the number of elements in the queue.
empty(): Return true if the queue is empty and false
otherwise.
We illustrate the operations in the queue ADT in the
following example.
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of 5
Computer Science
Example 5.4: The following table shows a series of queue
operations and their effects on an initially empty queue, Q, of
integers.
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of 6
Computer Science
Queue Operations
• Some of the queue operations are:
− initializeQueue
− isEmptyQueue
− isFullQueue
− front
− back
− addQueue
− deleteQueue
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 7
Implementation of Queues as
Arrays
• You need at least four (member) variables:
− An array to store the queue elements
− queueFront and queueRear
• To keep track of first and last elements
− maxQueueSize
• To specify the maximum size of the queue
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 8
Implementation of Queues as
Arrays (continued)
• To add an element to the queue:
− Advance queueRear to next array position
− Add element to position pointed to by
queueRear
• Example: array size is 100; originally empty
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 9
Implementation of Queues as
Arrays (continued)
• To delete an element from the queue:
− Retrieve element pointed to by queueFront
− Advance queueFront to next queue element
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 10
Implementation of Queues as
Arrays (continued)
• Will this queue design work?
− Suppose A stands for adding an element to
the queue
− And D stands for deleting an element from the
queue
− Consider the following sequence of
operations:
• AAADADADADADADADA...
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 11
Implementation of Queues as
Arrays (continued)
• The sequence AAADADADADADADADA...
would eventually set queueRear to point to
the last array position
− Giving the impression that the queue is full
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 12
Implementation of Queues as
Arrays (continued)
• Solution 1:
− When the queue overflows to the rear (i.e.,
queueRear points to the last array position):
• Check value of queueFront
• If value of queueFront indicates that there is
room in the front of the array, slide all of the
queue elements toward the first array position
• Problem: too slow for large queues
• Solution 2: assume that the array is circular
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 13
Implementation of Queues as
Arrays (continued)
• To advance the index in a (logically) circular
array:
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 14
Implementation of Queues as
Arrays (continued)
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 15
Implementation of Queues as
Arrays (continued)
• Case 1:
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 16
Implementation of Queues as
Arrays (continued)
• Case 2:
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 17
Implementation of Queues as
Arrays (continued)
• Problem:
− Figures 19-47 and 19-49 have identical values
for queueFront and queueRear
− However, the former represents an empty
queue, whereas the latter shows a full queue
• Solution?
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 18
Implementation of Queues as
Arrays (continued)
• Solution 1: keep a count
− Incremented when a new element is added to
the queue
− Decremented when an element is removed
− Initially, set to 0
− Very useful if user (of queue) frequently needs
to know the number of elements in the queue
• We will implement this solution
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 19
Implementation of Queues as
Arrays (continued)
• Solution 2: let queueFront indicate index of
the array position preceding the first element
− queueRear still indicates index of last one
− Queue empty if:
• queueFront == queueRear
− Slot indicated by queueFront is reserved
• Queue can hold 99 (not 100) elements
− Queue full if the next available space is the
reserved slot indicated by queueFront
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 20
Implementation of Queues as
Arrays (continued)
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 21
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of 22
Computer Science
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of 23
Computer Science
Empty Queue and Full Queue
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 24
Initialize Queue
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 25
Front
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 26
Back
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 27
addQueue
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 28
deleteQueue
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 29
Constructors
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 30
Destructors
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 31
Linked Implementation of Queues
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 32
Linked Implementation of Queues
(continued)
• Elements are added at one end and removed
from the other
− We need to know the front of the queue and
the rear of the queue
• Two pointers: queueFront and queueRear
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 33
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of 34
Computer Science
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of 35
Computer Science
Empty and Full Queue
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 36
Initialize Queue
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 37
addQueue
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 38
front and back Operations
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 39
deleteQueue
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 40
Default Constructor
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 41
Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of 42
Computer Science