Chapter 4 - Queue 2019

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

CMP344

DATA STRUCTURES AND ALGORITHMS

prepared by: Nur Hanani Azami 2014 & Mohd Hazli Zabil 2019

1
CHAPTER 4
QUEUE

2
Learning Outcomes
 In this chapter, you will learn about:
◦ Introduction to Queue
◦ Queue operations
◦ Implementation of Queue with Circular Array
◦ Implementation of Queue in C++ STL
◦ Priority Queue

3
Introduction to Queue
 Definition 1:
A queue is a set of elements of the same type in which the elements are
added at one end, called the back or rear, and deleted from the other end,
called the front or first

 Definition 2:

4
Queue Operations
A QUEUE IS A CONTAINER IN WHICH:

. INSERTIONS ARE MADE ONLY AT


THE BACK;

. DELETIONS, RETRIEVALS, AND


MODIFICATIONS ARE MADE ONLY
AT THE FRONT.

5
Queue Operations

6
Queue Operations

7
Queue Operations

8
Exercise 1:

9
Implementation of Queue

10
Implementation of Queue

11
Implementation of Queue

12
Implementation of Queue

13
Implementation of Queue with Circular Array

14
Implementation of Queue with Circular Array

15
Implementation of Queue with Circular Array

16
Implementation of Queue in C++ STL
 Queue in STL
◦ Queues are a type of container adaptors which
operate in a first in first out (FIFO) type of
arrangement. Elements are inserted at the back
(end) and are deleted from the front.
◦ Queue functions in STL are as follows:
 empty() – returns whether the queue is empty
 size() – returns the size of the queue
 front() – returns a reference to the first element of the
queue
 back() – returns a reference to the last element of the
queue
 push() – Adds the element at the back of the queue
 pop() – Deletes the element at the front of the queue
17
Sample coding of Queue in STL – push, display front and back data

18
Sample coding of Queue in STL – push, pop and display data

19
Priority Queue

20
Priority Queue in C++ STL
 Priority queues are a type of container adapters, extension of the queue
specifically designed such that the first element of the queue is the
greatest of all elements in the queue and elements are in non increasing
order (hence we can see that each element of the queue has a
priority{fixed order}).
 It is similar to the queue in certain aspects and yet it differs from the
ordinary queue in the following points:

 Each item in the priority queue is associated with a priority.

 The item with the highest priority is the first item to be removed from the
queue.

 If more than one item has the same priority, then their order in the queue
is considered.

21
Priority Queue in C++ STL

22
Sample: Priority Queue in C++ STL

// Note that by default C++ creates a max-heap for priority queue

#include <iostream>
#include <queue>

using namespace std;

void showpq(priority_queue <int> mq)


{
priority_queue <int> q = mq;
while (!q.empty())
{
cout << '\t' << q.top();
q.pop();
}
cout << '\n';
}

23
int main ()
{
priority_queue <int> myQ;
myQ.push(10);
myQ.push(30);
myQ.push(20);
myQ.push(5);
myQ.push(1);

cout << "The priority queue MyQ is : ";


showpq(myQ);
cout << "\nMyQ.size() : " << myQ.size();
cout << "\nMyQ.top() : " << myQ.top();
cout << "\nMyQ.pop() : ";
myQ.pop();
showpq(myQ);
return 0;
}

24
End of Chapter 4

25

You might also like