queue and its implementationtmengistu/courses/fall2015/cs220...•priority can be implemented using...
TRANSCRIPT
![Page 1: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/1.jpg)
Queue and Its Implementation
1
Tessema M. MengistuDepartment of Computer Science
Southern Illinois University [email protected]
Room - 3131
![Page 2: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/2.jpg)
Outline
• Queue ADT
• Linked List Based Implementation
• Array Based Implementation
• Vector Based Implementation
• Variants of Queue
– Double Ended Queue - Deque
– Priority Queue
![Page 3: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/3.jpg)
Queue ADT
• Another name for a waiting line
• Organizes its entries according to the order inwhich they were added
• Has a characteristics of First in, first out (FIFO)
– The first element entered the queue is the firstelement to be processed
• Has two ends – back (rear) and front
![Page 4: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/4.jpg)
Queue
• All additions to a queue are at its back (rear)
– Called enqueue
– The recent item added
• All removal from the queue is at its front
– Called dequeue
• The earliest item added
![Page 5: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/5.jpg)
Queue
• Used within operating systems
• Simulate many real world events
![Page 6: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/6.jpg)
ADT Queue • Data
– A collection of objects in chronological order and having the same data type
• Operations
– enqueue(newEntry):void
– dequeue():T
– getFront():T
– isEmpty():boolean
– clear():void
![Page 7: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/7.jpg)
![Page 8: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/8.jpg)
Example
![Page 9: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/9.jpg)
![Page 10: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/10.jpg)
Java Class Library
• Interface Queue– public boolean add(T newEntry)
– public boolean offer(T newEntry)
– public T remove()
– public T poll()
– public T element()
– public T peek()
– public boolean isEmpty()
– public void clear()
– public int size()
![Page 11: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/11.jpg)
Linked Implementation of a Queue
• Consider chain of linked nodes
– Head reference insufficient
– Must also have tail reference
• Which should be front of queue?
– Head easier to be front of queue for entry removal
– Adding entries at tail/back of queue easily done
![Page 12: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/12.jpg)
![Page 13: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/13.jpg)
enqueue Method
• If empty
![Page 14: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/14.jpg)
enqueue Method
• If not empty
![Page 15: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/15.jpg)
enqueue Method
![Page 16: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/16.jpg)
dequeue Method
• Only one element
![Page 17: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/17.jpg)
dequeue Method
• More than one Elements
![Page 18: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/18.jpg)
dequeue Method
![Page 19: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/19.jpg)
Other Methods
![Page 20: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/20.jpg)
Array-Based Implementation of a Queue
• Array named queue
– queue[0] is front
– frontIndex, backIndex are indices of front and back of queue
![Page 21: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/21.jpg)
Array Based Implementation …
• What happens during dequeue?
– With queue[0] always as front, must shift elements
• Not efficient
– Instead, move frontIndex
![Page 22: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/22.jpg)
Array Based Implementation …
• Then we run off the end of the array!?
• Solution ?
• Expand?
– left many spaces unoccupied
• Use unoccupied spaces
![Page 23: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/23.jpg)
Array Based Implementation …
• Once the queue reaches the end of the array,,we can add subsequent entries to the queueat the beginning of the array.
• The array behave as circular
– Its first location follows its last one
![Page 24: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/24.jpg)
Array Based Implementation …• Increment indices with modulo operatorbackIndex = (backIndex + 1) % queue.length;
frontIndex =(frontIndex + 1)% queue.length;
![Page 25: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/25.jpg)
Array Based Implementation …
![Page 26: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/26.jpg)
Array Based Implementation …
• How do we know the queue is full?
fronIndex = backIndex + 1
![Page 27: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/27.jpg)
Array Based Implementation …• How do we know the queue is Empty?
frontIndex = backIndex + 1
![Page 28: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/28.jpg)
Array Based Implementation …
• Problem
– No way to decide whether the queue is empty or full using index
• Solution
– Have a counter variable and test the variable
• The enqueue and dequeue methods should manipulate this variable – inefficient
– Leave one array location unused
![Page 29: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/29.jpg)
Circular Array with One Unused Element
• Allows detection of empty Vs. full queue
– Examine frontIndex, backIndex
![Page 30: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/30.jpg)
Circular Array …
• Any pattern?– full
frontIndex = (backIndex + 2) % queue.length– Empty
frontIndex = (backIndex + 1) % queue.length
![Page 31: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/31.jpg)
Circular Array …
![Page 32: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/32.jpg)
dequeue Method
![Page 33: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/33.jpg)
dequeue Method
![Page 34: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/34.jpg)
getFront Method
![Page 35: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/35.jpg)
enqueue Method
• ensureCapacity() – reading assignment
![Page 36: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/36.jpg)
Other Methods
public void clear(){
while(!isEmpty())dequeue();
}
![Page 37: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/37.jpg)
Vector Based Implementation of a Queue
• Front of queue at beginning of vector
• Vector add method used at back of queue
• Remove from front of queue
– Vector takes care of moving elements
– No indices needed
• Vector manages additional space as needed
![Page 38: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/38.jpg)
![Page 39: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/39.jpg)
Vector Based Implementation …
• enqueue method
• getFront method
![Page 40: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/40.jpg)
Vector Based Implementation …• dequeue method
• isEmpty method
• clear method
![Page 41: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/41.jpg)
Efficiency of Vector Based Implementation
• Since we add entries to one end of a queue and remove them from the other end, the vector implementation inherently moves its entries after each removal.
– dequeue() is O(n)
– Other methods O(1)
![Page 42: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/42.jpg)
• Exercise
– Create a queue that can contain Strings
– Add 5 strings to the queue
– Remove the first two strings from the queue
– Add additional three strings
– Display the content of the queue
![Page 43: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/43.jpg)
Double Ended Queue
• Allows add, remove, or retrieve entries at both the front and back of a queue
• In short called deque – pronounced as “deck”
• Has queue like operations and stack like operations
– addToBack() and removeFront() – queue
– addToFront() and removeFront() – stack
– getFront(), getBack(), and removeBack()
![Page 44: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/44.jpg)
Deque ADT
![Page 45: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/45.jpg)
Deque …
![Page 46: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/46.jpg)
Deque …
• Output?
![Page 47: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/47.jpg)
Doubly Linked Implementation of a Deque
• We need a way to traverse the liked nodes from both ends
– Doubly linked list
![Page 48: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/48.jpg)
Doubly Linked …
![Page 49: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/49.jpg)
addToBack() Method
![Page 50: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/50.jpg)
addToBack() Method
![Page 51: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/51.jpg)
addToFront() Method
![Page 52: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/52.jpg)
removeFront() Method
![Page 53: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/53.jpg)
removeFront() Method
![Page 54: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/54.jpg)
removeBack() Method
![Page 55: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/55.jpg)
Other Methods
• getFront()
• getBack()?
![Page 56: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/56.jpg)
Other Methods
• Better clear() implementation??
![Page 57: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/57.jpg)
Java Class Library• Interface Deque - extends Queue
– public void addFirst(T newEntry)
– public boolean offerFirst(T newEntry)
– public void addLast(T newEntry)
– public boolean offerLast(T newEntry)
– public T removeFirst()
– public T pollFirst()
– public T removeLast()
– public T pollLast()
– public T getFirst()
– public T peekFirst()
– public T getLast()
– Public T peekLast()
– public boolean isEmpty()
– public void clear()
– public int size()
![Page 58: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/58.jpg)
Java Class Library
• Class ArrayDeque
– Implements Deque
• Note – has methods appropriate for deque, queue, and stack
– Could be used for instances of any of these
• Constructors– public ArrayDeque()
– public ArrayDeque(int initialCapacity)
![Page 59: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/59.jpg)
Priority Queue
• Organizes objects according to their priorities
• Example– Bank Vs Hospital ER
• What exactly is a priority depends on the context of the application
• By making the objects Comparable, we can hide this detail in the objects’ method compareTo
![Page 60: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/60.jpg)
![Page 61: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/61.jpg)
Priority Queue
• Example
![Page 62: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/62.jpg)
Priority Queue
• Priority can be implemented using Array, linked List, or Vector
• If a linked chain contains the entries in a priority queue, the entry with the highest priority should occur at the beginning of the chain, where it is easy to remove
![Page 63: Queue and Its Implementationtmengistu/Courses/Fall2015/CS220...•Priority can be implemented using Array, linked List, or Vector •If a linked chain contains the entries in a priority](https://reader034.vdocuments.site/reader034/viewer/2022042201/5ea115d03531ab4fc02e4eb7/html5/thumbnails/63.jpg)
Java Class Library• Class PriorityQueue constructors and methods
– public PriorityQueue()
– public PriorityQueue(int initialCapacity)
– public boolean add(T newEntry)
– public boolean offer(T newEntry)
– public T remove()
– public T poll()
– public T element()
– public T peek()
– public boolean isEmpty()
– public void clear()
– public int size()