Chapter 4 2 4 3 Queues Priority Queues

  • Slides: 17
Download presentation
Chapter 4. 2 & 4. 3 Queues & Priority Queues Data Structures and Algorithms

Chapter 4. 2 & 4. 3 Queues & Priority Queues Data Structures and Algorithms in Java

Objectives Discuss the following topics: • What is Queue? • Examples of Queue Application

Objectives Discuss the following topics: • What is Queue? • Examples of Queue Application • Queue Implementations • Priority Queue Data Structures and Algorithms in Java 2

What is Queue? • A queue is a waiting line that grows by adding

What is Queue? • A queue is a waiting line that grows by adding elements to its end (also called its rear) and shrinks by taking elements from its front • A queue is a structure in which both ends are used: – One for adding new elements – One for removing them • A queue is an FIFO structure: first in/first out Data Structures and Algorithms in Java 3

What is Queue? (continued) • The following operations are needed to properly manage a

What is Queue? (continued) • The following operations are needed to properly manage a queue: – clear() — Clear the queue – is. Empty() — Check to see if the queue is empty – enqueue(el) — Put the element el at the end of the queue – dequeue() — Take out the first element from the queue – first. El() — Return the first element in the queue without removing it Data Structures and Algorithms in Java 4

What is Queue? (continued) Figure 4. 7 A series of operations executed on a

What is Queue? (continued) Figure 4. 7 A series of operations executed on a queue Data Structures and Algorithms in Java 5

Examples of Queue Application • Waiting lines: Queues are commonly used in systems where

Examples of Queue Application • Waiting lines: Queues are commonly used in systems where waiting line has to be maintained for obtaining access to a resource. For example, an operating system may keep a queue of processes that are waiting to run on the CPU. • Access to shared resources (e. g. , network printer) • Multiprogramming Data Structures and Algorithms in Java 6

Queue Implementations • One possible queue implementation is an array, although this may not

Queue Implementations • One possible queue implementation is an array, although this may not be the best choice. • However, in order not to waste cells, the array is treated as circular (Figure 4. 8(c)) • Queue is Full if either: – first element is in the first cell and the last element in the last cell (Figure 4. 8 a) or – first element is right after the last (Figure 4. 8 b). • enqueue() and dequeue() must also consider the circular nature of the array – if last is at end off array, enqueue in first cell (Figure 4. 8 d), else enqueue at the next empty cell (Figure 4. 8 e) – After a dequeue, if first is at the end, next first is at the beginning, else, next first is at next cell. Data Structures and Algorithms in Java 7

Queue Implementations (continued) Figure 4. 8 Two possible configurations in an array implementation of

Queue Implementations (continued) Figure 4. 8 Two possible configurations in an array implementation of a queue when the queue is full Data Structures and Algorithms in Java 8

Queue Implementations (continued) Figure 4. 9 Array implementation of a queue Data Structures and

Queue Implementations (continued) Figure 4. 9 Array implementation of a queue Data Structures and Algorithms in Java 9

Queue Implementations (continued) Figure 4. 9 Array implementation of a queue (continued) Data Structures

Queue Implementations (continued) Figure 4. 9 Array implementation of a queue (continued) Data Structures and Algorithms in Java 10

Queue Implementations (continued) • A more natural queue implementation is a doubly linked list

Queue Implementations (continued) • A more natural queue implementation is a doubly linked list (Figure 4. 10). Figure 4. 10 Linked list implementation of a queue Data Structures and Algorithms in Java 11

Queue Implementations (continued) Figure 4. 10 Linked list implementation of a queue (continued) Data

Queue Implementations (continued) Figure 4. 10 Linked list implementation of a queue (continued) Data Structures and Algorithms in Java 12

Queue Implementations (continued) • Queuing theory is when various scenarios are analyzed and models

Queue Implementations (continued) • Queuing theory is when various scenarios are analyzed and models are built that use queues Figure 4 -12 A series of operations executed on an abstract queue (a) and the stack implemented with an array (b) and with a linked list (c) Data Structures and Algorithms in Java 13

Priority Queues • In many situations, simple queues are inadequate, as when first in/first

Priority Queues • In many situations, simple queues are inadequate, as when first in/first out scheduling has to be overruled using some priority criteria. Examples: – Emergency cases in Hospitals – Handicapped persons in a post office – police cars, ambulances, fire engines, etc. on roads with tollbooths – A sequence of processes where some processes must be executed before others for the proper functioning of the system Data Structures and Algorithms in Java 14

Priority Queues (continued) • A priority queue is a queue that allows elements to

Priority Queues (continued) • A priority queue is a queue that allows elements to be dequeued according to their priority and their current queue position • Priority queues can be represented by two variations of linked lists: – Ordered list. Each new element is enqueued according to its priority. In this case, dequeue is simple – dequeue the front element (as usual) – Unordered list. Elements are enqueued at the rear (as usual). In this case, the dequeue operation requires searching to find one with highest priority. Data Structures and Algorithms in Java 15

Summary • A stack is a linear data structure that can be accessed at

Summary • A stack is a linear data structure that can be accessed at only one of its ends for storing and retrieving data. • A stack is called an LIFO structure: last in/first out. • A queue is a waiting line that grows by adding elements to its rear and shrinks by taking elements from its front. • A queue is an FIFO structure: first in/first out. Data Structures and Algorithms in Java 16

Summary (continued) • A priority queue can be assigned to enable a particular process,

Summary (continued) • A priority queue can be assigned to enable a particular process, or event, to be executed out of sequence without affecting overall system operation. • In priority queues, elements are dequeued according to their priority and their current queue position. Data Structures and Algorithms in Java 17