queues - uc3m€¦ · [email protected] characteristics linear structure access on one end for...

49
[email protected] Queues Carlos Delgado Kloos Dep. Ingeniería Telemática Univ. Carlos III de Madrid Java: Queues / 1

Upload: others

Post on 24-Jun-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

[email protected]

Queues

Carlos Delgado Kloos

Dep. Ingeniería Telemática

Univ. Carlos III de Madrid

Java: Queues / 1

Page 2: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

[email protected]

Example

The queue at the bus stop

The printer queue

Java: Queues / 2

Page 3: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

[email protected]

Characteristics

Linear structure

Access on one end for insertion andon the other for extraction

Java: Queues / 3

Page 4: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

[email protected]

Queue interface

public interface Queue {

public int size();

public boolean isEmpty();

public void enqueue(Object o)

throws QueueOverflowException;

public Object dequeue()

throws EmptyQueueException;

public Object front()

throws EmptyQueueException;

} Java: Queues / 4

Page 5: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

[email protected]

One interface and

several implementations

Java: Queues / 5

ArrayQueue

Queue

LinkedQueue

Page 6: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

Array-based

implementation

[email protected] Java: Queues / 6

0 1 2 N-13 4 5

1 2 3 4 5

0 1 2 N-13 4 5

5 6 78 9

top tail

toptail

Page 7: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

[email protected]

Implementation

based on linked lists

Java: Queues / 7

Madrid Miami Munich

top tail

Page 8: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

[email protected]

Implementation

based on linked lists

Java: Queues / 8

public class LinkedQueue implements Queue {

private Node top = null;

private Node tail = null;

private int size = 0;

(…)

}

Page 9: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

[email protected]

Insertion (enqueue)

Java: Queues / 9

MoscowMadrid Miami Munich

top tailn

Page 10: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

[email protected]

Implementation

based on linked lists

public void enqueue(Object info) {

Node n = new Node(info, null);

if (top == null)

top = n;

else

tail.setNext(n);

tail = n;

size++;

}

Java: Queues / 10

Page 11: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

[email protected]

Extraction (dequeue)

Java: Queues / 11

MoscowMadrid Miami Munich

toptail

Page 12: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

[email protected]

Implementation

based on linked lists

public Object dequeue()

throws EmptyQueueException {

Object info;

if (top == null)

throw new EmptyQueueException();

info = top.getInfo();

top = top.getNext();

if (top == null)

tail = null;

size--;

return info;

}

Java: Queues / 12

Page 13: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

Activity

View queue animations: http://courses.cs.vt.edu/

csonline/DataStructures/

Lessons/

QueuesImplementationView/

applet.html

http://www.cs.usask.ca/

resources/tutorials/

csconcepts/1998_2/queues/java/

[email protected] Java: Colas / 13

Page 14: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

Activity

Try the applet DepthBreadth.java

that can be found here: http://www.faqs.org/docs/javap/c11/s3.html

[email protected] Java: Queues / 14

Page 15: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

[email protected]@it.uc3m.es Java: Queues / 15

Other kinds of queues

(not queues any more)

Double-ended queues

Priority queues

Page 16: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

[email protected] Java: Queues / 16

Deques

(Double-ended queues)

insertFirst

removeFirst

removeLast

insertLast

first last

Page 17: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

[email protected] Java: Queues / 17

Interface for deques

public interface Deque {

public int size();

public boolean isEmpty();

public void insertFirst(Object info);

public void insertLast(Object info);

Page 18: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

[email protected] Java: Queues / 18

Interface for deques

public Object removeFirst()

throws EmptyDequeException;

public Object removeLast()

throws EmptyDequeException;

public Object first()

throws EmptyDequeException;

public Object last()

throws EmptyDequeException;

}

Page 19: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

[email protected] Java: Queues / 19

Stacks and queues

as deques

Stack Dequesize() size()

isEmpty() isEmpty()

top() last()

push(o) insertLast(o)

pop() removeLast()

Queue Dequesize() size()

isEmpty() isEmpty()

front() first()

enqueue(o) insertLast(o)

dequeue() removeFirst()

Page 20: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

[email protected] Java: Queues / 20

Definition of stacks

from deques

public class DequeStack implements Stack {

private Deque deque;

public DequeStack() {

deque = new Deque();

}

public int size() {

return deque.size();

}

public boolean isEmpty() {

return deque.isEmpty();

}

Page 21: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

[email protected] Java: Queues / 21

Definition of stacks

from deques

public void push(Object info) {

deque.insertLast(info);

}

public Object pop()

throws EmptyStackException {

try {

return deque.removeLast();

} catch (EmptyDequeException ede) {

throw new EmptyStackException();

}

}

Page 22: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

[email protected] Java: Queues / 22

Definition of stacks

from deques

public Object top()

throws EmptyStackException {

try {

return deque.last();

} catch (EmptyDequeException ede) {

throw new EmptyStackException();

}

}

Page 23: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

Implementation of deques

based on lists

Singly-linked lists are not appropriate because removeLast requires the whole

list to be traversed, in order to get the reference of the last-but-one node

Solution: doubly-linked lists

[email protected] Java: Queues / 23

Page 24: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

Doubly Linked Lists

Linked lists in which each node has anadditional reference pointing to theprevious node in the list.

Can be traversed both from the beginning tothe end and vice-versa

RemoveLast does not need the whole list to

be traversedtop

24

info

nextprev

info

nextprev

info

nextprev

nullnull

tail

Page 25: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

The DLNode class

Public class DLNode {

private Object info;

private DLNode next;

private DLNode prev;

public DLNode(Object info) {…}

public DLNode(Object info, DLNode prev, DLNode next) {…}

public DLNode getNext() {…}

public void setNext(DLNode next) {…}

public DLNode getPrev() {…}

public void setPrev(DLNode prev) {…}

public Object getInfo() {…}

public void setInfo(Object info) {…}

}

25

Page 26: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

Inserting a node

top

node

DLNode node = new DLNode(data);

prev

null

26

nullnull

null

tail

Page 27: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

Inserting a node

top

node

node.setPrev(prev);

if (prev != null) {

node.setNext(prev.getNext());

prev.setNext(node);

}

prev

null

27

null

tail

Page 28: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

Inserting a node

top

nodeif (prev == null) {

node.setNext(top);

top = node;

}

if (node.getNext() != null) {

node.getNext().setPrev(node);

} else {

tail = node;

}

prev

null

28

null

tail

Page 29: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

Inserting a node

/**

* Inserts ‘data’ after the ‘prev’ node. If ‘prev’

* is null, ‘data’ is inserted at the first position of

* the list.

*/

public void insert(DLNode prev, Object data) {

DLNode node = new DLNode(data);

node.setPrev(prev);

if (prev != null) {

node.setNext(prev.getNext());

prev.setNext(node);

} else {

node.setNext(top);

top = node;

}

if (node.getNext() != null) {

node.getNext().setPrev(node);

} else {

tail = node;

}

}29

Page 30: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

Removing a node

top

Object data = removed.getInfo();

removed

null

30

null

taildata

Page 31: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

Removing a node

top

if (removed.getPrev() != null) {

removed.getPrev().setNext(removed.getNext());

} else {

top = removed.getNext();

}

removed

null

31

null

taildata

Page 32: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

Removing a node

top

if (removed.getNext() != null) {

removed.getNext().setPrev(removed.getPrev());

} else {

tail = removed.getPrev();

}

removed

null

32

null

taildata

Page 33: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

Removing a node

top

null

33

null

taildata

Page 34: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

Removing a node

/**

* Removes a node from the list and returns

* the information it holds.

*/

public Object remove(DLNode removed) {

Object data = removed.getInfo();

if (removed.getPrev() != null) {

removed.getPrev().setNext(removed.getNext());

} else {

top = removed.getNext();

}

if (removed.getNext() != null) {

removed.getNext().setPrev(removed.getPrev());

} else {

tail = removed.getPrev();

}

return data;

}

34

Page 35: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

Alternate implementation

Checking that the previous and next nodes are not null makes the previous implementation complex and error-prone

Possible simplification:Create two special nodes, without associated info, so

that one is always at the beginning of the list and the other one is always at the end:An empty list contains only those two empty nodes.For insertions and removals, it is guaranteed that the

previous and next nodes exists, so there is no need to check them.References top and tail do not need to be updated

35

Page 36: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

Alternate implementation

36

top

nullnull

tail

null

null null

Page 37: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

[email protected] Java: Queues / 37

Implementation

based on lists

public class DLDeque implements Deque {

private DLNode top, tail;

private int size;

public DLDeque() {

top = new DLNode();

tail = new DLNode();

tail.setPrev(top);

top.setNext(tail);

size = 0;

}

Page 38: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

[email protected] Java: Queues / 38

Insertion

Madrid Miami Munich

Moscow

first

second

top tail

Page 39: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

[email protected] Java: Queues / 39

Implementation

based on lists

public void insertFirst(Object info) {

DLNode second = top.getNext();

DLNode first = new DLNode(info, top, second);

second.setPrev(first);

top.setNext(first);

size++;

}

Page 40: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

[email protected] Java: Queues / 40

Extraction

Madrid Miami MunichMoscow

firsttop second tail

Page 41: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

[email protected] Java: Queues / 41

Implementation

based on lists

public Object removeFirst()

throws EmptyDequeException {

if (top.getNext() == tail)

throw new EmptyDequeException();

DLNode first = top.getNext();

Object info = first.getInfo();

DLNode second = first.getNext();

top.setNext(second);

second.setPrev(top);

size--;

return info;

}

Page 42: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

Activity

Review how “queues” are implemented in

http://java.sun.com/docs/books/tutorial

/collections/interfaces/queue.html

http://java.sun.com/javase/6/docs/api

/java/util/Queue.html

[email protected] Java: Queues / 42

Page 43: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

[email protected] Java: Queues / 43

Priority queue

A priority queue is a linear data structure where elements are retuned according to a value associated to them (priority)(and not necessarily to the order of insertion).

The priority might be the value of the element itself, but it might also differ from it.

Page 44: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

[email protected] Java: Queues / 44

Interface for

priority queues

public interface PriorityQueue {

public int size();

public boolean isEmpty();

public void insertItem(Comparable priority,

Object info);

public Object minElem()

throws EmptyPriorityQueueException;

public Object removeMinElem()

throws EmptyPriorityQueueException;

public Object minKey()

throws EmptyPriorityQueueException;

}

Page 45: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

[email protected] Java: Queues / 45

Example

2 1

3 2 4 1 5

2 1 3

2 1

+ + + – + – + –+ + + – + – + –

4 533

Page 46: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

[email protected] Java: Queues / 46

Example

2 1

3 2 4 1 5

2 1 3

2 1

+ + + – + – + –+ + + – + – + –

4 533

minminmin

Page 47: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

[email protected] Java: Queues / 47

Example

2 5

3 2 4 1 5

2 1 3

2

+ + + – + – + –+ + + – + – + –

11 33 4

insert in orderinsert in orderinsert in orderinsert in orderinsert in order

Page 48: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

[email protected] Java: Queues / 48

Implementations

With an unsorted sequence

Easy insertion

Comparison needed for extraction

With a sorted sequence

Comparison needed for insertion

Easy extraction

Page 49: Queues - UC3M€¦ · cdk@it.uc3m.es Characteristics Linear structure Access on one end for insertion and on the other for extraction Java: Queues / 3

Activity

Tryhttp://www.akira.ruc.dk/~keld/algoritmik_e99/

Applets/Chap11/PriorityQ/PriorityQ.html

[email protected] Java: Queues / 49