Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithm and Data Structure Part III Dr. Naimah Yaakob

Similar presentations


Presentation on theme: "Algorithm and Data Structure Part III Dr. Naimah Yaakob"— Presentation transcript:

1

2 Algorithm and Data Structure Part III Dr. Naimah Yaakob
EKT 334 Algorithm and Data Structure Chapter 5 – Queues Part III Dr. Naimah Yaakob

3 Deques A deque has two variants, viz., input restricted deque and output restricted deque. An input restricted deque is one where insertions are allowed at one end only while deletions are allowed at both ends. On the other hand, an output restricted deque allows insertions at both ends of the deque but permits deletions only at one end. A deque is commonly implemented as a circular array with two variables LEFT and RIGHT taking care of the active ends of the deque

4 Example 5.4: Let DEQ[1:6] be a deque implemented as a circular array.
The contents of DEQ and that of LEFT and RIGHT are as given below: Should be here

5 It is easy to observe that for insertion at the left end, LEFT is decremented by 1 (mod n)
and for insertions at the right end, RIGHT is incremented by 1 (mod n). For deletions at the left end, LEFT is incremented by 1 (mod n) and for deletions at the right end, RIGHT is decremented by 1 (mod n) where n is the capacity of the queue. Again, before performing a deletion, if LEFT=RIGHT, then it implies that there is only one element and in such a case after deletion set LEFT=RIGHT=NIL to indicate that the deque is empty

6 Applications : Linear Queue
Application Of A Linear Queue : A Time Sharing System A CPU (processor) endowed with memory resources, is to be shared by n number of computer users. The sharing of the processor and memory resources is done by allotting a definite time slice of the processor’s attention on the users and in a round- robin fashion (time quantum). In a system such as this the users are unaware of the presence of other users and are led to believe that their job receives the undivided attention of the CPU. However, to keep track of the jobs initiated by the users, the processor relies on a queue data structure recording the active user- ids.

7 Example

8 Example of Linear Queue
Arrival Time A B 1 C 2 **Any 2 sequential job has 5µs delay

9 How the CPU handles the queue??
As soon as the request is made by a user, the user ID is inserted into a queue A job that is to processed next would be the one at the head of the queue A job remains at the head of the queue until it completes its execution. Once completed, its user ID is deleted from the queue A snapshot of the queue data structure at t=5,10 and 14 is as follows B

10 Applications : Priority Queue
Assume different kinds of job request: 1. Real time (highest) 2. Online (medium) 3. Batch job (lowest) ** the job scheduler needs to maintain one-queue for Real-time (R), one for Online job (O) and one for batch job (B) **CPU can only executes the job in O when the job in queue R is empty. Queue B can only be executed when queue R and O are emptied.

11 Example of Priority Queue
Arrival Time A B 1 C 2 **Any 2 sequential job has 5µs delay

12 ADT for Queues Data objects: A finite set of elements of the same type Operations: Create an empty queue and initialize front and rear variables of the queue CREATE ( QUEUE, FRONT, REAR) Check if queue QUEUE is empty CHK_QUEUE_EMPTY (QUEUE ) (Boolean function) Check if queue QUEUE is full CHK_QUEUE_FULL (QUEUE) (Boolean function) Insert ITEM into rear of queue QUEUE ENQUEUE (QUEUE, ITEM) Delete element from the front of queue QUEUE and output the element deleted in ITEM DEQUEUE (QUEUE , ITEM)


Download ppt "Algorithm and Data Structure Part III Dr. Naimah Yaakob"

Similar presentations


Ads by Google