Download presentation

Presentation is loading. Please wait.

Published byJosiah Kirkby Modified over 2 years ago

1
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
2 Array-based Implementation Front element: Q[f] Rear element: Q[r – 1] Queue is empty: f = r Queue size: r – f

3
3 Dequeue() and Enqueue() 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;

4
4 Circular Array Implementation 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 “%”

5
5 Circular Array Implementation 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.

6
6 Pseudo-code 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;

7
7 Pseudo-code 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

8
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

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

10
10 removeLast() and addLast()

11
11 Implementing Stacks and Queues with Deques

Similar presentations

OK

Queue. The Queue ADT Insertions and deletions follow the first-in first-out scheme Insertions are at the rear of the queue and removals are at the front.

Queue. The Queue ADT Insertions and deletions follow the first-in first-out scheme Insertions are at the rear of the queue and removals are at the front.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Resource based view ppt on mac Ppt on plants for grade 4 Ppt on 3d tv without glasses Ppt on leverages meaning Ppt on data handling for class 11 Ppt on national education policy 1986 world Ppt on water conservation and management Ppt on contact dermatitis Ppt on punjabi culture in punjabi language Ppt on natural resources and conservation degree