what is a queue
play

What is a queue? First-in first-out data structure (FIFO) Queues - PDF document

What is a queue? First-in first-out data structure (FIFO) Queues New objects are placed at rear Removal restricted to front Examples? Queue ADT Operations Example enqueue (o): Insert o at rear of queue enqueue(5)


  1. What is a queue? • First-in first-out data structure (FIFO) Queues • New objects are placed at rear • Removal restricted to front • Examples? Queue ADT Operations Example • enqueue (o): Insert o at rear of queue • enqueue(5) • enqueue(9) – Input: Object; Output: None • enqueue(3) • enqueue(7) • dequeue (): Remove object at front; error if empty • dequeue() • size() – Input: None; Output: Object removed • enqueue(7) • enqueue(3) • size (): Return number of objects in queue – Input: None; Output: Integer • dequeue() • enqueue(5) • isEmpty (): Return a boolean indicating queue empty • front() • dequeue() – Input: None; Output: Boolean • dequeue() • first (): Return object at front without removing; error if empty • dequeue() – Input: None; Output: Object • isEmpty() Queue Interface Underlying Representation int size(); • Array versus Linked List – Pros and cons? bool isEmpty(); • Running time? Object front() throws QueueEmptyException; – size – isEmpty void enqueue(Object obj); – enqueue – dequeue Object dequeue() throws QueueEmptyException; – front 1

  2. Array Implementation Array Implementation 0 1 2 3 4 5 6 … n-1 0 1 2 3 4 5 6 … n-1 enqueue(5) enqueue(5) 5 3 5 3 enqueue(3) enqueue(3) 0 1 2 3 4 5 6 … n-1 0 1 2 3 4 5 6 … n-1 3 dequeue() ? dequeue() ? 0 1 2 3 4 5 6 … n-1 Circular Array Circular Array f r f r 0 1 2 3 4 5 6 … n-1 0 1 2 3 4 5 6 … n-1 • f – stores index of cell which stores first element • How do you add a new element? of queue – insert at array[r] • r – stores index of next available cell in queue – increment r • How do you remove an element? • Initially, f = r = 0 – return array[f] • How do you add a new element? – increment f • How do you remove an element? • What happens when r >= n-1? Circular Array Circular Array r f f r f dequeue 0 1 2 3 4 5 6 … n-1 0 1 2 0 1 2 f r r f • Need to be able to wrap around r =(2+1)%3= 0 enqueue enqueue • Modulo – % 0 1 2 0 1 2 – increment f using (f+1)%n f r enqueue – increment r using (r+1)%n 0 1 2 2

  3. Algorithms Deque • size • Double-ended queue – return (N-f+r) mod N • isEmpty – insertFirst – return (f == r) • front – insertLast – if isEmpty then throw QueueEmptyException – removeFirst – return array[f] • dequeue – removeLast – if isEmpty then throw QueueEmptyException – temp = array[f] – first – f = (f+1)%N – return temp – last • enqueue – if size == N-1 then throw QueueFullException SIZE MUST BE < N-1 – size – array[r] = object – r = (r+1)%N – isEmpty Example Doubly Linked List • insertFirst(3) Object1 Object2 Object3 prev prev prev • insertFirst(5) header trailer next next next • first() • removeFirst() • Algorithms • insertLast(7) – insertFirst • last() • removeFirst() – removeLast • removeLast() 3

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend