68
Queue 7 December 2016 OSU CSE 1

Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

  • Upload
    dinhdan

  • View
    214

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Queue

7 December 2016 OSU CSE 1

Page 2: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Queue

• The Queue component family allows you to manipulate strings of entries of any (arbitrary) type in FIFO (first-in-first-out) order– "First" here refers to the temporal order in

which entries are put into the string and taken out of it, not about the left-to-right or right-to-left order in the string when it is written down

7 December 2016 OSU CSE 2

Page 3: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Interfaces and Classes

7 December 2016 OSU CSE 3

Queue

Queue1L Queue2

implements implements

QueueKernel

extends

Standard

extends

Page 4: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Interfaces and Classes

7 December 2016 OSU CSE 4

Queue

Queue1L Queue2

implements implements

QueueKernel

extends

Standard

extendsStandard has contracts

for three methods:clear

newInstancetransferFrom

Page 5: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Interfaces and Classes

7 December 2016 OSU CSE 5

Queue

Queue1L Queue2

implements implements

QueueKernel

extends

Standard

extends

QueueKernel has contracts for three methods:

enqueuedequeuelength

Page 6: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Interfaces and Classes

7 December 2016 OSU CSE 6

Queue

Queue1L Queue2

implements implements

QueueKernel

extends

Standard

extends

rotate

Queuehas contracts for six other

methods:appendflipfront

replaceFrontsortrotate

Page 7: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Mathematical Model

• The value of a Queue variable is modeled as a string of entries of type T

• Formally:type Queue is modeled bystring of T

7 December 2016 OSU CSE 7

Page 8: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Generics

• Note that Queue is a generic type (also called a parameterized type)

• The actual type of the entries is selected only later by the client when the type Queue is used to declare or instantiate a variable, e.g.:Queue<Integer> qi =new Queue1L<Integer>();

7 December 2016 OSU CSE 8

Page 9: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Generics

• Note that Queue is a generic type (also called a parameterized type)

• The actual type of the entries is selected only later by the client when the type Queue is used to declare or instantiate a variable, e.g.:Queue<Integer> qi =new Queue1L<Integer>();

7 December 2016 OSU CSE 9

The formal type parameter was called T; here, the actual type or

argument type is Integer.

Page 10: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Generics

• Note that Queue is a generic type (also called a parameterized type)

• The actual type of the entries is selected only later by the client when the type Queue is used to declare or instantiate a variable, e.g.:Queue<Integer> qi =new Queue1L<Integer>();

7 December 2016 OSU CSE 10

As of Java 7, generic arguments in a constructor call are inferred from the

declared type...

Page 11: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Generics

• Note that Queue is a generic type (also called a parameterized type)

• The actual type of the entries is selected only later by the client when the type Queue is used to declare or instantiate a variable, e.g.:Queue<Integer> qi =new Queue1L<>();

7 December 2016 OSU CSE 11

... so this diamond operator means the same

thing as the constructor with explicit generic

arguments.

Page 12: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Wrapper Types

• Note the use of Integer here, not int• Java demands that generic arguments

must be reference types• Each of the primitive types has a

corresponding wrapper type that is a reference type (in part to satisfy this requirement)

7 December 2016 OSU CSE 12

Page 13: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Wrapper Types

7 December 2016 OSU CSE 13

primitive type wrapper type

boolean Boolean

char Character

int Integer

double Double

Page 14: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Wrapper Types

• Each wrapper type is an immutable type• There is a constructor from the

corresponding primitive type• Java includes features called auto-boxing

and auto-unboxing so wrapper types can be used with primitive-type syntax almostas if they were primitive types– Details later (for now, look it up if it seems to

matter to your code)7 December 2016 OSU CSE 14

Page 15: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Constructors

• There is one constructor for each implementation class for Queue

• As always:– The name of the constructor is the name of

the implementation class– The constructor has its own contract (which is

in the kernel interface QueueKernel)

7 December 2016 OSU CSE 15

Page 16: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

No-argument Constructor

• Ensures:this = < >

7 December 2016 OSU CSE 16

Page 17: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Example

7 December 2016 OSU CSE 17

Code State

Queue<Integer> qi =new Queue1L<>();

Page 18: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Example

7 December 2016 OSU CSE 18

Code State

Queue<Integer> qi =new Queue1L<>();

qi = < >

Page 19: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Methods for Queue

• All the methods for Queue are instance methods, i.e., you call them as follows:q.methodName(arguments)

where q is an initialized non-null variable of type Queue<T> for some T

7 December 2016 OSU CSE 19

Page 20: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

enqueue

void enqueue(T x)

• Adds x at the back (right end) of this.• Aliases: reference x• Updates: this• Ensures:this = #this * <x>

7 December 2016 OSU CSE 20

Page 21: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

enqueue

void enqueue(T x)

• Adds x at the back (right end) of this.• Aliases: reference x• Updates: this• Ensures:this = #this * <x>

7 December 2016 OSU CSE 21

The list of references that might be aliases upon return from the

method is advertised here, because aliasing is important and

otherwise is not specified.

Page 22: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Example

7 December 2016 OSU CSE 22

Code Stateqi = < 49, 3 >k = 70

qi.enqueue(k);

Page 23: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Example

7 December 2016 OSU CSE 23

Code Stateqi = < 49, 3 >k = 70

qi.enqueue(k);

qi = < 49, 3, 70 >k = 70

Page 24: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Meaning of “Aliases: ...”

7 December 2016 OSU CSE 24

49 3 70 Before...

Page 25: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Meaning of “Aliases: ...”

7 December 2016 OSU CSE 25

49 3 70 After...

Page 26: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Meaning of “Aliases: ...”

• The tracing table notation with ➞ gives us no easy way to describe this situation– The picture is, however, a handy way to see

what’s going on, so draw pictures!• Since Integer is immutable, there is no

consequence to this case of aliasing– But consider:Queue<NaturalNumber> qn = ...

7 December 2016 OSU CSE 26

Page 27: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

dequeue

T dequeue()

• Removes and returns the entry at the front (left end) of this.

• Updates: this• Requires:this /= < >

• Ensures:#this = <dequeue> * this

7 December 2016 OSU CSE 27

Page 28: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Example

7 December 2016 OSU CSE 28

Code Stateqi = < 49, 3, 70 >k = –584

k = qi.dequeue();

Page 29: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Example

7 December 2016 OSU CSE 29

Code Stateqi = < 49, 3, 70 >k = –584

k = qi.dequeue();

qi = < 3, 70 >k = 49

Page 30: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

length

int length()

• Reports the length of this.• Ensures:length = |this|

7 December 2016 OSU CSE 30

Page 31: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Example

7 December 2016 OSU CSE 31

Code Stateqi = < 49, 3, 70 >n = –45843

n = qi.length();

Page 32: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Example

7 December 2016 OSU CSE 32

Code Stateqi = < 49, 3, 70 >n = –45843

n = qi.length();

qi = < 49, 3, 70 >n = 3

Page 33: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

front

T front()

• Returns the entry at the the front (left end) of this.

• Aliases: reference returned by front• Requires:this /= < >

• Ensures:<front> is prefix of this

7 December 2016 OSU CSE 33

Page 34: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Example

7 December 2016 OSU CSE 34

Code Stateqi = < 49, 3, 70 >k = –58

k = qi.front();

Page 35: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Example

7 December 2016 OSU CSE 35

Code Stateqi = < 49, 3, 70 >k = –58

k = qi.front();

qi = < 49, 3, 70 >k = 49

Page 36: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Meaning of “Aliases: ...”

7 December 2016 OSU CSE 36

49 3 70-58 Before...

Page 37: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Meaning of “Aliases: ...”

7 December 2016 OSU CSE 37

49 3 70-58 After...

Page 38: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

replaceFront

T replaceFront(T x)

• Replaces the front of this with x, and returns the old front.

• Aliases: reference x• Updates: this• Requires:

this /= < >

• Ensures:<replaceFront> is prefix of #this andthis = <x> * #this[1, |#this|)

7 December 2016 OSU CSE 38

Page 39: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Example

7 December 2016 OSU CSE 39

Code Stateqi = < 49, 70 >k = –58j = 16

k = qi.replaceFront(j);

Page 40: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Example

7 December 2016 OSU CSE 40

Code Stateqi = < 49, 70 >k = –58j = 16

k = qi.replaceFront(j);

qi = < 16, 70 >k = 49j = 16

Page 41: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Meaning of “Aliases: ...”

7 December 2016 OSU CSE 41

49 70-58 Before...16

Page 42: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Meaning of “Aliases: ...”

7 December 2016 OSU CSE 42

49 70-58 After...16

Page 43: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Another Example

7 December 2016 OSU CSE 43

Code Stateqi = < 49, 70 >j = 16

j = qi.replaceFront(j);

Page 44: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Another Example

7 December 2016 OSU CSE 44

Code Stateqi = < 49, 70 >j = 16

j = qi.replaceFront(j);

qi = < 16, 70 >j = 49

Page 45: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Another Example

7 December 2016 OSU CSE 45

Code Stateqi = < 49, 70 >j = 16

j = qi.replaceFront(j);

qi = < 16, 70 >j = 49

This use of the method avoids creating an alias: it

swaps j with the entry previously at the front.

Page 46: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

append

void append(Queue<T> q)

• Concatenates (“appends”) q to the end of this.

• Updates: this• Clears: q• Ensures:this = #this * #q

7 December 2016 OSU CSE 46

Page 47: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Example

7 December 2016 OSU CSE 47

Code Stateq1 = < 4, 3, 2 >q2 = < 1, 0 >

q1.append(q2);

Page 48: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Example

7 December 2016 OSU CSE 48

Code Stateq1 = < 4, 3, 2 >q2 = < 1, 0 >

q1.append(q2);

q1 = < 4, 3, 2, 1, 0 >q2 = < >

Page 49: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

flip

void flip()

• Reverses (“flips”) this.• Updates: this• Ensures:this = rev(#this)

7 December 2016 OSU CSE 49

Page 50: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Example

7 December 2016 OSU CSE 50

Code State

qi = < 18, 6, 74 >

qi.flip();

Page 51: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Example

7 December 2016 OSU CSE 51

Code State

qi = < 18, 6, 74 >

qi.flip();

qi = < 74, 6, 18 >

Page 52: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

sortvoid sort(Comparator<T> order)

• Sorts this according to the ordering provided by the compare method from order.

• Updates: this• Requires:

[the relation computed by order.compareis a total preorder]

• Ensures:this = [#this ordered by the relation

computed by order.compare]

7 December 2016 OSU CSE 52

Page 53: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Comparators

• The Java interface Comparator<T> is:public interface Comparator<T> {

/*** Returns a negative integer, zero, or* a positive integer as the first* argument is less than, equal to, or* greater than the second.*/ int compare(T o1, T o2);

}

7 December 2016 OSU CSE 53

Page 54: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Comparators

• The notion of “less than” and “greater than” is quite flexible

• The notion of “equal to” is not flexible– It is based on mathematical equality, not on a

flexible notion of being “equivalent to” • There are important technicalities...

7 December 2016 OSU CSE 54

Page 55: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

sortvoid sort(Comparator<T> order)

• Sorts this according to the ordering provided by the compare method from order.

• Updates: this• Requires:

[the relation computed by order.compareis a total preorder]

• Ensures:this = [#this ordered by the relation

computed by order.compare]

7 December 2016 OSU CSE 55

A total preorder means that any two values of type T are

comparable, and that there are no “cycles”, e.g., nothing like

a < b < c < a.

Page 56: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Creating a Comparatorprivate static class IntegerLT

implements Comparator<Integer> {@Overridepublic int compare(Integer o1, Integer o2) {if (o1 < o2) {return -1;

} else if (o1 > o2) {return 1;

} else {return 0;

}}

}

7 December 2016 OSU CSE 56

Page 57: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Creating a Comparatorprivate static class IntegerLT

implements Comparator<Integer> {@Overridepublic int compare(Integer o1, Integer o2) {if (o1 < o2) {return -1;

} else if (o1 > o2) {return 1;

} else {return 0;

}}

}

7 December 2016 OSU CSE 57

A class that implements Comparator is usually a

nested class (i.e., declared for local use inside another class),

and if so should be declared private static.

Page 58: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

An Easy Comparatorprivate static class IntegerLT

implements Comparator<Integer> {@Overridepublic int compare(Integer o1, Integer o2) {return o1.compareTo(o2);

}}

7 December 2016 OSU CSE 58

Since a generic parameter must be a referencetype, and each wrapper type T (here, Integer)

implements the interface Comparable<T>, each has a compareTo method that can be

called like this to simplify the code for comparein a Comparator<T> implementation.

Page 59: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Example

7 December 2016 OSU CSE 59

Code State

qi = < 8, 6, 92, 1 >

Comparator<Integer> ci =new IntegerLT ();

qi.sort(ci);

Page 60: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Example

7 December 2016 OSU CSE 60

Code State

qi = < 8, 6, 92, 1 >

Comparator<Integer> ci =new IntegerLT ();

qi.sort(ci);

qi = < 1, 6, 8, 92 >

Page 61: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

rotate

void rotate(int distance)• Rotates this.• Updates: this• Ensures:

if #this = <> thenthis = #this

elsethis =

#this[distance mod |#this|, |#this|) *#this[0, distance mod |#this|)

7 December 2016 OSU CSE 61

Page 62: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Example

7 December 2016 OSU CSE 62

Code State

qi = < 8, 6, 92, 3 >

qi.rotate(1);

Page 63: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Example

7 December 2016 OSU CSE 63

Code State

qi = < 8, 6, 92, 3 >

qi.rotate(1);

qi = < 6, 92, 3, 8 >

Page 64: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Example

7 December 2016 OSU CSE 64

Code State

qi = < 8, 6, 92, 3 >

qi.rotate(3);

Page 65: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Example

7 December 2016 OSU CSE 65

Code State

qi = < 8, 6, 92, 3 >

qi.rotate(3);

qi = < 3, 8, 6, 92 >

Page 66: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Example

7 December 2016 OSU CSE 66

Code State

qi = < 8, 6, 92, 3 >

qi.rotate(-1);

Page 67: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Example

7 December 2016 OSU CSE 67

Code State

qi = < 8, 6, 92, 3 >

qi.rotate(-1);

qi = < 3, 8, 6, 92 >

Page 68: Queue - Computer Science and Engineeringweb.cse.ohio-state.edu/.../2221/web-sw1/extras/slides/27.Queue.pdfMethods for Queue • All the methods for Queueare instance methods, i.e.,

Resources

• OSU CSE Components API: Queue– http://cse.osu.edu/software/common/doc/

• Java Libraries API: Comparator, Comparable– http://docs.oracle.com/javase/8/docs/api/

7 December 2016 OSU CSE 68