1 / 11

Array-based Implementation

Array-based Implementation. An array Q of maximum size N Need to keep track the front and rear of the queue: f : index of the front object r : index immediately past the rear element Note: Q [ r ] is empty (does not store any object). Array-based Implementation. Front element: Q [ f ]

hwest
Download Presentation

Array-based Implementation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Array-based Implementation • An array Q of maximum size N • Need to keep track the front and rear of the queue: f: index of the front object r: index immediately past the rear element • Note: Q[r] is empty (does not store any object)

  2. Array-based Implementation • Front element: Q[f] • Rear element: Q[r – 1] • Queue is empty: f = r • Queue size: r – f

  3. Algorithm dequeue(): if (isEmpty()) throw QueueEmptyException; temp = Q[f]; f = f + 1; return temp; Algorithm enqueue(object): if (r == N) throw QueueFullException; Q[r] = object; r = r + 1; Dequeue() and Enqueue()

  4. Analogy: A snake chases its tail Front element: Q[f] Rear element: Q[r – 1] Incrementing f, r f = (f + 1) mod N r = (r + 1) mod N mod: Java operator “%” Circular Array Implementation

  5. Queue size = (N – f + r) mod N → verify this Queue is empty: f = r When r reaches and overlaps with f, the queue is full: r = f To distinguish between empty and full states, we impose a constraint: Q can hold at most N – 1 objects (one cell is wasted). So r never overlaps with f, except when the queue is empty. Circular Array Implementation

  6. Algorithm enqueue(object): if (size() == N – 1) throw QueueFullException; Q[r] = object; r = (r + 1) mod N; Algorithm dequeue(): if (isEmpty()) throw QueueEmptyException; temp = Q[f]; f = (f + 1) mod N; return temp; Pseudo-code

  7. Algorithm front(): if (isEmpty()) throw QueueEmptyException; return Q[f]; Algorithm isEmpty(): return (f = r); Algorithm size(): return ((N – f + r) mod N); Homework: Remove the constraint “Q can hold at most N – 1 objects”. That is, Q can store up to N objects. Implement the Queue ADT using a circular array. Note: there is no corresponding built-in Java class for queue ADT Pseudo-code

  8. Double-Ended Queue ADT Deque (pronounced “deck”) Allows insertion and deletion at both the front and the rear of the queue Deque ADT: operations addFirst(e): insert e at the beginning of the deque addLast(e): insert e at the end of the deque removeFirst(): remove and return the first element removeLast(): remove and return the last element getFirst(): return the first element getLast(): return the last element isEmpty(): return true if deque is empty; false otherwise size(): return the number of objects in the deque 8

  9. Implementation Choices Array (homework) Singly linked list: removing at the tail costs θ(n) Doubly linked list 9

  10. removeLast() and addLast() 10

  11. Implementing Stacks and Queues with Deques 11

More Related