Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter Objectives  Learn how to represent a waiting line (queue)  Become proficient using the methods in the Queue  Understand how to implement the.

Similar presentations


Presentation on theme: "Chapter Objectives  Learn how to represent a waiting line (queue)  Become proficient using the methods in the Queue  Understand how to implement the."— Presentation transcript:

1 Chapter Objectives  Learn how to represent a waiting line (queue)  Become proficient using the methods in the Queue  Understand how to implement the Queue interface using  a single-linked list,  a circular array  double-linked list  Analyze the pros and cons for different Queue implementations  Become familiar with the Deque interface its methods  Simulate the operation of a physical system that has one or more waiting lines CS340 1

2 Queue The queue, like the stack, is a widely used data structure A queue differs from a stack in one important way A stack is LIFO list – Last-In, First-Out while a queue is FIFO list, First-In, First-Out CS340 2

3 Queue Abstract Data Type CS340 3

4 Queue Abstract Data Type  Line of customers waiting for service  Similar to a stack  We do not access the “middle” objects  Pointer in the head as in stack  Additional pointer on tail CS340 4

5 More Queues  Operating systems use queues to  keep track of tasks  ensure that the tasks are carried out in the order they were generated  Print queue CS340 5

6 Specification for a Queue Interface  The Queue interface implements the Collection interface CS340 6

7 Class LinkedList Implements the Queue Interface  LinkedList class implements the Queue interface Queue names = new LinkedList (); CS340 7

8 Implementing the Queue Interface CS340 8

9 Using a Double-Linked List to Implement the Queue Interface  Insertion and removal from either end of a double-linked list is ???  Problem: Other LinkedList methods in addition to the ones required and permitted by the Queue interface  Solution: Create a new class with a LinkedList component CS340 9

10 Using a Single-Linked List to Implement a Queue Insertions are at the rear of a queue and removals are from the front We need a reference to the last list node so that insertions can be performed at O(1) The number of elements in the queue is changed by methods insert and remove CS340 10

11 Implementing a Queue Using an Array  Single- or double-linked list is time efficient BUT  Space inefficiencies  Storage space increased due to references stored in the nodes  Array Implementation  Insertion at rear of array is ??  Removal from the front is ??  Removal from rear of array is ??  Insertion at the front is ?? CS340 11

12 [ 0 ] An array of integers to implement a queue of integers 486 We don't care what's in this part of the array. [ 1 ] [ 2 ] [ 3 ] [ 4 ] size 3 first 0 last 2 Implementing a Queue Using an Array CS340 12

13 dequeue() [ 0 ]... 86 [ 1 ] [ 2 ] [ 3 ] [ 4 ] size 3 first 0 last 2 4 [ 0 ] 86 [ 1 ] [ 2 ] [ 3 ] [ 4 ] size 2 first 0 last 1 Implementing a Queue Using an Array CS340 13

14 dequeue() [ 0 ] 86 [ 1 ] [ 2 ] [ 3 ] [ 4 ] size 3 first 0 last 2 4 [ 0 ] 6 [ 1 ] [ 2 ] [ 3 ] [ 4 ] size 2 first 0 last 1 8 Implementing a Queue Using an Array CS340 14 dequeue() takes O(n)!

15 dequeue() size 3 first 0 last 2 [ 0 ] 4 [ 1 ] 8 [ 2 ] 6 [ 3 ] [ 4 ] Solution: Cyclic Array CS340 15

16 size 2 first 1 last 2 [ 0 ] [ 1 ] 8 [ 2 ] 6 [ 3 ] [ 4 ] enqueue(2) dequeue() enqueue(12) enqueue(5 ) Cyclic Array (cont.) CS340 16

17 size 3 first 3 last 0 [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] 2 12 5 Cyclic Array (cont.) CS340 17

18 Implementing a Queue Using a Circular Array (cont.) 18

19 Implementing a Queue Using a Circular Array (cont.) CS340 19

20 Implementing a Queue Using a Circular Array (cont.) CS340 20 size = 0 front = 0 rear = 4 public ArrayQueue(int initCapacity) { capacity = initCapacity; theData = (E[])new Object[capacity]; front = 0; rear = capacity – 1; size = 0; } ArrayQueue q = new ArrayQueue(5); capacity = 5

21 Implementing a Queue Using a Circular Array (cont.) CS340 21 size = 0 front = 0 rear = 4 public boolean offer(E item) { if (size == capacity) { reallocate(); } size++; rear = (rear + 1) % capacity; theData[rear] = item; return true; } q.offer('*'); capacity = 5 1 rear = 0 *

22 Implementing a Queue Using a Circular Array (cont.) CS340 22 size = 1 front = 0 rear = 1 public boolean offer(E item) { if (size == capacity) { reallocate(); } size++; rear = (rear + 1) % capacity; theData[rear] = item; return true; } q.offer('+'); capacity = 5 2 rear = 0 * +

23 Implementing a Queue Using a Circular Array (cont.) CS340 23 size = 2 front = 0 rear = 1 public boolean offer(E item) { if (size == capacity) { reallocate(); } size++; rear = (rear + 1) % capacity; theData[rear] = item; return true; } q.offer('/'); capacity = 5 3 * + rear = 2 /

24 Implementing a Queue Using a Circular Array (cont.) CS340 24 size = 3 front = 0 rear = 3 public boolean offer(E item) { if (size == capacity) { reallocate(); } size++; rear = (rear + 1) % capacity; theData[rear] = item; return true; } q.offer('-'); capacity = 5 4 * + rear = 2 / -

25 Implementing a Queue Using a Circular Array (cont.) CS340 25 size = 4 front = 0 rear = 4 public boolean offer(E item) { if (size == capacity) { reallocate(); } size++; rear = (rear + 1) % capacity; theData[rear] = item; return true; } q.offer('A'); capacity = 5 5 * + rear = 3 / A -

26 CS340 26 size = 5 front = 0 public E poll() { if (size == 0) { return null } E result = theData[front]; front = (front + 1) % capacity; size--; return result; } next = q.poll(); capacity = 5 4 * + / - result = '*' front = 1 A rear = 4 Implementing a Queue Using a Circular Array (cont.)

27 CS340 27 size = 4 front = 1 public E poll() { if (size == 0) { return null } E result = theData[front]; front = (front + 1) % capacity; size--; return result; } next = q.poll(); capacity = 5 3 * + / - result = '+' front = 2 A rear = 4 Implementing a Queue Using a Circular Array (cont.)

28 CS340 28 size = 3 public boolean offer(E item) { if (size == capacity) { reallocate(); } size++; rear = (rear + 1) % capacity; theData[rear] = item; return true; } q.offer('B'); capacity = 5 4 * + / - front = 2 A rear = 4 rear = 0 B Implementing a Queue Using a Circular Array (cont.)

29 CS340 29 size = 4 public boolean offer(E item) { if (size == capacity) { reallocate(); } size++; rear = (rear + 1) % capacity; theData[rear] = item; return true; } q.offer('C'); capacity = 5 5 B + / - front = 2 A rear = 0 rear = 1 C Implementing a Queue Using a Circular Array (cont.)

30 CS340 30 size = 5 public boolean offer(E item) { if (size == capacity) { reallocate(); } size++; rear = (rear + 1) % capacity; theData[rear] = item; return true; } q.offer('D'); capacity = 5 B + / - front = 2 A rear = 1 C Implementing a Queue Using a Circular Array (cont.)

31 CS340 31 size = 5 private void reallocate() { int newCapacity = 2 * capacity; E[] newData = (E[])new Object[newCapacity]; int j = front; for (int i = 0; i < size; i++) { newData[i] = theData[j]; j = (j + 1) % capacity; } front = 0; rear = size – 1; capacity = newCapacity; theData = newData; } q.offer('D'); capacity = 5 B + / - front = 2 A rear = 1 C B + / - front = 2 A rear = 1 C newCapacity = 10 theData Implementing a Queue Using a Circular Array (cont.)

32 CS340 32 Implementing a Queue Using a Circular Array (cont.) size = 5 private void reallocate() { int newCapacity = 2 * capacity; E[] newData = (E[])new Object[newCapacity]; int j = front; for (int i = 0; i < size; i++) { newData[i] = theData[j]; j = (j + 1) % capacity; } front = 0; rear = size – 1; capacity = newCapacity; theData = newData; } q.offer('D'); capacity = 5 B + / - front = 2 A rear = 1 C newCapacity = 10 j = 2 i = 0 newData theData

33 CS340 33 Implementing a Queue Using a Circular Array (cont.) size = 5 private void reallocate() { int newCapacity = 2 * capacity; E[] newData = (E[])new Object[newCapacity]; int j = front; for (int i = 0; i < size; i++) { newData[i] = theData[j]; j = (j + 1) % capacity; } front = 0; rear = size – 1; capacity = newCapacity; theData = newData; } q.offer('D'); capacity = 5 B + / - front = 2 A rear = 1 C newCapacity = 10 j = 2 i = 0 / / j = 3 i = 1 newData theData

34 CS340 34 Implementing a Queue Using a Circular Array (cont.) size = 5 private void reallocate() { int newCapacity = 2 * capacity; E[] newData = (E[])new Object[newCapacity]; int j = front; for (int i = 0; i < size; i++) { newData[i] = theData[j]; j = (j + 1) % capacity; } front = 0; rear = size – 1; capacity = newCapacity; theData = newData; } q.offer('D'); capacity = 5 B + / - front = 2 A rear = 1 C newCapacity = 10 j = 3 i = 1 - - j = 4 i = 2 / newData theData

35 CS340 35 Implementing a Queue Using a Circular Array (cont.) size = 5 private void reallocate() { int newCapacity = 2 * capacity; E[] newData = (E[])new Object[newCapacity]; int j = front; for (int i = 0; i < size; i++) { newData[i] = theData[j]; j = (j + 1) % capacity; } front = 0; rear = size – 1; capacity = newCapacity; theData = newData; } q.offer('D'); capacity = 5 B + / - front = 2 A rear = 1 C newCapacity = 10 j = 0 i = 2 A A j = 4 i = 3 / - newData theData

36 CS340 36 Implementing a Queue Using a Circular Array (cont.) size = 5 private void reallocate() { int newCapacity = 2 * capacity; E[] newData = (E[])new Object[newCapacity]; int j = front; for (int i = 0; i < size; i++) { newData[i] = theData[j]; j = (j + 1) % capacity; } front = 0; rear = size – 1; capacity = newCapacity; theData = newData; } q.offer('D'); capacity = 5 B + / - front = 2 A rear = 1 C newCapacity = 10 j = 1 i = 3 B B j = 0 i = 4 / - A newData theData

37 CS340 37 Implementing a Queue Using a Circular Array (cont.) size = 5 private void reallocate() { int newCapacity = 2 * capacity; E[] newData = (E[])new Object[newCapacity]; int j = front; for (int i = 0; i < size; i++) { newData[i] = theData[j]; j = (j + 1) % capacity; } front = 0; rear = size – 1; capacity = newCapacity; theData = newData; } q.offer('D'); capacity = 5 B + / - front = 2 A rear = 1 C newCapacity = 10 j = 2 i = 4 C C j = 1 i = 5 / - A B newData theData

38 newData CS340 38 Implementing a Queue Using a Circular Array (cont.) size = 5 private void reallocate() { int newCapacity = 2 * capacity; E[] newData = (E[])new Object[newCapacity]; int j = front; for (int i = 0; i < size; i++) { newData[i] = theData[j]; j = (j + 1) % capacity; } front = 0; rear = size – 1; capacity = newCapacity; theData = newData; } q.offer('D'); capacity = 5 front = 2 rear = 1 newCapacity = 10 C i = 5 / - A B B + / - A C j = 2 C theData front = 0 rear = 4 10

39 CS340 39 Implementing a Queue Using a Circular Array (cont.) size = 5 q.offer('D'); capacity = 5 C / - A B newData front = 0 rear = 4 10 public boolean offer(E item) { if (size == capacity) { reallocate(); } size++; rear = (rear + 1) % capacity; theData[rear] = item; return true; } 6 rear = 5 D

40 Implementing Class ArrayQueue.Iter (cont.) private class Iter implements Iterator { private int index; private int count = 0; public Iter() { index = front; } @Override public boolean hasNext() { return count < size; }.... CS340 40 Just as for class ListQueue, we must implement the missing: Queue methods class Iter

41 Implementing Class ArrayQueue.Iter (cont.) private class Iter implements Iterator { private int index; private int count = 0; public Iter() { index = front; } @Override public boolean hasNext() { return count < size; }.... CS340 41 Just as for class ListQueue, we must implement the missing Queue methods and an inner class Iter to fully implement the Queue interface index stores the subscript of the next element to be accessed

42 Implementing Class ArrayQueue.Iter (cont.) private class Iter implements Iterator { private int index; private int count = 0; public Iter() { index = front; } @Override public boolean hasNext() { return count < size; }.... CS340 42 Just as for class ListQueue, we must implement the missing Queue methods and an inner class Iter to fully implement the Queue interface The constructor initializes index to front when a new Iter object is created

43 Implementing Class ArrayQueue.Iter (cont.) private class Iter implements Iterator { private int index; private int count = 0; public Iter() { index = front; } @Override public boolean hasNext() { return count < size; }.... CS340 43 Just as for class ListQueue, we must implement the missing Queue methods and an inner class Iter to fully implement the Queue interface count keeps track of the number of items accessed so far

44 Implementing Class ArrayQueue.Iter (cont.) private class Iter implements Iterator { private int index; private int count = 0; public Iter() { index = front; } @Override public boolean hasNext() { return count < size; }.... CS340 44 Just as for class ListQueue, we must implement the missing Queue methods and an inner class Iter to fully implement the Queue interface hasNext() returns true if count is less than size

45 Implementing Class ArrayQueue.Iter (cont.) @Override public E next() { if (!hasNext()) { throw new NoSuchElementException(); } E returnValue = theData[index]; index = (index + 1) % capacity; count+; return returnValue; } @Override public void remove { throw new UnsupportedOperationException(); } CS340 45 Just as for class ListQueue, we must implement the missing Queue methods and an inner class Iter to fully implement the Queue interface next() returns the element at position index and increments Iter 's fields index and count

46 Implementing Class ArrayQueue.Iter (cont.) @Override public E next() { if (!hasNext()) { throw new NoSuchElementException(); } E returnValue = theData[index]; index = (index + 1) % capacity; count+; return returnValue; } @Override public void remove { throw new UnsupportedOperationException(); } CS340 46 Just as for class ListQueue, we must implement the missing Queue methods and an inner class Iter to fully implement the Queue interface remove() throws an exception because removing an item other than the first item violates the queue's contract

47 Comparing the Three Implementations Computation time Comparable in terms of computation time All operations are O(1) Although reallocating an array is O(n), its is amortized over n items, so the cost per item is O(1) CS340 47

48 Comparing the Three Implementations (cont.)  Storage  Linked-list implementations require more storage due to the extra space required for the links  A double-linked list requires 1.5 times the storage of a single- linked list  A circular array requires half the storage of a single-linked list to store the same number of elements  But a recently reallocated circular array is half empty CS340 48

49 The Deque Interface Section 4.4 CS340 49

50 Deque Interface Deque: double-ended queue Allows insertions and removals from both ends The Java Collections Framework provides two implementations of the Deque interface ArrayDeque LinkedList CS340 50

51 Deque Example CS340 51

52 Deque Interface (cont.) The Deque interface can be used as a Queue Stack 52

53 Simulating Waiting Lines Using Queues CS340 53

54 Simulating Waiting Lines Using Queues  Simulation is used to study the performance of a physical system by using  a physical,  mathematical, or  computer model of the system CS340 54

55 Simulating Waiting Lines Using Queues (cont.) A branch of mathematics called queuing theory studies such problems CS340 55

56 Simulating Google webserver You are working for google and need to estimate how many servers you need for the new data center The servers receive requests with an exponential arrival process with average 1,000 requests/sec The servers serve with average rate 1,800 requests /sec You need to provision for peak and low times. During peak the rate increases to 1,700 req/seq for duration1 hr Overnight the server falls to 500 rer/sec for duration 6 hrs Simulate 1000 requests on the server. CS340 56

57 Simulating Google webserver Find: The average waiting time of a request To decrease the waiting to half time, would it be better to use two servers or double the rate of the e- commerce server to 2x? How many servers do you need to have 0 sec delay? CS340 57


Download ppt "Chapter Objectives  Learn how to represent a waiting line (queue)  Become proficient using the methods in the Queue  Understand how to implement the."

Similar presentations


Ads by Google