lec14 - CSE 12 The Queue ADT and Circular Arrays...

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

View Full Document Right Arrow Icon
14 Alternative implementations of Queue The Adapter pattern, revisited Circular arrays Time costs of Queue implementations CSE 12 The Queue ADT and Circular Arrays
Background image of page 1

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

View Full DocumentRight Arrow Icon
Implementing Queue Any interface specification (such as the one for Queue that we have just shown) can be implemented in many ways As one example, let’s consider using the Adapter Pattern, defining a ListQueue class to implement the Queue interface by adapting a List class
Background image of page 2
Implementing Queue Recall that we are implementing a particular variant of the Queue ADT, which is a limited capacity queue As with any interface specification, this ADT can be implemented in many ways We will consider two approaches: Adapting an existing List class A direct implementation using a circular array
Background image of page 3

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

View Full DocumentRight Arrow Icon
ListQueue: Applying the Adapter Pattern 1. Choose which implementation of List is most appropriate for representing Queue 2. Decide which attributes from List will play the roles of the Queue attributes 3. Select methods from List that will provide the behavior needed for the Queue API 4. Deciding how to support any Queue behavior or attributes that are not supported by List 5. Define the exception classes FullQueueException and EmptyQueueException Tasks to complete to implement a Queue by adapting List:
Background image of page 4
Choose the List Implementation to use What are the essential characteristics of a Queue? It is a linear structure that is accessed and modified only at the ends What are the essential characteristics of the List implementations in java.util ? ArrayList – modifying at index 0 is expensive O(n) while modifications at end are cheap O(1) LinkedList – accesses at either end are O(1) because it is doubly-linked and maintains a reference to the head and tail of the list LinkedList wins!
Background image of page 5

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

View Full DocumentRight Arrow Icon
Map Queue Attributes to List Attributes and/or Methods Queue Attribute List Attribute or Method size size() front head – position 0 of the list rear tail – position size() of the list capacity none What are we going to do about this?
Background image of page 6
Map Queue Methods to List Methods Queue Operation List Operation void enqueue( element ) void add( list.size(), element ) E dequeue() E remove( 0 ) E front() E get( 0 ) int size() int size() boolean isEmpty() boolean isEmpty() void clear() void clear() boolean isFull() none capacity() none What are we going to do about this?
Background image of page 7

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

View Full DocumentRight Arrow Icon
Providing for Queue Characteristics Not Supported by List Characteristics List has no support built in for enforcing a fixed upper
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.

This note was uploaded on 03/27/2012 for the course CSE 12 taught by Professor Gary during the Fall '08 term at UCSD.

Page1 / 27

lec14 - CSE 12 The Queue ADT and Circular Arrays...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online