Top Banner
DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE
38

DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

Dec 16, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

DATA STRUCTURE & ALGORITHMS

CHAPTER 4: QUEUE

Page 2: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

2

Queues

• Queue: list of homogeneous elements• Elements are:

– Added at one end (the back or rear)– Deleted from the other end (the front)

• First In First Out (FIFO) data structure– Middle elements are inaccessible

• Example:– Waiting line in a bank

Page 3: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

3

Queue Operations

• Some of the queue operations are:– initializeQueue– isEmptyQueue– isFullQueue– front– back– addQueue– deleteQueue

• Abstract class queueADT defines these operations

Page 4: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

4

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

Page 5: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

5

Implementation of Queues as Arrays (continued)

• To add an element to the queue:– Advance queueRear to next array position – Add element to position pointed by queueRear

• Example: array size is 100; originally empty

Page 6: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

6

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

Page 7: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

7

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...

Page 8: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

8

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

Page 9: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

9

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

Page 10: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

10

Implementation of Queues as Arrays (continued)• To advance the index in a (logically)

circular array:

Page 11: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

11

Implementation of Queues as Arrays (continued)

Page 12: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

12

Implementation of Queues as Arrays (continued)• Case 1:

Page 13: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

C++ Programming: Program Design Including Data Structures, Fourth Edition

13

Implementation of Queues as Arrays (continued)• Case 2:

Page 14: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

14

Implementation of Queues as Arrays (continued)

• Problem:– Figures 18-47 and 18-49 have identical

values for queueFront and queueRear– However, the former represents an empty

queue, whereas the latter shows a full queue

• Solution?

Page 15: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

15

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

Page 16: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

16

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

Page 17: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

17

Implementation of Queues as Arrays (continued)

Page 18: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

Exercise

Consider the following queue where circular QUEUE is allocated 6 memory cells:FRONT = 2, REAR = 5QUEUE: _______, London, Berlin, Rome, Paris, _______Describe the queue, including FRONT and REAR, as the following operations take place:1. Athens is added2. Two cities are deleted3. Madrid is added4. Moscow is added5. Three cities are deleted6. Oslo is added

Page 19: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

19

Empty Queue and Full Queue

Page 20: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

20

Initialize Queue

Page 21: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

21

Front

• Returns the first element of the queue

Page 22: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

22

Back

• Returns the last element of the queue

Page 23: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

23

addQueue

Page 24: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

24

deleteQueue

Page 25: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

25

Constructors and Destructors

Page 26: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

26

Constructors and Destructors (continued)• 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

Page 27: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

27

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

Page 28: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

28

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

Page 29: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

29

Empty and Full Queue

• The queue is empty if queueFront is NULL

• The queue is never full

Page 30: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

30

Initialize Queue

• Initializes queue to an empty state– Must remove all the elements, if any

Page 31: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

31

addQueue

Page 32: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

32

front and back Operations

Page 33: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

33

deleteQueue

Page 34: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

34

Default Constructor

Page 35: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.
Page 36: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

36

Queue Derived from the class unorderedLinkedListType

• The linked implementation of a queue is similar to the implementation of a linked list created in a forward manner– addQueue is similar to insertFirst– initializeQueue is like initializeList– isEmptyQueue is similar to isEmptyList– deleteQueue can be implemented as before– queueFront is the same as first– queueRear is the same as last

Page 37: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

37

Application of Queues: Simulation• Simulation: a technique in which one

system models the behavior of another system

• Computer simulations using queues as the data structure are called queuing systems

Page 38: DATA STRUCTURE & ALGORITHMS CHAPTER 4: QUEUE. 2 Queues Queue: list of homogeneous elements Elements are: –Added at one end (the back or rear) –Deleted.

38

Summary (continued)

• Queue: items are added at one end and removed from the other end– First In First Out (FIFO) data structure– Operations: add, remove, initialize, destroy,

check if queue is empty/full– Can be implemented as array or linked list– Middle elements should not be accessed– Restricted versions of arrays and linked

lists