Chapt 4 - II

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 42

Queues

Chap 4-II
Queues

A queue is a container of elements that are inserted and


removed according to the first-in first-out (FIFO)
principle. Elements can be inserted in a queue at any
time, but only the element that has been in the queue
the longest can be removed at any time. We usually say
that elements enter the queue at the rear and are
removed from the front.

Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of 2
Computer Science
Queues

Formally, the queue abstract data type defines a


container that keeps elements in a sequence, where
element access and deletion are restricted to the first
element in the sequence, which is called the front of the
queue, and element insertion is restricted to the end of
the sequence, which is called the rear of the queue.
This restriction enforces the rule that items are inserted
and deleted in a queue according to the first-in first-out
(FIFO) principle

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

• Returns the first element of the queue

Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 26
Back

• Returns the last element of the queue

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

• The array to store the queue elements is


created dynamically
− When the queue object goes out of scope, the
destructor simply deallocates the memory
occupied by the array

Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 31
Linked Implementation of Queues

• Array size is fixed: only a finite number of


queue elements can be stored in it
• The array implementation of the queue
requires array to be treated in a special way
− Together with queueFront and queueRear
• The linked implementation of a queue
simplifies many of the special cases of the
array implementation
− In addition, the queue is never full

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

• The queue is empty if queueFront is NULL


• The queue is never full

Data Structures and Algorithms in C++ Second Edition Michael T. Goodrich - Department of Computer Science 36
Initialize Queue

• Initializes queue to an empty state


− Must remove all the elements, if any

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

You might also like