Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received
Properties of queues First In First Out (FIFO) Data added at one end only (the Tail of the queue) Data removed at other end only (the Head of the queue)
Queue operations Initialise queue Add item (to tail of queue) Remove item (from head of queue) Check if queue empty Check if queue full
Queues using arrays Use array to store queue elements Define a data item Head which identifies the item at Head of queue Define a data item Tail which identifies the first empty location after last item in queue Tail identifies location where next item is placed in queue
Queues using arrays (first try) Head Tail Empty queue (Tail == Head)
Queues using arrays (first try) Head Tail Add item T at location Tail T
Queues using arrays (first try) Head Tail Add item H at location Tail TH
Queues using arrays (first try) Head Tail Add item I at location Tail THI
Queues using arrays (first try) Head Tail Remove item T from Head HI
Queues using arrays (first try) Head Tail Continue until Tail == ArraySize Queue full? AQUEUE
Queues using arrays (first try) Head Tail Must shift queue contents back to start of array - inefficient! AQUEUE
Circular Queue Use a circular queue Consider (perceive?) the array as a circular structure (i.e. as if the last element of the array is connected/joined to the first element of the array) The benefit of this insight is that we never have to shift data
A circular array MaxSize - 1 MaxSize - 2 MaxSize - 3
A queue using a circular array MaxSize - 1 Tail Head Empty queue Tail == Head 0
A queue using a circular array T MaxSize - 1 Tail Head Add T at Tail Tail = (Tail + 1) % MaxSize 0
A queue using a circular array T H MaxSize - 1 Tail Head Add H at Tail Tail = (Tail +1) % MaxSize 0
A queue using a circular array T H MaxSize - 1 Tail Head Add I at Tail Tail = (Tail +1) % MaxSize 0 I
A queue using a circular array H MaxSize - 1 Tail Head Remove T from Head Head = (Head +1) % MaxSize 0 I
A queue using a circular array MaxSize - 1 Tail Head Continue until Tail == MaxSize E U E U Q A
A queue using a circular array MaxSize - 1 Tail Head 0 E U E U Q A Add Z at Tail Tail = (Tail +1) % MaxSize i.e. [(MaxSize - 1) + 1] % MaxSize = MaxSize % MaxSize = 0 Z
Empty and Full Queue Tests Empty queue condition: Head = = Tail Full queue condition: (Tail + 1) % MaxSize = = Head
Queue ADT in Java Constructor isempty isfull Join Leave
Constructor public QueueOfInts() {Queue = new int[10] ; Head = Tail = 0 ; } public QueueOfInts(int capacity) {Queue = new int[capacity] ; Head = Tail = 0 ; }
Test for empty queue public boolean isempty() { return Head == Tail) } }
Test for full queue public boolean isfull() { if ((Tail + 1) % Queue.length == Head) { return true; } else { return false; }
Adding an element public void Join(int val) {Queue[Tail] = val ; Tail = (Tail + 1) % Queue.length; }
Remove an element public int Leave() {int Removed ; Removed = Queue[Head]; Head = (Head + 1) % Queue.length ; return Removed ; }