queue ADT Sept. 27/28, 2017 1 ADT (abstract data type) List - - PowerPoint PPT Presentation

queue adt
SMART_READER_LITE
LIVE PREVIEW

queue ADT Sept. 27/28, 2017 1 ADT (abstract data type) List - - PowerPoint PPT Presentation

COMP 250 Lecture 9 queue ADT Sept. 27/28, 2017 1 ADT (abstract data type) List add(i,e), remove(i), get(i), set(i), .. Stack push, pop(), .. Queue enqueue( e ), dequeue() 2 Queue dequeue (remove from enqueue front)


slide-1
SLIDE 1

1

COMP 250

Lecture 9

queue ADT

  • Sept. 27/28, 2017
slide-2
SLIDE 2

ADT (abstract data type)

  • List

add(i,e), remove(i), get(i), set(i), …..

  • Stack

push, pop(), ..

  • Queue

enqueue( e ), dequeue()

2

slide-3
SLIDE 3

Queue

3

dequeue (remove from front) enqueue (add at back)

slide-4
SLIDE 4

Examples

  • keyboard buffer
  • printer jobs
  • CPU processes (applications do not run in parallel)
  • web server
  • …….

4

slide-5
SLIDE 5

5

Stack

push(e) pop() LIFO (last in, first out)

slide-6
SLIDE 6

6

Stack

push(e) pop() LIFO (last in, first out)

Queue

enqueue( e ) dequeue() FIFO (first in, first out) “first come, first serve”

slide-7
SLIDE 7

7

Exercise: Use stack(s) to implement a queue.

enqueue( e ){ // add element : } dequeue( ) { // remove ‘oldest’ element : } Write pseudocode for these two methods that uses a stack, namely use the operations push(e ) , pop(), isEmpty() .

slide-8
SLIDE 8

Hint for Exercise

8

i h g f e d c b a top

s

tmpS

Use a second stack.

slide-9
SLIDE 9

Hint for Exercise

9

i h g f e d c b a top

s

tmpS while ( ! s.isEmpty() ){ tmpS.push( s.pop( ) ) } a b c d e f g h i

s

tmpS

slide-10
SLIDE 10

Queue Example

enqueue( a ) enqueue( b ) dequeue( ) a ab b

10

slide-11
SLIDE 11

Queue Example

enqueue( a ) enqueue( b ) dequeue( ) enqueue( c ) enqueue( d ) enqueue( e ) dequeue( ) enqueue( f ) enqueue( g ) a ab b bc bcd bcde cde cdef cdefg

11

slide-12
SLIDE 12

How to implement a queue?

enqueue(e) dequeue() singly linked list doubly linked list array list

12

slide-13
SLIDE 13

How to implement a queue?

enqueue(e) dequeue() singly linked list addLast(e) removeFirst() doubly linked list (unnecessary) array list

13

slide-14
SLIDE 14

How to implement a queue?

enqueue(e) dequeue() singly linked list addLast(e) removeFirst() doubly linked list (unnecessary) array list addLast(e) removeFirst() SLOW

14

slide-15
SLIDE 15

15

length = 4

Implementing a queue with an array list. (BAD) enqueue( a ) a--- enqueue( b ) ab-- dequeue( ) b---

Requires shift

0123

indices

0123

slide-16
SLIDE 16

16

length = 4

Implementing a queue with an array list. (BAD) enqueue( a ) a--- enqueue( b ) ab-- dequeue( ) b--- enqueue( c ) bc-- enqueue( d ) bcd- enqueue( e ) bcde dequeue( ) cde-

Requires shift Requires shift

0123

indices

0123

slide-17
SLIDE 17

17

length = 4

Implementing a queue with an array list. (BAD) enqueue( a ) a--- enqueue( b ) ab-- dequeue( ) b--- enqueue( c ) bc-- enqueue( d ) bcd- enqueue( e ) bcde dequeue( ) cde- enqueue( f ) cdef enqueue( g ) cdefg--- 0123

indices requires expansion

0123

slide-18
SLIDE 18

18

Use head and tail indices (tail = head + size – 1)

Implementing a queue with an expanding array. (also BAD) enqueue( a ) a--- (0,0) enqueue( b ) ab-- (0,1) dequeue( )

  • b--

(1,1) enqueue( c )

  • bc-

(1,2) enqueue( d )

  • bcd

(1,3) enqueue( e ) ? --- (1,4) dequeue( )

  • -cde---

(2,4) enqueue( f )

  • -cdef--

(2,5) enqueue( g )

  • -cdefg-

(2,6)

slide-19
SLIDE 19

19

Use head and tail indices (tail = head + size – 1)

Implementing a queue with an expanding array. (also BAD) enqueue( a ) a--- (0,0) enqueue( b ) ab-- (0,1) dequeue( )

  • b--

(1,1) enqueue( c )

  • bc-

(1,2) enqueue( d )

  • bcd

(1,3) enqueue( e )

  • bcde---

(1,4) dequeue( )

  • -cde---

(2,4) enqueue( f )

  • -cdef--

(2,5) enqueue( g )

  • -cdefg-

(2,6)

Make bigger array and copy to it.

slide-20
SLIDE 20

20

An expanding array is an inefficient usage of space. A better idea is….

slide-21
SLIDE 21

Circular array

length = 4 length = 8

0123 01234567 0123 01234567

1 2 3 4 5 6 7 1 2 3

slide-22
SLIDE 22

22

Circular array

tail = (head + size – 1) % length head tail

  • bc-

head=1 tail=2

b c

0123

1 2 3 enqueue( a enqueue( b ) dequeue() enqueue( c )

slide-23
SLIDE 23

23

Circular array

tail = (head + size – 1) % length head tail e-cd

c d e

0123

1 2 3

tail=0 head=2

enqueue( a ) enqueue( b ) dequeue( ) enqueue( c ) enqueue( d ) enqueue( e ) dequeue()

slide-24
SLIDE 24

24

Circular array

tail = (head + size – 1) % length e-cd tail=0 head=2

0123

enqueue( element ){ if (size < length) queue[ (tail + 1) % length] = element else …. // coming up size = size+1 } dequeue(){ // check if empty omitted element = queue[head] head = (head + 1) % length size = size-1 return element }

slide-25
SLIDE 25

tail = (head + size – 1) % length (head, tail, size)

enqueue( a )

a--- (0, 0, 1)

enqueue( b )

ab-- (0, 1, 2)

dequeue()

  • b--

(1, 1, 1)

enqueue( c )

  • bc-

(1, 2, 2)

enqueue( d )

  • bcd

(1, 3, 3)

enqueue( e ) ebcd (1, 0, 4) dequeue() e-cd (2, 0, 3) enqueue( f ) efcd (2, 1, 4)

array

Implementing a queue with a circular array (GOOD)

slide-26
SLIDE 26

tail = (head + size – 1) % length (head, tail, size)

enqueue( a )

a--- (0, 0, 1)

enqueue( b )

ab-- (0, 1, 2)

dequeue()

  • b--

(1, 1, 1)

enqueue( c )

  • bc-

(1, 2, 2)

enqueue( d )

  • bcd

(1, 3, 3)

enqueue( e ) ebcd (1, 0, 4) dequeue() e-cd (2, 0, 3) enqueue(f) efcd (2, 1, 4) enqueue(g) ?

array

Implementing a queue with a circular array

slide-27
SLIDE 27

27

Increase length of array and copy? BAD

tail head head tail

head = 2 tail = 1 size = 4

e f c d e f c d - - - -

0 1 2 3

enqueue(g) ?

slide-28
SLIDE 28

28

tail head head tail

head = 2 tail = 1 size = 4 head = 0 tail = 3 size = 4

e f c d c d e f - - - -

0 1 2 3

Increase length of array. Copy so that head moves to front.

(GOOD)

enqueue(g)

slide-29
SLIDE 29

29

enqueue( element ){ if ( queue.size == queue.length) { // increase length of array create a bigger array tmp[ ] // e.g. 2*length for i = 0 to queue.length - 1 tmp[i] = queue[ (head + i) % queue.length ] head = 0 queue = tmp } queue[size] = element queue.size = queue.size + 1 }

slide-30
SLIDE 30

tail = (head + size – 1) % length (head, tail, size)

Initial state

  • (0, 3, 0)

enqueue( a )

a--- (0, 0, 1)

enqueue( b )

ab-- (0, 1, 2)

dequeue()

  • b--

(1, 1, 1)

dequeue()

  • (2, 1, 0)

array

What happens when size == 0 ?

tail head

slide-31
SLIDE 31

ADT’s, API’s & Java

The following are related, but quite different:

  • ADT (abstract data type)
  • Java API (application program interface)
  • Java keyword interface

To be discussed much more at end of the course.

31

slide-32
SLIDE 32

ADT (abstract data type)

Defines a data type by the values and operations from the user’s perspective only. It ignores the details of the implementation. Examples:

  • list
  • stack
  • queue

32

slide-33
SLIDE 33

Java API

API = application program interface Gives class methods and some fields, and comments

  • n what the methods do. e.g.

https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html

33

slide-34
SLIDE 34

Java interface

  • reserved word

(nothing to do with “I” in “API”)

  • like a class, but only the method signatures are

defined

34

slide-35
SLIDE 35

interface List<T> { void add(T) void add(int, T) T remove(int) boolean isEmpty() T get( int ) int size() : }

https://docs.oracle.com/javase/7/docs/api/java/util/List.html

Example: List interface

35

slide-36
SLIDE 36

class ArrayList<T> implements List<T> { void add(T) { …. } void add(int, T) { …. } T remove(int) { …. } boolean isEmpty() { …. } T get( int ) { …. } int size() { …. } : } Each of the List methods are implemented. (In addition, other methods may be defined and implemented.)

36

slide-37
SLIDE 37

class LinkedList<T> implements List<T> { void add(T) { …. } void add(int, T) { …. } T remove(int) { …. } boolean isEmpty() { …. } T get( int ) { …. } int size() { …. } : } Each of the List methods are implemented. (In addition, other methods may be defined and implemented.)

37

slide-38
SLIDE 38

More examples

  • interface List

add(i,e), remove(i), get(i), set(i), …..

  • class Stack

push, pop(), ..

  • interface

Queue

  • ffer( e ), poll (), ….

38