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() FIFO LIFO (first in, (last in, first out) first out) 3
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
Queue Example enqueue( a ) a enqueue( b ) ab dequeue( ) b enqueue( c ) bc enqueue( d ) bcd enqueue( e ) bcde dequeue( ) cde enqueue( f ) cdef dequeue( ) def enqueue( g ) defg 5
Implementing a queue with a singly linked list. enqueue( ) = addLast( ) dequeue( ) = removeFirst( ) 6
Implementing a queue with an array list. (1st attempt) length = 4 0123 indices enqueue( a ) a--- enqueue( b ) ab-- removeFirst (& shift) dequeue( ) b--- enqueue( c ) bc-- enqueue( d ) bcd- enqueue( e ) bcde removeFirst (& shift) dequeue( ) cde- enqueue( f ) cdef removeFirst (& shift) dequeue( ) def- enqueue( g ) defg 7
Implementing a queue with an array. (2 nd attempt) Use head and tail indices (tail = head + size – 1) enqueue( a ) a--- (0,0) Start with length = 4. enqueue( b ) ab-- (0,1) dequeue( ) -b-- (1,1) enqueue( c ) -bc- (1,2) enqueue( d ) -bcd (1,3) Need to increase length of array. enqueue( e ) -bcde--- (1,4) dequeue( ) --cde--- (2,4) enqueue( f ) --cdef-- (2,5) dequeue( ) ---def-- (3,5) enqueue( g ) ---defg- (3,6) 8
Circular array length = 4 length = 8 01234567 0123
Circular array tail = (head + size – 1) % length 0123 -bc- b c head tail tail head 0123 d e e-cd c head tail tail 10 head
Implementing a queue with a circular array (GOOD) tail = (head + size – 1) % length (head, tail, size) queue array enqueue( a ) a a--- (0,0, 1) enqueue( b ) ab ab-- (0,1, 2) dequeue() b -b-- (1,1, 1) enqueue( c ) -bc- (1,2, 2) bc enqueue( d ) -bcd (1,3, 3) bcd enqueue( e ) ebcd (1,0, 4) bcde dequeue() e-cd (2,4, 3) cde enqueue( f ) efcd (2,5, 4) cdef dequeue() ef-d (3,5, 3) def enqueue( g ) ef-- (2,6, 2) defg 11
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. 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 } 12
How to enqueue if the array is full ? enqueue( element ){ if ( size == length) increase length of array and rearrange size = size + 1 tail = (tail + 1) % length queue[tail] = element } 13
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. 14
increase length of array and rearrange tail head head = 1 0 1 2 3 tail = 0 e b c d size = 4 WHY? head = 0 tail = 3 b c d e - - - - size = 4 head tail 15
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 } 16
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() . 17
Hint for Exercise top top i a h b g c while ( ! s.isEmpty() ){ f d tmpS.push( s.pop( ) ) e e } d f c g b h a i s s tmpS tmpS 18
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 offer( e ), poll (), …. 19
Recommend
More recommend