Presentation is loading. Please wait.

Presentation is loading. Please wait.

Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,

Similar presentations


Presentation on theme: "Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,"— Presentation transcript:

1 queue

2

3 Avoid confusion

4 Britain Italy

5

6 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g., printer) –Multiprogramming –Simulations Indirect applications –Auxiliary data type for algorithms –Component of other data types ADS Lecture 11

7 7 The Queue ADT (GoTa §5.2) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme Insertions are at the rear of the queue and removals are at the front of the queue Main queue operations: –enqueue(object): inserts an element at the end of the queue –object dequeue(): removes and returns the element at the front of the queue Auxiliary queue operations: –object front(): returns the element at the front without removing it –integer size(): returns the number of elements stored –boolean isEmpty(): indicates whether no elements are stored Exceptions –Attempting the execution of dequeue on front of an empty queue throws an EmptyQueueException ADS Lecture 11

8

9 9 /** * Inspects the element at the front of the queue * @return element at the front of the queue * @exception EmptyQueueException if the queue is empty */ public E front() throws EmptyQueueException; /** * Inserts an element at the rear of the queue * @param element new element to be inserted */ public void enqueue (E element); /** * Removes the element at the front of the queue * @return element removed * @exception EmptyQueueException if the queue is empty */ public E dequeue() throws EmptyQueueException; } Queue interface in Java contd.

10 10 Queue Interface in Java Java interface corresponding to our Queue ADT Requires the definition of class EmptyQueueException No corresponding built-in Java class ADS Lecture 11 public interface Queue { /** * Returns the number of elements in the queue * @return number of elements in the queue */ public int size(); /** * Returns whether the queue is empty * @return true if queue is empty, false otherwise */ public boolean isEmpty();

11 A naïve implementation We could have a one dimensional array Q An integer pointing to the front of the queue and integer pointing to the end of the queue Q.enqueue(x) would do this increment end Q[end] = x Need also Q.peek(), Q.size(), Q.isEmpty(), Q.isFull() Q.dequeue() would do this discuss!!!

12 A naïve implementation We could have a one dimensional array Q Q.dequeue() would do this discuss!!! result = Q[front]; for (int i=front+1;i<=end;i++) Q[i-1] = Q[i]; end--; Obviously with tests for empty queue etc. What is the complexity of dequeue?

13 ADS Lecture 1113 COMPLEXITY Q.peek() O(1) Q.dequeue() O(n) Q.enqueue(x) O(1) Q.isEmpty() O(1)

14 ADS Lecture 1114 Can use a restricted/disciplined linked list

15 ADS Lecture 1115

16 ADS Lecture 1116

17 ADS Lecture 1117

18 ADS Lecture 1118

19 ADS Lecture 1119

20 ADS Lecture 1120

21 ADS Lecture 1121

22 ADS Lecture 1122

23 ADS Lecture 1123

24 ADS Lecture 1124

25 ADS Lecture 1125

26 ADS Lecture 1126

27 ADS Lecture 1127

28 ADS Lecture 1128

29 ADS Lecture 1129

30 ADS Lecture 1130

31 ADS Lecture 1131

32 ADS Lecture 1132

33 ADS Lecture 1133

34 ADS Lecture 1134

35 ADS Lecture 1135

36 ADS Lecture 1136

37 ADS Lecture 1137

38 ADS Lecture 1138

39 ADS Lecture 1139 COMPLEXITY Q.peek() O(1) Q.dequeue() O(1) Q.enqueue(x) O(1) Q.isEmpty() O(1)

40 Array-based Queue Use an array of size N in a circular fashion Three variables keep track of the front, rear, and size f index of the front element r index immediately past the rear element, where we add new elements (enqueue) size is number of entries in the queue

41 41 Array-based Queue Q 012rf normal configuration Q 012fr wrapped-around configuration ADS Lecture 11 Use an array of size N in a circular fashion Three variables keep track of the front, rear, and size f index of the front element r index immediately past the rear element, where we add new elements (enqueue) size is number of entries in the queue

42 42 Queue Operations We use the modulo operator (remainder of division) Algorithm size() return size Algorithm isEmpty() return size == 0 Q 012rf Q 012fr ADS Lecture 11 Operation enqueue throws an exception if the array is full This exception is implementation-dependent Algorithm enqueue(o) if size() = N then throw FullQueueException else Q[r]  o r  (r + 1) mod N update size! size++

43 43 Queue Operations (cont.) Operation dequeue throws an exception if the queue is empty This exception is specified in the queue ADT Algorithm dequeue() if isEmpty() then throw EmptyQueueException else o  Q[f] f  (f + 1) mod N return o ADS Lecture 11 Pros and cons of array based implementation: Again: quick and easy, all methods run in constant time But again, need good idea of capacity a priori update size! size--

44 44 Application: Round Robin Schedulers We can implement a round robin scheduler using a queue, Q, by repeatedly performing the following steps: 1. e = Q.dequeue() 2. Service element e 3. Q.enqueue(e) The Queue Shared Service 1.Dequeue the next element 3.Enqueue the serviced element 2.Service the next element ADS Lecture 11

45 Double-Ended queue (deque) ADS Lecture 1145 Supports insertion & deletion at the front and rear of the queue. Pronounced “deck” Since deque requires insertion & removal at both ends of list, using singly linked list would be inefficient Could use a doubly linked list See GoTa p. 213 Fundamental methods are: addFirst(e) addLast(e) removeFirst() removeLast()

46 Your mission See assessed exercise 2

47


Download ppt "Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,"

Similar presentations


Ads by Google