0% found this document useful (0 votes)
38 views35 pages

Queue

The document discusses different implementations of queues using arrays and linked lists in C++. It covers queue operations like add, delete, front and back. For arrays, it describes using queueFront and queueRear pointers to track the front and rear of the queue. It also addresses issues like queue overflow and underflow. For linked lists, queues are implemented using pointers without size constraints. The queue class can also be derived from an existing linked list class.

Uploaded by

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

Queue

The document discusses different implementations of queues using arrays and linked lists in C++. It covers queue operations like add, delete, front and back. For arrays, it describes using queueFront and queueRear pointers to track the front and rear of the queue. It also addresses issues like queue overflow and underflow. For linked lists, queues are implemented using pointers without size constraints. The queue class can also be derived from an existing linked list class.

Uploaded by

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

Data Structures Using C++ 2E

Chapter 8
Queues
Objectives

• Learn about queues


• Examine various queue operations
• Learn how to implement a queue as an array
• Learn how to implement a queue as a linked list

Data Structures Using C++ 2E 2


Introduction

• Queue data structure


– Elements added at one end (rear), deleted from other
end (front)
– First In First Out (FIFO)
– Middle elements inaccessible

Data Structures Using C++ 2E 3


Queue Operations

• Two key operations


– addQueue
– deleteQueue
• Additional operations
– initializeQueue, isEmptyQueue,
isFullQueue, front, back
• queueFront, queueRear pointers
– Keep track of front and rear
• See code on pages 453-454

Data Structures Using C++ 2E 4


Implementation of Queues as Arrays
• Four member variables
– Array to store queue elements
– Variables queueFront, queueRear
– Variable maxQueueSize
• Using queueFront, queueRear to access queue
elements
– queueFront: first queue element index
– queueRear: last queue element index
• queueFront changes after each deleteQueue
operation
• queueRear changes after each addQueue operation

Data Structures Using C++ 2E 5


Implementation of Queues as Arrays
(cont’d.)
• Execute operation
– addQueue(Queue,'A');
• Execute
– addQueue(Queue,'B');
– addQueue(Queue,'C');
• Execute
– deleteQueue();

Data Structures Using C++ 2E 6


FIGURE 8-1 Queue after the first addQueue operation

FIGURE 8-2 Queue after two more addQueue operations

FIGURE 8-3 Queue after the deleteQueue operation

Data Structures Using C++ 2E 7


Implementation of Queues as Arrays
(cont’d.)
• Consider the sequence of operations:
AAADADADADADADADA... (A:add D:delete)
– Eventually index queueRear points to last array
position
• Looks like a full queue
• Reality: queue has two or three elements, array empty
in the front

FIGURE 8-4 Queue after the sequence


of operations AAADADADADADA...
Data Structures Using C++ 2E 8
Implementation of Queues as Arrays
(cont’d.)
• First solution
– Upon queue overflow to the rear
• Check value of queueFront
• If room in front: slide all queue elements toward first
array position
– Works if queue size very small
• Second solution: assume circular array

FIGURE 8-5 Circular queue


Data Structures Using C++ 2E 9
Implementation of Queues as Arrays
(cont’d.)
• queueRear = (queueRear + 1) %
maxQueueSize;
– Advances queueRear (queueFront) to next array
position

FIGURE 8-6 Queue before and after the add operation

Data Structures Using C++ 2E 10


Implementation of Queues as Arrays
(cont’d.)
• If queueRear < maxQueueSize – 1
▪ queueRear + 1 <= maxQueueSize – 1
▪ (queueRear + 1) % maxQueueSize = queueRear +
1
• If queueRear == maxQueueSize – 1
▪ queueRear + 1 == maxQueueSize
▪ (queueRear + 1) % maxQueueSize = 0
▪ queueRear set to zero
▪ First array position

Data Structures Using C++ 2E 11


Implementation of Queues as Arrays
(cont’d.)
• Two cases with identical queueFront, queueRear
values
– Figure 8-7(b) represents an empty queue
– Figure 8-8(b) represents a full queue

FIGURE 8-7 Queue before and after the delete operation

FIGURE 8-8 Queue before and after the add operation


Data Structures Using C++ 2E 12
Implementation of Queues as Arrays
(cont’d.)
• First solution: use variable count
– Incremented when new element added
– Decremented when element removed
– Functions initializeQueue, destroyQueue
initialize count to zero

Data Structures Using C++ 2E 13


Implementation of Queues as Arrays
(cont’d.)
• Second solution
– queueFront indicates index of array position
preceding first element of the queue
– Assume queueRear indicates index of last element
• Empty queue if queueFront == queueRear
– Slot indicated by index queueFront is reserved
– Queue is full
• If next available space represents special reserved slot

Data Structures Using C++ 2E 14


Implementation of Queues as Arrays
(cont’d.)

FIGURE 8-9 Array to store the queue


elements with a reserved slot

• See code on pages 459-460


– Uses first solution

Data Structures Using C++ 2E 15


Empty Queue and Full Queue
• Empty queue
– If count == 0
• Full queue
– If count == maxQueueSize

Data Structures Using C++ 2E 16


Initialize Queue
• Initializes queue to empty state
– First element added at the first array position
– Initialize queueFront to zero, queueRear to
maxQueueSize - one, count to zero

FIGURE 8-10 Empty queue

Data Structures Using C++ 2E 17


Front

• Returns first queue element


– If the queue nonempty
• Element indicated by index queueFront returned
– Otherwise
• Program terminates

Data Structures Using C++ 2E 18


Back

• Returns last queue element


– If queue nonempty
• Returns element indicated by index queueRear
– Otherwise
• Program terminates

Data Structures Using C++ 2E 19


Add Queue

Data Structures Using C++ 2E 20


Delete Queue

Data Structures Using C++ 2E 21


Constructors and Destructors

Data Structures Using C++ 2E 22


Constructors and Destructors (cont’d.)

• Array storing queue elements


– Created dynamically
– When queue object goes out of scope
• Destructor deallocates memory occupied by the array
storing queue elements

Data Structures Using C++ 2E 23


Linked Implementation of Queues

• Array implementation issues


– Fixed array size
• Finite number of queue elements
– Requires special array treatment with the values of
the indices queueFront, queueRear
• Linked implementation of a queue
– Simplifies special cases of the array implementation
– Queue never full
• See code on pages 464-465

Data Structures Using C++ 2E 24


Empty and Full Queue
• Empty queue if queueFront is NULL
• Memory allocated dynamically
– Queue never full
– Function implementing isFullQueue operation
returns the value false

Data Structures Using C++ 2E 25


Initialize Queue

• Initializes queue to an empty state


– Empty if no elements in the queue

Data Structures Using C++ 2E 26


addQueue, front, back, and
deleteQueue Operations
• addQueue operation adds a new element at end of
the queue
– Access the pointer queueRear

Data Structures Using C++ 2E 27


addQueue, front, back, and
deleteQueue Operations (cont’d.)
• If queue nonempty
– Operation front returns first element
– Element indicated queueFront returned
• If queue empty: front terminates the program

Data Structures Using C++ 2E 28


addQueue, front, back, and
deleteQueue Operations (cont’d.)
• If queue nonempty
– Operation back returns last element
– Element indicated by queueRear returned
• If queue empty: back terminates the program

Data Structures Using C++ 2E 29


addQueue, front, back, and
deleteQueue Operations (cont’d.)
• If queue nonempty
– Operation deleteQueue removes first element
• Access pointer queueFront

Data Structures Using C++ 2E 30


addQueue, front, back, and
deleteQueue Operations (cont’d.)
• Default constructor
– When queue object goes out of scope
• Destructor destroys the queue
• Deallocates memory occupied by the queue elements
– Function definition similar to function
initializeQueue

Data Structures Using C++ 2E 31


Queue Derived from the class
unorderedLinkedListType
• Linked queue implementation
– Similar to forward manner linked list implementation
– Similar operations
• add Queue, insertLast
• initializeQueue , initializeList
• isEmptyQueue, isEmptyList
– deleteQueue operation implemented as before
– Same pointers
• queueFront and first, queueRear and last

Data Structures Using C++ 2E 32


Queue Derived from the class
unorderedLinkedListType
(cont’d.)
• Linked queue implementation (cont’d.)
– Can derive the class to implement the queue from the
class linkedListType
– class linkedListType
• An abstract
• Does not implement all operations
– class unorderedLinkedListType
• Derived from class linkedListType
• Provides definitions of the abstract functions of the
class linkedListType

Data Structures Using C++ 2E 33


Summary

• Queue
– First In First Out (FIFO) data structure
– Implemented as array or linked list
– Linked lists: queue never full

Data Structures Using C++ 2E 34


Programming Exercises

• Chapter 8 – Exercise 1
• Chapter 8 – Exercise 2

Data Structures Using C++ 2E 35

You might also like