queue adt
play

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)


  1. COMP 250 Lecture 9 queue ADT Sept. 27/28, 2017 1

  2. ADT (abstract data type) • List add(i,e), remove(i), get(i), set(i), ….. • Stack push, pop(), .. • Queue enqueue( e ), dequeue() 2

  3. Queue dequeue (remove from enqueue front) (add at back) 3

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

  5. Stack push(e) pop() LIFO (last in, first out) 5

  6. Queue Stack enqueue( e ) push(e) dequeue() pop() FIFO LIFO (first in, first out) (last in, first out) “first come, first serve” 6

  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() . 7

  8. Hint for Exercise top i h g f e Use a second stack. d c b a s tmpS 8

  9. Hint for Exercise 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 9

  10. Queue Example enqueue( a ) a enqueue( b ) ab dequeue( ) b 10

  11. Queue Example 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 11

  12. How to implement a queue? enqueue(e) dequeue() singly linked list doubly linked list array list 12

  13. How to implement a queue? enqueue(e) dequeue() singly linked list addLast(e) removeFirst() doubly linked list (unnecessary) array list 13

  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

  15. Implementing a queue with an array list. (BAD) 0123 indices length = 4 0123 enqueue( a ) a--- enqueue( b ) ab-- dequeue( ) b--- Requires shift 15

  16. Implementing a queue with an array list. (BAD) 0123 indices length = 4 0123 enqueue( a ) a--- enqueue( b ) ab-- dequeue( ) b--- Requires shift enqueue( c ) bc-- enqueue( d ) bcd- enqueue( e ) bcde Requires shift dequeue( ) cde- 16

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

  18. Implementing a queue with an expanding array. (also BAD) Use head and tail indices (tail = head + size – 1) 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) 18

  19. Implementing a queue with an expanding array . (also BAD) Use head and tail indices (tail = head + size – 1) enqueue( a ) a--- (0,0) enqueue( b ) ab-- (0,1) dequeue( ) -b-- (1,1) enqueue( c ) -bc- (1,2) enqueue( d ) -bcd (1,3) Make bigger enqueue( e ) -bcde--- (1,4) array and copy to it. dequeue( ) --cde--- (2,4) enqueue( f ) --cdef-- (2,5) enqueue( g ) --cdefg- (2,6) 19

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

  21. Circular array length = 4 length = 8 01234567 0123 01234567 0123 2 1 1 0 3 0 3 2 7 4 6 5

  22. Circular array tail = (head + size – 1) % length enqueue( a enqueue( b ) dequeue() 0123 head 1 0 enqueue( c ) b -bc- c 2 3 head=1 tail=2 tail 22

  23. Circular array tail = (head + size – 1) % length enqueue( a ) enqueue( b ) dequeue( ) enqueue( c ) enqueue( d ) enqueue( e ) dequeue() 0123 1 tail 0 e e-cd c d 2 3 head tail=0 head=2 23

  24. Circular array tail = (head + size – 1) % length enqueue( element ){ if (size < length) queue[ (tail + 1) % length] = element else …. // coming up 0123 size = size+1 e-cd } dequeue(){ // check if empty omitted tail=0 head=2 element = queue[head] head = (head + 1) % length size = size-1 return element } 24

  25. Implementing a queue with a circular array (GOOD) tail = (head + size – 1) % length (head, tail, size) array 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)

  26. Implementing a queue with a circular array tail = (head + size – 1) % length (head, tail, size) array 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) ?

  27. Increase length of array and copy? BAD tail head head = 2 0 1 2 3 tail = 1 e f c d size = 4 e f c d - - - - enqueue(g) ? head tail 27

  28. Increase length of array. Copy so that head moves to front. (GOOD) tail head head = 2 0 1 2 3 tail = 1 e f c d size = 4 head = 0 tail = 3 c d e f - - - - size = 4 enqueue(g) head tail 28

  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 } 29

  30. What happens when size == 0 ? tail = (head + size – 1) % length (head, tail, size) array 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) tail head

  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

  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

  33. Java API API = application program interface Gives class methods and some fields, and comments on what the methods do. e.g. https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html 33

  34. Java interface • reserved word (nothing to do with “I” in “API”) • like a class, but only the method signatures are defined 34

  35. Example: List interface 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 35

  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

  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

  38. More examples • interface List add(i,e), remove(i), get(i), set(i), ….. • class Stack push, pop(), .. Queue • interface offer( e ), poll (), …. 38

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend