Queue

1,171 views

Published on

Queue

  1. 1. Q U E U E A lecture by ABDUL GHAFFAR Based on Chapter 3 Of Reference Book #1
  2. 2. Reference Books <ul><ul><ul><li>Data Structures and Algorithm Analysis in C </li></ul></ul></ul><ul><ul><ul><ul><li>By Mark Allen Weiss </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>Published by Addison Wesley </li></ul></ul></ul></ul></ul><ul><ul><ul><li>Data Structures (Schaum’s Outline Series) </li></ul></ul></ul><ul><ul><ul><ul><li>By Seymour Lipschutz </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>Published by Mc Graw Hill </li></ul></ul></ul></ul></ul>
  3. 3. A QUEUE MODEL <ul><li>Definitions: </li></ul><ul><ul><li>A QUEUE is a list with the restriction that insertion is done at one end , where as deletion at the other and.for that reason it is called FIFO ( First in first out ) structure. </li></ul></ul><ul><ul><li>The end where insertion is performed is called the rear . </li></ul></ul><ul><ul><li>The end where Deletion is performed is called the front . </li></ul></ul><ul><ul><li>The number of elements in a queue is called the size of the queue </li></ul></ul><ul><li>Set of operations: </li></ul><ul><ul><li>Enqueue : Inserts an element at the end (rear) of the list </li></ul></ul><ul><ul><li>Dequeue : Returns and deletes the element at the start (front) of the list. </li></ul></ul>QUEUE Dequeue Enqueue
  4. 4. Implementation <ul><ul><li>As Array </li></ul></ul><ul><ul><ul><li>Define an Array ‘ Queue ’ of sufficient capacity to store Queue elements. </li></ul></ul></ul><ul><ul><ul><li>Uses a variable i.e rear to remember the position of insertion end. </li></ul></ul></ul><ul><ul><ul><li>Uses a variable i.e front to remember the position of deletion end. </li></ul></ul></ul><ul><ul><ul><li>Uses a variable i.e size to remember the size of the array. </li></ul></ul></ul><ul><ul><ul><li>Write a function Enqueue which takes an element x as argument and performs </li></ul></ul></ul><ul><ul><ul><ul><li>size = size +1 </li></ul></ul></ul></ul><ul><ul><ul><ul><li>rear = rear +1 </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Queue[rear] = x </li></ul></ul></ul></ul><ul><ul><ul><li>Write a function Dequeue, which returns the value at front as </li></ul></ul></ul><ul><ul><ul><ul><li>return Queue[front] </li></ul></ul></ul></ul><ul><ul><ul><ul><li>front=front+1 </li></ul></ul></ul></ul><ul><ul><ul><ul><li>size=size-1 </li></ul></ul></ul></ul>
  5. 5. Example: 4 2 R F Size = 2 Initial 4 2 1 F Size = 3 R After Enqueue(1) 4 2 3 1 F Size = 4 R After Enqueue(3) 4 2 3 1 F Size = 3 R After Dequeue r, 2 is returned 4 2 3 1 Size = 2 R F After Dequeue, 4 is returned 4 2 3 1 Size = 1 RF After Dequeue, 1 is returned 4 2 3 1 Size = 0 F R After Dequeue, 3 is returned

×