20
Created By Zaheer Abbas Aghani 2k9-152

Lect 17-18 Zaheer Abbas

Embed Size (px)

Citation preview

Page 1: Lect 17-18 Zaheer Abbas

Created By Zaheer Abbas Aghani 2k9-152

Page 2: Lect 17-18 Zaheer Abbas

Lecture 17-18

Page 3: Lect 17-18 Zaheer Abbas

Queue (definition) Queue is an ordered list of elements in which

we can add elements only at one end, called the rear of the queue and delete elements only at the other end called the front of queue.

OR

A queue is also define as a linear list of elements in which insertion takes place only at one end called rear and deletion takes place at other end called front of queue.

Page 4: Lect 17-18 Zaheer Abbas

4

• A queue is a FIFO structure: First In First Out

Queues in Our Life

Page 5: Lect 17-18 Zaheer Abbas

Queue examples Cont….: In computer implementation, there is

an example of accessing the printer in multi user environment. If printer is in process and more than one user wants to access the printer then it maintain the queue for requesting user and serve as first in first out manner, means gives the access permission to the user which comes first in the queue.

PRINTER QUEUE

PC-4 PC-1 PC-3 PC-5 PC-2

FRONT REAR

Page 6: Lect 17-18 Zaheer Abbas

Basic operations of queue.

The main primitive operations of a queue are known as:

Enqueue: insert an element in queue Dequeue: remove an element from

queue. Queue Front: Return the first

element of queue. Queue End: return the last element

of queue.

Page 7: Lect 17-18 Zaheer Abbas

QUEUE OPERATIONS

Page 8: Lect 17-18 Zaheer Abbas

Queue operation continue…

Add OPERATION: Add new data element at the rear of queue.

BA C

D

BA C D

FRONT

REARFRONT

REAR

1 2 3 1 2 3 4

Enqueue

Page 9: Lect 17-18 Zaheer Abbas

Queue operation continue…

Delete OPERATION: Remove data element at the front of queue.

BA C B C

FRONT

REARFRONT

REAR

1 2 3 2 3

Dequeue

A

1

Page 10: Lect 17-18 Zaheer Abbas

Queue operation continue…

Queue Front OPERATION: Returns the first element of queue.

BA C B C

FRONT

REAR

FRONT

REAR

1 2 3 2 3

FRONT

A

A

1

Page 11: Lect 17-18 Zaheer Abbas

Queue operation continue…

Queue End OPERATION: Returns the Last element of queue.

BA C B C

FRONT

REAR

FRONT

REAR

1 2 3 2 3

END

C

A

1

Page 12: Lect 17-18 Zaheer Abbas

Conditions of Queue:

Overflow: It may be possible that a condition arises when there is no place for adding the element in queue. This is called overflow.

Underflow: The second possibility arises when there is no element for deleting from queue. This is called underflow.

Page 13: Lect 17-18 Zaheer Abbas

Implementation of queue in array or linked list: As stack, Queue is also a collection of same

type of element so we can take array or linked list for implementing the queue.

Suppose we implement queue in array. Since we want to add an item in queue at rear & delete an item in queue at front.

We take two variables REAR( keep the status of last item in queue) and FRONT (keeps the status of first item of queue).

Before adding or deleting elements in queue we first check the condition of overflow & underflow.

Page 14: Lect 17-18 Zaheer Abbas

Algorithm (Add Operation) The following procedure add

an item in queue. Here we consider two

variables REAR & FRONT to keeps the status of last and first element of queue. The third variable MAXQUE indicates the max number of elements store in queue. Let suppose queue is of size 4 therefore MAXQUE=4

Step1: (first check condition of overflow) IF REAR=MAXQUE

then print overflow & exit.

Step2: Set REAR=REAR+1 Step3: Add

Queue[REAR]=ITEM Step4: Exit

B

1 2 3 4

FRONT REAR

A

A B C

1 2 3 4

FRONT REAR

MAXQUE

MAXQUE

Page 15: Lect 17-18 Zaheer Abbas

Algorithm (Delete Operation) The following procedure delete an

item from queue. Again consider two variables REAR

& FRONT to keeps the status of last and first element of queue & third variable MAXQUE indicates the max number of elements store in queue.

Step1: (first check conditionof Underflow) If FRONT== 0 or FRONT>REAR

then print Underflow & exit.

Step2: Delete Queue[FRONT] Step3: set FRONT=FRONT+1 Step4: Exit.

A B C

1 2 3 4

FRONT REAR

B C

1 2 3

FRONT REAR

MAXQUE

MAXQUE

5

4 5

Page 16: Lect 17-18 Zaheer Abbas

TYPES of queue:

Priority Queue: As define earlier, queue is an ordered list of elements in which we can add the elements only at one end called the rear of queue & delete element at the other end called the front of queue. But in priority queue every element of queue has some priority and based on that priority it will be processed. So the element of more priority will be processed before the element which has less priority.

Page 17: Lect 17-18 Zaheer Abbas

TYPES of queue cont…: Dequeue: Dequeue is also called Double

ended queue, as the name implies we can add or delete the elements from both sides. Dequeue can be of two types:

Input Restricted. Output Restricted.

In Input restricted dequeue, elements can be added at only one end but we can delete the element from both sides.

In Output restricted dequeue, elements can added from both sides but deletion is allowed only at one end.

Page 18: Lect 17-18 Zaheer Abbas

Operations example:

Consider a queue of size 4 and perform the following operations on it.

i) Enqueue A ii) Enqueue Biii) Queue Front iv) Dequeuev) Add C. vi) Queue Last

Page 19: Lect 17-18 Zaheer Abbas

Step1: Enqueue A Step2: Enqueue B Step3:Queue-front

Enqueue A

1 2 3 4 1 2 3 4

FRONT=0 FRONTREARREAR=0

A

A

Step:1

Enqueue B

1 2 3 4 1 2 3 4

FRONT FRONT REARREAR

B

A B

Step:2

A

FRONT

1 2 3 4 1 2 3 4

FRONT FRONT REARREAR

A

A B

Step:3

A B

Page 20: Lect 17-18 Zaheer Abbas

Dequeue

1 2 3 4 1 2 3 4

FRONT

FRONT

REARREAR

A

B

Step:4

A B

Enqueue C

1 2 3 4 1 2 3 4

FRONTFRONT REARREAR

C

B

Step:5

B C

LAST

1 2 3 4 1 2 3 4

FRONT FRONT REARREAR

C

B

Step:6

B CC

Step4: Dequeue Step5: Enqueue C Step6: Queue-Last