Chapter 8 Queues Data Structures Using C 1

  • Slides: 38
Download presentation
Chapter 8 Queues Data Structures Using C++ 1

Chapter 8 Queues Data Structures Using C++ 1

Chapter Objectives • Learn about queues • Examine various queue operations • Learn how

Chapter 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++ 2

Chapter Objectives • Discover queue applications • Examine the STL class queue • Discover

Chapter Objectives • Discover queue applications • Examine the STL class queue • Discover priority queues Data Structures Using C++ 3

Queues • Definition: data structure in which the elements are added at one end,

Queues • Definition: data structure in which the elements are added at one end, called the rear, and deleted from the other end, called the front or first • First In First Out (LIFO) data structure Data Structures Using C++ 4

Basic Operations on a Queue • initialize. Queue: Initializes the queue to an empty

Basic Operations on a Queue • initialize. Queue: Initializes the queue to an empty state • destroy. Queue: Removes all the elements from the queue, leaving the queue empty • is. Empty. Queue: Checks whether the queue is empty. If the queue is empty, it returns the value true; otherwise, it returns the value false Data Structures Using C++ 5

Basic Operations on a queue • is. Full. Queue: Checks whether the queue is

Basic Operations on a queue • is. Full. Queue: Checks whether the queue is full. If the queue is full, it returns the value true; otherwise, it returns the value false • front: Returns the front (first) element of the queue; the queue must exist • back: Returns the front (first) element of the queue; the queue must exist Data Structures Using C++ 6

Basic Operations on a queue • add. Queue: Adds a new element to the

Basic Operations on a queue • add. Queue: Adds a new element to the rear of the queue; the queue must exist and must not be full • delete. Queue: Removes the front element of the queue; the queue must exist and must not be empty Data Structures Using C++ 7

Queues as Arrays Data Structures Using C++ 8

Queues as Arrays Data Structures Using C++ 8

Queues as Arrays Data Structures Using C++ 9

Queues as Arrays Data Structures Using C++ 9

Circle Queue Data Structures Using C++ 10

Circle Queue Data Structures Using C++ 10

Queues as Arrays Data Structures Using C++ 11

Queues as Arrays Data Structures Using C++ 11

Queues as Arrays Data Structures Using C++ 12

Queues as Arrays Data Structures Using C++ 12

Queues as Arrays Data Structures Using C++ 13

Queues as Arrays Data Structures Using C++ 13

Queues as Arrays Data Structures Using C++ 14

Queues as Arrays Data Structures Using C++ 14

Queues as Arrays Data Structures Using C++ 15

Queues as Arrays Data Structures Using C++ 15

Queues as Arrays Data Structures Using C++ 16

Queues as Arrays Data Structures Using C++ 16

Queues as Arrays Data Structures Using C++ 17

Queues as Arrays Data Structures Using C++ 17

UML Diagram of the class queue. Type Data Structures Using C++ 18

UML Diagram of the class queue. Type Data Structures Using C++ 18

Initialize Queue Definition of the function initialize. Queue: template<class Type> void queue. Type<Type>: :

Initialize Queue Definition of the function initialize. Queue: template<class Type> void queue. Type<Type>: : initialize. Queue() { queue. Front = 0; queue. Rear = max. Queue. Size - 1; count = 0; } Data Structures Using C++ 19

Empty Queue and Full Queue template<class Type> bool queue. Type<Type>: : is. Empty. Queue()

Empty Queue and Full Queue template<class Type> bool queue. Type<Type>: : is. Empty. Queue() { return (count == 0); } template<class Type> bool queue. Type<Type>: : is. Full. Queue() { return (count == max. Queue. Size); } Data Structures Using C++ 20

Destroy Queue template<class Type> void queue. Type<Type>: : destroy. Queue() { queue. Front =

Destroy Queue template<class Type> void queue. Type<Type>: : destroy. Queue() { queue. Front = 0; queue. Rear = max. Queue. Size - 1; count = 0; } Data Structures Using C++ 21

front and back template<class Type> Type queue. Type<Type>: : front() { assert(!is. Empty. Queue());

front and back template<class Type> Type queue. Type<Type>: : front() { assert(!is. Empty. Queue()); return list[queue. Front]; } template<class Type> Type queue. Type<Type>: : back() { assert(!is. Empty. Queue()); return list[queue. Rear]; } Data Structures Using C++ 22

Add Queue template<class Type> void queue. Type<Type>: : add. Queue(const Type& new. Element) {

Add Queue template<class Type> void queue. Type<Type>: : add. Queue(const Type& new. Element) { if(!is. Full. Queue()) { queue. Rear = (queue. Rear + 1) % max. Queue. Size; //use the mod //operator to advance queue. Rear //because the array is circular count++; list[queue. Rear] = new. Element; } else cerr<<"Cannot add to a full queue. "<<endl; } Data Structures Using C++ 23

Delete Queue template<class Type> void queue. Type<Type>: : delete. Queue() { if(!is. Empty. Queue())

Delete Queue template<class Type> void queue. Type<Type>: : delete. Queue() { if(!is. Empty. Queue()) { count--; queue. Front = (queue. Front + 1) % max. Queue. Size; //use the mod //operator to advance queue. Front //because the array is circular } else cerr<<"Cannot remove from an empty queue. "<<endl; } Data Structures Using C++ 24

Constructor and Destructor • Constructor – Creates an array of the size specified by

Constructor and Destructor • Constructor – Creates an array of the size specified by the user – Default value is 100 – Initializes queue. Front queue. Rear to indicate that the queue is empty • Destructor – When queue object goes out of scope, destructor reallocates memory occupied by the array that stores the queue elements Data Structures Using C++ 25

Linked Queue as an ADT Data Structures Using C++ 26

Linked Queue as an ADT Data Structures Using C++ 26

Empty, Full, and Destroy Queue • Queue is empty if queue. Front is NULL

Empty, Full, and Destroy Queue • Queue is empty if queue. Front is NULL • Queue is full only if we run out of memory • Destroy queue removes all elements of the queue Data Structures Using C++ 27

add. Queue • Adds a new element to the end of the queue •

add. Queue • Adds a new element to the end of the queue • Access the pointer queue. Rear to implement add. Queue Data Structures Using C++ 28

Front, Back, and Delete Queue • If queue is nonempty: – operation front returns

Front, Back, and Delete Queue • If queue is nonempty: – operation front returns the first element of the queue – operation back returns the last element of the queue – operation delete. Queue removes the first element of the queue • If queue is empty: – function front terminates the program – function back terminates the program Data Structures Using C++ 29

STL class queue (Queue Container Adapter) Data Structures Using C++ 30

STL class queue (Queue Container Adapter) Data Structures Using C++ 30

Priority Queue • FIFO rules of a queue are relaxed • Customers or jobs

Priority Queue • FIFO rules of a queue are relaxed • Customers or jobs with higher priority are pushed to front of queue • To implement: – use an ordinary linked list, which keeps the items in order from the highest to lowest priority – use a treelike structure Data Structures Using C++ 31

Application of Queues Data Structures Using C++ 32

Application of Queues Data Structures Using C++ 32

Application of Queues Data Structures Using C++ 33

Application of Queues Data Structures Using C++ 33

Application of Queues Data Structures Using C++ 34

Application of Queues Data Structures Using C++ 34

Application of Queues: waiting. Customer. Queue. Type Data Structures Using C++ 35

Application of Queues: waiting. Customer. Queue. Type Data Structures Using C++ 35

Poisson Distribution Data Structures Using C++ 36

Poisson Distribution Data Structures Using C++ 36

Chapter Summary • Queue Data Structure – Restricted Version of arrays and linked list

Chapter Summary • Queue Data Structure – Restricted Version of arrays and linked list – Basic operations • First In First Out (FIFO) • Queues Implemented as Arrays Data Structures Using C++ 37

Chapter Summary • • Queues Implemented as Linked Lists STL class queue Priority Queues

Chapter Summary • • Queues Implemented as Linked Lists STL class queue Priority Queues Application of Queues Data Structures Using C++ 38