Lect 17-18 Zaheer Abbas

1,678 views

Published on

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,678
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
45
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Lect 17-18 Zaheer Abbas

  1. 1. Created By Zaheer Abbas Aghani 2k9-152
  2. 2. Lecture 17-18
  3. 3. Queue (definition) <ul><li>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. </li></ul><ul><li>OR </li></ul><ul><li>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. </li></ul>
  4. 4. <ul><li>A queue is a FIFO structure: First In First Out </li></ul>Queues in Our Life
  5. 5. Queue examples Cont….: <ul><li>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. </li></ul>PRINTER QUEUE PC-4 PC-1 PC-3 PC-5 PC-2 FRONT REAR
  6. 6. Basic operations of queue. <ul><li>The main primitive operations of a queue are known as: </li></ul><ul><li>Enqueue: insert an element in queue </li></ul><ul><li>Dequeue: remove an element from queue. </li></ul><ul><li>Queue Front: Return the first element of queue. </li></ul><ul><li>Queue End: return the last element of queue. </li></ul>
  7. 7. QUEUE OPERATIONS
  8. 8. Queue operation continue… <ul><li>Add OPERATION: </li></ul><ul><li>Add new data element at the rear of queue. </li></ul>Enqueue B A C D B A C D FRONT REAR FRONT REAR 1 2 3 1 2 3 4
  9. 9. Queue operation continue… <ul><li>Delete OPERATION: </li></ul><ul><li>Remove data element at the front of queue. </li></ul>Dequeue A 1 B A C B C FRONT REAR FRONT REAR 1 2 3 2 3
  10. 10. Queue operation continue… <ul><li>Queue Front OPERATION: </li></ul><ul><li>Returns the first element of queue. </li></ul>FRONT A A 1 B A C B C FRONT REAR FRONT REAR 1 2 3 2 3
  11. 11. Queue operation continue… <ul><li>Queue End OPERATION: </li></ul><ul><li>Returns the Last element of queue. </li></ul>END C A 1 B A C B C FRONT REAR FRONT REAR 1 2 3 2 3
  12. 12. Conditions of Queue: <ul><li>Overflow: It may be possible that a condition arises when there is no place for adding the element in queue. This is called overflow. </li></ul><ul><li>Underflow: The second possibility arises when there is no element for deleting from queue. This is called underflow. </li></ul>
  13. 13. Implementation of queue in array or linked list: <ul><li>As stack, Queue is also a collection of same type of element so we can take array or linked list for implementing the queue. </li></ul><ul><li>Suppose we implement queue in array. Since we want to add an item in queue at rear & delete an item in queue at front. </li></ul><ul><li>We take two variables REAR( keep the status of last item in queue) and FRONT (keeps the status of first item of queue). </li></ul><ul><li>Before adding or deleting elements in queue we first check the condition of overflow & underflow. </li></ul>
  14. 14. Algorithm (Add Operation) <ul><li>The following procedure add an item in queue. </li></ul><ul><li>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 </li></ul><ul><li>Step1: (first check condition </li></ul><ul><li> of overflow) </li></ul><ul><li> IF REAR=MAXQUE then </li></ul><ul><li> print overflow & exit. </li></ul><ul><li>Step2: Set REAR=REAR+1 </li></ul><ul><li>Step3: Add Queue[REAR]=ITEM </li></ul><ul><li>Step4: Exit </li></ul>A MAXQUE MAXQUE B 1 2 3 4 FRONT REAR A B C 1 2 3 4 FRONT REAR
  15. 15. Algorithm (Delete Operation) <ul><li>The following procedure delete an item from queue. </li></ul><ul><li>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. </li></ul><ul><li>Step1: (first check conditionof Underflow) If FRONT== 0 or FRONT>REAR </li></ul><ul><li> then print Underflow & exit. </li></ul><ul><li>Step2: Delete Queue[FRONT] </li></ul><ul><li>Step3: set FRONT=FRONT+1 </li></ul><ul><li>Step4: Exit. </li></ul>MAXQUE MAXQUE 5 4 5 A B C 1 2 3 4 FRONT REAR B C 1 2 3 FRONT REAR
  16. 16. TYPES of queue: <ul><li>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. </li></ul>
  17. 17. TYPES of queue cont…: <ul><li>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: </li></ul><ul><li>Input Restricted. </li></ul><ul><li>Output Restricted. </li></ul><ul><li>In Input restricted dequeue, elements can be added at only one end but we can delete the element from both sides. </li></ul><ul><li>In Output restricted dequeue , elements can added from both sides but deletion is allowed only at one end. </li></ul>
  18. 18. Operations example: <ul><li>Consider a queue of size 4 and perform the following operations on it. </li></ul><ul><li>Enqueue A ii) Enqueue B </li></ul><ul><li>iii) Queue Front iv) Dequeue </li></ul><ul><li>v) Add C. vi) Queue Last </li></ul>
  19. 19. Step1: Enqueue A Step2: Enqueue B Step3:Queue-front Step:1 Enqueue A 1 2 3 4 1 2 3 4 FRONT=0 FRONT REAR REAR=0 A A Enqueue B 1 2 3 4 1 2 3 4 FRONT FRONT REAR REAR B A B Step:2 A FRONT 1 2 3 4 1 2 3 4 FRONT FRONT REAR REAR A A B Step:3 A B
  20. 20. Step4: Dequeue Step5: Enqueue C Step6: Queue-Last Dequeue 1 2 3 4 1 2 3 4 FRONT FRONT REAR REAR A B Step:4 A B Enqueue C 1 2 3 4 1 2 3 4 FRONT FRONT REAR REAR C B Step:5 B C LAST 1 2 3 4 1 2 3 4 FRONT FRONT REAR REAR C B Step:6 B C C

×