{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lect12_array_queue0 - ITI 1121 Introduction to Computing II...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
ITI 1121. Introduction to Computing II * Marcel Turcotte School of Information Technology and Engineering Version of March 1, 2010 Abstract Queues ArrayQueue * These lecture notes are meant to be looked at on a computer screen. Do not print them unless it is necessary.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Definitions 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: first-in first-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.
Background image of page 2
ADT public interface Queue<E> { public abstract boolean isEmpty(); public abstract void enqueue( E o ); public abstract E dequeue(); }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Using an array public class ArrayQueue<E> implements Queue<E> { private E[] elems; public boolean isEmpty() { ... } public void enqueue( E o ) { ... } public E dequeue() { ... } }
Background image of page 4
Array implementation of a queue Implementation 1: Let’s set the front of the queue to a fix location, say 0, and use an instance variable to indicate the location of the rear element. 6 7 0 1 2 3 4 5 8 9 queue rear = 4 a b c d e Contrary to the array-based implementation of a stack, using an array to implement a queue will create some problems, which we will circumvent, of course!
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Array implementation of a queue public class ArrayQueue<E> implements Queue<E> { private E[] elems; private int rear; public boolean isEmpty() { ... } public void enqueue( E o ) { ... } public E dequeue() { ... } }
Background image of page 6
Remarks Just like stacks, rear can be made to designate the first free location (instead of the element itself); a queue could be implemented in the upper part of the array. Without effects on the efficiency.
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Insertion (enqueue) Inserting an element into a queue, when the front location is fixed, is similar to adding an element onto a stack.
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}