• No results found

Queues 1

N/A
N/A
Protected

Academic year: 2022

Share "Queues 1"

Copied!
56
0
0

Loading.... (view fulltext now)

Full text

(1)

Queues

(2)

Definition

A queue is a linear list in which data can only be inserted at one end, called the Rear, and deleted from the other end, called the Front.

This makes queue as FIFO(First in First Out) data structure, which means that element inserted first will be removed first.

Ex. just like any queue of peoples waiting at Ticket counter.

(3)
(4)
(5)

Applications of Queue

Queue, as the name suggests is used whenever we need to manage any group of objects in an order in which the first one coming in, also gets out first while the others wait for their turn, like in the following scenarios:

Serving requests on a single shared resource, like a printer, CPU task scheduling etc.

In real life scenario, Call Center phone systems uses Queues to hold people calling them in an order, until a service representative is free.

(6)

Operations On Queues

Queue is an abstract data type.

Following operations can be performed on queues:

Enqueue() - Insert element at the end of queue if it is not FULL.

So Rear=Rear+1

Dequeue() - Delete element from queue if it is not EMPTY.

So, Front=Front+1

display() – To display elements of the queue.

(7)

Implementation of Queue

Static Implementation

using array

Dynamic Implementation

using Linked List

Note:

When a queue is implemented using array, that queue can organize only limited number of elements. When a queue is implemented using linked list, that queue can organize unlimited number of elements.

(8)

Array Implementation of Queue

Queue, when implemented as an array is functionally same as any other array except that here, adding an element is done in one side and deletion from other side.

Create a Queue using a fixed size array

Maximum size specified is N.

(9)

Types of Queues

Simple queues

Circular queues

Priority queues

Double ended queue(deques)

(10)

Enqueue and Dequeue

Primary queue operations: Enqueue and Dequeue

Enqueue

Insert an element at the rear of the queue

Dequeue

Remove an element from the front of the queue

Insert (Enqueue) Remove

(Dequeue) front rear

(11)

Enqueue:Insertion in a Simple Queue

Empty Queue Front=-1

Rear=-1

[0] [1] [2] [3] [4] [5]

Array Index

Front: index of the front element

Rear: index of the element after inserting element.

We can insert atmost N (Max. size of array) element

(12)

Insertion in a Simple Queue

Front=Rear=0

Queue before insertion Front=-1 Rear=-1

[0] [1] [2] [3] [4] [5]

Array Index

Array Index [0] [1] [2] [3] [4] [5]

(13)

Insertion in a Simple Queue

Front=0 Rear=1

Queue before inserting 2nd item Front Rear

Queue after 2nd item inserted

[0] [1] [2] [3] [4] [5]

[0] [1] [2] [3] [4] [5]

(14)

Insertion in a Simple Queue

Front=0

Rear=2

Queue before 3rd item inserted Front Rear

Queue after 3rd item inserted

[0] [1] [2] [3] [4] [5]

(15)

Insertion in a Simple Queue

Front=0 Rear=3

Queue before 4th item inserted

Front Rear

Queue after 4th item inserted

[0] [1] [2] [3] [4] [5]

(16)

Insertion in a Simple Queue

Front=0 Rear=4

Queue before 5th item inserted

Front Rear

Queue after 5th item inserted

[0] [1] [2] [3] [4] [5]

(17)

Insertion in a Simple Queue

Front Rear=5

Queue before 6th item inserted

Front Rear

Queue after 6th item inserted

Front will always be in left to the Rear

[0] [1] [2] [3] [4] [5]

(18)

Algorithm : Insertion

Input: Here QUEUE is an array with N locations. FRONT and REAR points to the front and rear of the QUEUE. ITEM is the value to be inserted.

1. If (REAR == N-1) Then Print: Overflow

2. Else If (FRONT and REAR == -1) Then

(a) Set FRONT = 0 (b) Set REAR = 0

3. Else

Set REAR = REAR + 1

4. QUEUE[REAR] = ITEM

5. Print: ITEM inserted

6. Exit

(19)

Deletion from a Simple Queue

Front=0 Rear

Queue before Front element deleted

Front=1 Rear

Queue after front item (2) deleted

[0] [1] [2] [3] [4] [5]

(20)

Deletion from a Simple Queue

Front Rear

Queue before front item deleted

Front=2 Rear

Queue after front item (3) deleted

[0] [1] [2] [3] [4] [5]

(21)

Deletion from a Simple Queue

Front Rear

Front=3 Rear

Queue after front item (1) deleted

[0] [1] [2] [3] [4] [5]

Queue before front item deleted

(22)

Deletion from a Simple Queue

Front Rear

Front=4 Rear

Queue after front item (6) deleted

[0] [1] [2] [3] [4] [5]

Queue before front item deleted

(23)

Deletion from a Simple Queue

Front=4 Rear

Front=5 Rear

Queue after front item (4) deleted

[0] [1] [2] [3] [4] [5]

Queue before front item deleted

(24)

Deletion from a Simple Queue

Front Rear

Front Rear

Queue after front item (10) deleted

[0] [1] [2] [3] [4] [5]

Queue before front item deleted

(25)

Algorithm: Deletion

Input: Here QUEUE is an array with N locations. FRONT and REAR points to the front and rear of the QUEUE.

1. If (FRONT == -1) Then Print: Underflow

2. Else

3. ITEM = QUEUE[FRONT]

5. If (FRONT == REAR) Then [Check if only one element is left]

(a) Set FRONT = -1 (b) Set REAR = -1

6. Else

7. Set FRONT = FRONT + 1

8. Print: ITEM deleted

9. Exit

(26)

Linear Queue

Front

Rear =5

Condition for Fullness of Queue:

Rear=-1

Condition for Emptiness of Queue

[0] [1] [2] [3] [4] [5]

[0] [1] [2] [3] [4] [5]

Rear=N-1

: Rear=-1, front=-1

(27)

Limitation of Simple queues

(28)

Limitation: False-Overflow

Suppose 6 calls to enqueue () have been made, so now the queue array is full

Assume 3 calls to dequeue ( ) are made

Assume a call to enqueue ( ) is made now. The Rear part seems to have no space, but the front has 3 unused spaces;

if never used, they are wasted.

Front

If we try to insert,

Overflow occurs

Though some cells are empty

Rear

[0] [1] [2] [3] [4] [5]

(29)

Solution:

Circular Queue

(30)

Circular Queue

A circular queue is one in which the insertion of a new element is done at the very first location of the queue if the last location of the queue is full.

If we have queue Q of N elements, then after inserting an element in last location(i.e N-1), the next will be inserted at the very first location, if it is empty.

Rear

Front

Next Element[0] [1] [2] [3] [4] [5]

(31)

Circular Queue

Front Rear

(32)

Circular Queue

A circular queue overcomes the problem of unutilized space of simple queues.

1. Front will always point to the first element

2. While inserting rear=rear+1

3. While deleting front=front+1

4. When Rear=N-1 then new Rear=Rear+1%maxsize of array (N) ==N%N=0

(33)

Circular Queue

INSERTION

(34)

Enqueue: Circular Queue

[0]

[1]

[2] [3]

[4]

[5]

Front = -1 Rear = -1

(35)

Enqueue:Circular Queue

Front = 0 Rear = 0

[0]

[1]

[2] [3]

[4]

10 [5]

Rear

Front

ENQUEUE 10

(36)

Enqueue: Circular Queue

Front = 0 Rear = 1

[0]

[1]

[2] [3]

[4]

[5]

20

10

Front Rear

ENQUEUE 20

(37)

Enqueue: Circular Queue

Front = 0 Rear = 2

[0]

[1]

[2] [3]

[4]

[5]

30

20

10

Rear

Front

ENQUEUE 30

(38)

Enqueue: Circular Queue

Front = 0 Rear = 3

[0]

[1]

[2] [3]

[4]

[5]

30 40

20

10

Front

ENQUEUE 40

(39)

Enqueue: Circular Queue

Front = 0 Rear = 4

[0]

[1]

[2] [3]

[4]

[5]

30 40

20 50

10

Front

ENQUEUE 50

(40)

Circular Queue

Front = 0 Rear = 5

Queue is FULL Now, No further insertion is possible

[0]

[1]

[2] [3]

[4]

[5]

30 40

20 50

10 60

ENQUEUE 60

Rear

(41)

Algorithm: insertion

Input: Here QUEUE is an array with N locations. FRONT and REAR points to the front and rear elements of the QUEUE. ITEM is the value to be

inserted.

1. If (FRONT == 0 and REAR == N-1) or (FRONT == (REAR + 1) % MAXSIZE ) Then

2. Print: Overflow and exit

3. Else

4. If (REAR == -1) Then (a) Set FRONT = 0 (b) Set REAR = 0

5. Else If (REAR == N-1) Then

6. Set REAR = 0

7. Else

8. Set REAR = REAR + 1

9. Set QUEUE[REAR] = ITEM

10. Print: ITEM inserted

11. Exit

(42)

Circular Queue

DELETION

[0]

[1]

[2] [3]

[4]

[5]

30 40

20 50

10 60

Front=0

Rear=5

(43)

Dequeue: Circular Queue

Front = 1 Rear = 5

Queue After First Dequeue

[0]

[1]

[2] [3]

[4]

[5]

30 40

20 50

60

Front

(44)

Dequeue: Circular Queue

Front = 2 Rear = 5

Queue After 2nd Dequeue

[0]

[1]

[2] [3]

[4]

[5]

30 40

50

60

Front

(45)

Circular Queue

Front = 2 Rear = 0

Insert 70 into Queue [0]

[1]

[2] [3]

[4]

[5]

30 40

50

70 60

Rear=0

Front=2

(46)

Algorithm: Deletion

Input: Here QUEUE is an array with N locations. FRONT and REAR points to the front and rear elements of the QUEUE.

1. If (FRONT == -1) Then

2. Print: Underflow and exit

3. Else

4. ITEM = QUEUE[FRONT]

5. If (FRONT == REAR) Then [If only element is left]

(a) Set FRONT = -1 (b) Set REAR = -1

6. Else If (FRONT == N-1) Then

7. Set FRONT = 0

8. Else

9. Set FRONT = FRONT + 1

10. Print: ITEM deleted

11. Exit

(47)

Priority Queues

(48)

Priority Queues

Each element has been assigned a priority.

Assume Maximum Element has highest priority.

Priority queue is an arrangement of data element that allows the insertion of data element as simple queue but allows to delete data according to their priority value.

The highest Priority element always deleted first from Priority Queue.

Note: in Priority Queue, First-In-First-Out does not apply.

(49)

Priority Queues

Insertion:

Insertion is same as normal Queue.

Ex:

2, 3, 8, 6, 1

2 3 8 6 1

Rear

[0] [1] [2] [3] [4] [5]

(50)

Priority Queues

Deletion:

While deleting, Normal Queue rules are not followed.

Based on the elements priority, following rules are followed:

An element of higher priority is deleted before any element of lower priority.

Two elements with the same priority are deleted according to the order in which they were added to the queue.

(51)

Deletion: Priority Queues

2 3 8 6 1

[0] [1] [2] [3] [4] [5]

[0] [1] [2] [3] [4] [5]

Rear

Delete highest priority element first:==8

(52)

Deletion: Priority Queues

[0] [1] [2] [3] [4] [5]

[0] [1] [2] [3] [4] [5]

Rear

After deletion shift rest of the element towards left element:

(53)

Types of Priority Queues

There are two types of Priority Queue:

Ascending

An ascending priority queue is a collection of items into which items can be inserted arbitrarily and from which only the smallest item can be removed.

Descending:

An descending priority queue is a collection of items into which items can be inserted arbitrarily and from which only the highest item can be removed.

(54)

Double Ended Queue :DEQueue

(55)

Deques

A deque is a double-ended queue.

Insertions and deletions can occur at either end.

Implementation is similar to that for queues.

Deques are not heavily used.

You should know what a deque is, but we

won’t explore them much further.

(56)

Applications of Queues

People waiting in line at a bank/reservation counter.

Queue in Computer Science: Used in time sharing systems

A service and more than one client – arrival rate (of clients) does not match with the service rate.

E.g. Ticket Counters

A producer and consumer operating at different speeds

E.g. Input devices and programs;

Programs and output devices

References

Related documents