ITI 1121. Introduction To Computing II
ITI 1121. Introduction To Computing II
ITI 1121. Introduction To Computing II
Introduction to Computing II
Marcel Turcotte School of Electrical Engineering and Computer Science Version of March 2, 2013 Abstract Queues LinkedQueue
These lecture notes are meant to be looked at on a computer screen. Do not print them unless it is necessary.
Denitions
A queue is a linear abstract data type such that insertions are made at one end, called the rear, and removals are made at the other end, called the front. Queues are sometimes called FIFOs: rst-in rst-out. enqueue() Queue dequeue() The two basic operations are: enqueue: adds an element to the rear of the queue; dequeue: removes and returns the element at the front of the queue.
Software queues are similar to physical ones: queuing at the supermarket, at the bank, at cinemas, etc.
Applications
Shared resources management (system programming): Access to the processor; Access to the peripherals such as disks and printers. Application programs: Simulations; Generating sequences of increasing length over a nite size alphabet; Navigating through a maze.
Example
public class Test { public static void main( String[] args ) { Queue<Integer> queue = new QueueImplementation<Integer>(); for ( int i=0; i<10; i++ ) queue.enqueue( new Integer( i ) ); while ( ! queue.isEmpty() ) System.out.println( queue.dequeue() ); } }
q = new Q(); q.enqueue( a ); q.enqueue( b ); q.enqueue( c ); q.dequeue( ); -> a q.dequeue( ); -> b q.enqueue( d ); q.dequeue( ); -> c q.dequeue( ); -> d
Elements of a queue are processed in the same order as the they are inserted into the queue, here a was the rst element to join the queue and it was the rst to leave the queue: rst-come rst-serve .
Implementations
Just like stacks, there are two families of implementations: Linked elements; Array-based.
ADT
public interface Queue { public abstract boolean isEmpty(); public abstract void enqueue( Object o ); public abstract Object dequeue(); }
ADT
public interface Queue<E> { public abstract boolean isEmpty(); public abstract void enqueue( E o ); public abstract E dequeue(); }
q D rear C B A
q A front rear B C D
Discussion
What are the consequences of these changes? Impact on memory usage is not signicant. Implementing the methods will be slightly more complex.
front rear
front rear
newElem
newElem
newElem
newElem
newElem
Removing an element
Identify the general case as well as the special case(s). Is the empty queue a special case? No, it is an illegal case, it should be handled by the pre-conditions and an exception should be thrown. The queue containing a single element will be the special case.
saved
rst
second
saved
rst
second
A saved
rst
second
A saved
rst
second
A saved
rst
second
A saved
rst
second
A saved
rst
second
saved
rst
saved
rst
Pitfall. Which expression can be used to identify a queue containing a single element? font != null && front.next == null What about this? front == rear
saved
rst
front rear
Solution:
saved
rst
saved
rst
saved
rst
saved
rst
saved
rst
saved
rst
Pitfall!
q D front rear
This memory diagram illustrates the kinds of errors that occur frequently with linked elements. What are the consequences? Consider using the following test to detect the empty queue: front == null && rear == null.