queue ADT Sept. 23, 2016 1 Queue dequeue (remove from enqueue - - PowerPoint PPT Presentation

queue adt
SMART_READER_LITE
LIVE PREVIEW

queue ADT Sept. 23, 2016 1 Queue dequeue (remove from enqueue - - PowerPoint PPT Presentation

COMP 250 Lecture 8 queue ADT Sept. 23, 2016 1 Queue dequeue (remove from enqueue front) (add at back) Queues are heavily used in OS (operating systems) e.g. process scheduling. 2 Queue Stack enqueue( e ) push(e) dequeue() pop()


slide-1
SLIDE 1

1

COMP 250

Lecture 8

queue ADT

  • Sept. 23, 2016
slide-2
SLIDE 2

Queue

2

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

Queues are heavily used in OS (operating systems) e.g. process scheduling.

slide-3
SLIDE 3

3

Stack

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

Queue

enqueue( e ) dequeue() FIFO (first in, first out)

slide-4
SLIDE 4

ADT’s (abstract data types)

  • List

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

  • Stack

push, pop(), ..

  • Queue

enqueue( e ), dequeue()

Although stacks and queues consist of a finite ordered set of elements, strictly speaking, they are not lists since their operations do not allow one to index directly to the arbitrary elements.

4

slide-5
SLIDE 5

Queue Example

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

5

slide-6
SLIDE 6

6

Implementing a queue with a singly linked list.

enqueue( ) = addLast( ) dequeue( ) = removeFirst( )

slide-7
SLIDE 7

7

length = 4

removeFirst (& shift) removeFirst (& shift) removeFirst (& shift)

Implementing a queue with an array list. (1st attempt)

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

a--- ab-- b--- bc-- bcd- bcde cde- cdef def- defg 0123

indices

slide-8
SLIDE 8

8

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

Implementing a queue with an array. (2nd attempt)

enqueue( a ) enqueue( b ) dequeue( ) enqueue( c ) enqueue( d ) enqueue( e ) dequeue( ) enqueue( f ) dequeue( ) enqueue( g ) a--- (0,0) ab-- (0,1)

  • b--

(1,1)

  • bc-

(1,2)

  • bcd

(1,3)

  • bcde---

(1,4)

  • -cde---

(2,4)

  • -cdef--

(2,5)

  • --def--

(3,5)

  • --defg-

(3,6)

Start with length = 4. Need to increase length of array.

slide-9
SLIDE 9

Circular array

length = 4 length = 8

0123 01234567

slide-10
SLIDE 10

10

Circular array

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

  • bc-

head tail head tail e-cd head tail

b c c d e

0123 0123

slide-11
SLIDE 11

11

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

enqueue( a ) enqueue( b ) dequeue() enqueue( c ) enqueue( d ) enqueue( e ) dequeue() enqueue( f ) dequeue() enqueue( g ) a--- (0,0, 1) ab-- (0,1, 2)

  • b--

(1,1, 1)

  • bc-

(1,2, 2)

  • bcd

(1,3, 3) ebcd (1,0, 4) e-cd (2,4, 3) efcd (2,5, 4) ef-d (3,5, 3) ef-- (2,6, 2)

queue array

a ab b bc bcd bcde cde cdef def defg

Implementing a queue with a circular array (GOOD)

slide-12
SLIDE 12

12

dequeue( ){ // check that size >=1 (omitted) element = queue[ head ] if (size > 1) head = (head + 1) % length size = size – 1 // don’t adjust tail return element }

The code below does not properly handle the case that size == 1. See lecture notes where this has been corrected. Note that, when size == 0, head is different from tail. Also, when queue is initialized, head == 0 and tail == length – 1.

slide-13
SLIDE 13

13

enqueue( element ){ if ( size == length) increase length of array and rearrange size = size + 1 tail = (tail + 1) % length queue[tail] = element }

How to enqueue if the array is full ?

slide-14
SLIDE 14

14

The example shown in the following slide is slightly different from the one used in the lecture. Please see lecture notes for further discussion of enqueueing an element when the array is full.

slide-15
SLIDE 15

15

increase length of array and rearrange

tail head head tail

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

e b c d b c d e - - - -

0 1 2 3

WHY?

slide-16
SLIDE 16

16

enqueue( element ){ if ( size == length) { // increase length of array

create a bigger array called tmp for i = 0 to queue.length - 1 tmp[i] = queue[ (head + i) % queue.length ] head = 0 tail = size-1

} size = size + 1 tail = (tail + 1) % length queue[tail] = element }

slide-17
SLIDE 17

17

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-18
SLIDE 18

Hint for Exercise

18

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

top

s

tmpS

slide-19
SLIDE 19

Some possibly confusing terminology

(ADT, Java API, Java interface)

  • List

interface

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

  • Stack class

push, pop(), ..

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

19