Queues 15-121 Fall 2020 Margaret Reid-Miller Logistics - - PowerPoint PPT Presentation

queues
SMART_READER_LITE
LIVE PREVIEW

Queues 15-121 Fall 2020 Margaret Reid-Miller Logistics - - PowerPoint PPT Presentation

Queues 15-121 Fall 2020 Margaret Reid-Miller Logistics Midsemester Grades: 37% A 30% B 20% C 13% < C (mostly poor homework grades) Exam 2: Thursday Nov 5? Tuesday Nov 9? Fall 2020 15-121 (Reid-Miller) 3 Today


slide-1
SLIDE 1

Queues

15-121 Fall 2020 Margaret Reid-Miller

slide-2
SLIDE 2

Logistics

Midsemester Grades:

  • 37% A
  • 30% B
  • 20% C
  • 13% < C (mostly poor homework grades)

Exam 2:

  • Thursday Nov 5?
  • Tuesday Nov 9?

Fall 2020 15-121 (Reid-Miller) 3

slide-3
SLIDE 3

Today

Today:

  • Stack applications
  • Queues
  • ListQueue class
  • Exercise: Reverse queue values
  • Circular ArrayQueue

Fall 2020 15-121 (Reid-Miller) 4

slide-4
SLIDE 4

Queues

Fall 2020 15-121 (Reid-Miller) 5

slide-5
SLIDE 5

Queue ADT

“last in last out” Operations:

  • ENQUEUE – adds an element to the BACK of the

queue

  • DEQUEUE – removes the element from the FRONT
  • f the queue
  • PEEK – returns the element in the front of the queue

without removing it (optional?)

  • Test if the queue is EMPTY

Fall 2020 15-121 (Reid-Miller) 6

slide-6
SLIDE 6

Queue

Uses:

  • A store checkout line
  • A playlist of songs on a jukebox, mp3 player, etc.
  • Putting a new bottle of milk behind the old one.

Fall 2020 15-121 (Reid-Miller) 7

slide-7
SLIDE 7

Queue Operations

public interface FIFOQueue<E> { void enqueue(E obj); // at back E dequeue(); // from front E peek(); // at front boolean isEmpty(); } Ideally, each of these operations should be O(1) time.

Fall 2020 15-121 (Reid-Miller) 8

slide-8
SLIDE 8

Queue Implementations

Want O(1) for all operations, so what underlying data structure can we use?

  • ArrayList?

No, one of the enqueue or dequeue must be O(n)

  • array?

Yes, surprisingly.

  • linked list?

Singly? Singly with reference to the tail?

Fall 2020 15-121 (Reid-Miller) 9

Doubly? No Yes Yes

slide-9
SLIDE 9

Linked List Queue

Fall 2020 15-121 (Reid-Miller) 10

slide-10
SLIDE 10

Queues using a singly-linked list

  • Store the elements of the queue from front to back in
  • rder in the list.
  • Why do we need an additional reference to the tail?

null front back

Fall 2020 15-121 (Reid-Miller) 11

slide-11
SLIDE 11

Linked List Implementation

Fields

public class ListQueue<E> implements FIFOQueue<E> { private Node front; private Node back; // methods (next slides) }

references to nodes with front and back queue elements in list

Fall 2020 15-121 (Reid-Miller) 12

slide-12
SLIDE 12

Linked List Implementation

Constructor & isEmpty

public ListQueue() { front = null; back = null; } public boolean isEmpty() { return (front == null); }

indicates an empty queue

Fall 2020 15-121 (Reid-Miller) 13

slide-13
SLIDE 13

Linked List Implementation

enqueue

public void enqueue(E obj) { Node newNode = new Node(obj); if (back != null) { back.next = newNode; back = newNode; } else { _________________ _________________ } } null

newNode

null front

❶ ❷

back back = newNode; front = newNode;

❶ ❷

Fall 2020 15-121 (Reid-Miller) 14

slide-14
SLIDE 14

Linked List Implementation

dequeue

public E dequeue() { if (front == null) throw new NoSuchElementException(); E element = front.data; front = front.next; ______________________________ return element; }

data

null front

❶ ❷ ❶

element data back

if (front == null) back = null;

Fall 2020 15-121 (Reid-Miller) 15

slide-15
SLIDE 15

Linked List Implementation

peek

public E peek() { if (front == null) throw new NoSuchElementException(); return front.data; }

Fall 2020 15-121 (Reid-Miller) 16

slide-16
SLIDE 16

Exercise: Reverse

// reverses the queue objects using a stack public static void reverse(FIFOQueue<String> q){

15-121 (Reid-Miller) 17 Fall 2020

slide-17
SLIDE 17

Reverse

// reverses the queue objects using a stack public static void reverse(FIFOQueue<String> q){ LIFOStack<string> s = new ArrayStack<String>(); while (!q.isEmpty()) s.push(q.dequeue()); while (!s.isEmpty()) q.enqueue(s.pop()); }

Fall 2020 15-121 (Reid-Miller) 18

slide-18
SLIDE 18

Reverse (recursively)

// reverses the queue objects, recursively public static void reverse(Queue<String> q) { if (!q.isEmpty()) String front = q.dequeue(); reverse(q); q.enqueue(front); }

Where’s the stack?

Fall 2020 15-121 (Reid-Miller) 19

slide-19
SLIDE 19

Array Queue

Fall 2020 15-121 (Reid-Miller) 20

slide-20
SLIDE 20

Queues using an array

1 2 3 4 5 6 7 8 9 10 11

  • Store the elements of the queue from front to back in
  • rder in the array.
  • What happens if we store the front of the queue always

in position 0?

  • Are all operations O(1)?

FRONT BACK Fall 2020 15-121 (Reid-Miller) 21

No, dequeue is O(n)

slide-21
SLIDE 21

Queues using an array

1 2 3 4 5 6 7 8 9 10 11

  • Store the elements of the queue from front to back in
  • rder in the array.
  • What happens if we store the back of the queue always

in last position in the array?

  • Are all operations O(1)?

FRONT BACK Fall 2020 15-121 (Reid-Miller) 22

No, enqueue is O(n)

slide-22
SLIDE 22

Queues using an array

1 2 3 4 5 6 7 8 9 10 11

  • Track positions of both front and back of queue.
  • To enqueue, increment back and add element at back.
  • To dequeue, remove the element at front and increment

front.

  • What if the back is last element of the array and want to

enqueue?

FRONT BACK Fall 2020 15-121 (Reid-Miller) 23

slide-23
SLIDE 23

Queues using a “circular” array

1 2 3 4 5 6 7 8 9 10 11

  • Allow the queue to “wrap around” from the last cell of the

array back to the first cell so no shifting is necessary.

  • Problem?
  • If the array is full, we can grow the array and copy the

queue data into the new array starting at position 0 again.

FRONT BACK Fall 2020 15-121 (Reid-Miller) 24

slide-24
SLIDE 24

Array Implementation

Fields

public class ArrayQueue<E> implements FIFOQueue<E> { private E[] dataArray; private int front; private int back; private int numElements; // methods (next slides) }

indices of front and back queue elements in array

Fall 2020 15-121 (Reid-Miller) 25

slide-25
SLIDE 25

Array Implementation

Constructor & isEmpty

public ArrayQueue() { dataArray = (E[])new Object[1]; front = -1; back = -1; numElements = 0; } public boolean isEmpty() { return (numElements == 0); }

indicates an empty queue

Fall 2020 15-121 (Reid-Miller) 26

slide-26
SLIDE 26

Array Implementation

enqueue

public void enqueue(E element) { if (______________________________________) grow(); back = ____________________________; dataArray[back] = element; if (front == -1) front = back; numElements++; }

numElements == dataArray.length

Fall 2020 15-121 (Reid-Miller) 27

(back+1) % dataArray.length

slide-27
SLIDE 27

Array Implementation

grow

1 2 3 4 5

FRONT BACK

1 2 3 4 5 6 7 8 9 10 11

FRONT BACK Fall 2020 15-121 (Reid-Miller) 28

What's wrong? If you keep dequeuing, it tries to dequeue at index 6.

slide-28
SLIDE 28

Array Implementation

grow

1 2 3 4 5

FRONT BACK

1 2 3 4 5 6 7 8 9 10 11

FRONT BACK Fall 2020 15-121 (Reid-Miller) 29

slide-29
SLIDE 29

Array Implementation

grow

private void grow() { E[] newArray = (E[])new Object[numElements*2]; int from = front; for (int to = 0; to < numElements; to++) { newArray[to] = dataArray[from]; from = ___________________________; } front = 0; back = numElements-1; dataArray = newArray; }

(from + 1) % numElements

Fall 2020 15-121 (Reid-Miller) 30

slide-30
SLIDE 30

Array Implementation

peek

public E peek() { if (__________________________) throw new NoSuchElementException(); return dataArray[front]; }

numElements == 0

Fall 2020 15-121 (Reid-Miller) 31

slide-31
SLIDE 31

Array Implementation

dequeue

public E dequeue() { E obj = peek(); dataArray[front] = null; if (front == back) { // was one element ______________________________________ } else ______________________________________ numElements--; return obj; }

front = -1; back = -1; front = (front+1)%dataArray.length;

Fall 2020 15-121 (Reid-Miller) 32

slide-32
SLIDE 32

Other uses for queues

  • Printer queues
  • Packet router
  • Simulating a queuing system
  • Supermarket checkout lanes
  • Highway traffic congestion models
  • Internet traffic

Fall 2020 15-121 (Reid-Miller) 33

slide-33
SLIDE 33

Java Queue<E> interface

  • The java.util package includes a Queue<E>

interface.

  • Queue<E> includes specifications for these methods:
  • boolean offer(E item)
  • E element() *
  • E peek()

**

  • E remove() *
  • E poll()

**

* Throws an exception if queue is empty. ** Returns null if the queue is empty. enqueue peek dequeue

Fall 2020 15-121 (Reid-Miller) 34

slide-34
SLIDE 34

Using Java Queue<E>

  • LinkedList<E> implements Queue<E>.
  • Example: A queue of customers

Queue<Customer> customerQ = new LinkedList<Customer>(); Customer c = new Customer("Andrew"); customerQ.offer(c); // OK customerQ.addFirst(c); // BAD (Why?)

Fall 2020 15-121 (Reid-Miller) 35