18.10.2014 Views

COSC 1P03 Data Structures and Abstraction 18.1

COSC 1P03 Data Structures and Abstraction 18.1

COSC 1P03 Data Structures and Abstraction 18.1

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>COSC</strong> <strong>1P03</strong><br />

<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong><br />

<strong>COSC</strong> <strong>1P03</strong><br />

The Queue<br />

<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong> <strong>18.1</strong><br />

<strong>COSC</strong> <strong>1P03</strong><br />

Queue<br />

<br />

<br />

<br />

<br />

a list (initially empty) of items (of some type) to which items may<br />

be added at one end (called the rear) <strong>and</strong> from which items may<br />

be removed at the other end (called the front)<br />

examples<br />

waiting lines<br />

print queues<br />

behaviour<br />

FIFO ordering<br />

error conditions:<br />

underflow<br />

overflow<br />

<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong> 18.2<br />

Figure <strong>18.1</strong> Bank waiting line<br />

<strong>18.1</strong>


<strong>COSC</strong> <strong>1P03</strong><br />

<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong><br />

<strong>COSC</strong> <strong>1P03</strong><br />

Queue Interface<br />

<br />

<br />

<br />

generic<br />

E – items to be stored<br />

operations:<br />

enter (enqueue, add, insert)<br />

leave e (dequeue, remove, delete)<br />

front (head, first)<br />

length (count, size)<br />

empty<br />

exceptions<br />

NoItemException<br />

NoSpaceException<br />

<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong> 18.4<br />

<strong>COSC</strong> <strong>1P03</strong><br />

Queue ADT<br />

Contiguous Implementation<br />

<br />

based on variable-sized array<br />

two indices: front & rear<br />

add at rear, remove at front<br />

queue moves towards rear<br />

repositioning on delete: O(n)<br />

circular array<br />

at end of array reuse front<br />

index modulo array size<br />

<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong> 18.5<br />

front<br />

rear<br />

front<br />

rear<br />

0 1 2 3 4<br />

0 1 2 3<br />

45 6 7<br />

(a)<br />

(b)<br />

front<br />

rear<br />

rear<br />

front<br />

0 1 2 3<br />

0 1 2 3<br />

(c)<br />

Figure 18.3 Contiguous queue representation<br />

(d)<br />

18.2


<strong>COSC</strong> <strong>1P03</strong><br />

<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong><br />

<strong>COSC</strong> <strong>1P03</strong><br />

<br />

implementation<br />

instance variables<br />

count<br />

constructors<br />

empty state<br />

methods<br />

enter<br />

overflow<br />

increment<br />

leave, front<br />

underflow<br />

length, empty<br />

compute?<br />

empty vs full<br />

<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong> 18.7<br />

<strong>COSC</strong> <strong>1P03</strong><br />

Queue ADT<br />

Linked Implementation<br />

<br />

<br />

<br />

sequentially-linked structure of items<br />

deletion from front<br />

insertion at end<br />

keep pointer to rear O(1)<br />

length?<br />

keep count else O(n)<br />

comparison with contiguous<br />

all operations O(1)<br />

space tradeoffs<br />

<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong> 18.8<br />

front<br />

rear<br />

Figure 18.5 Linked queue representation<br />

18.3


<strong>COSC</strong> <strong>1P03</strong><br />

<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong><br />

<strong>COSC</strong> <strong>1P03</strong><br />

Java Collections Framework<br />

<br />

<br />

Queue interface<br />

generic in element type (E)<br />

some duplication of methods (Collection)<br />

st<strong>and</strong>ard queue methods<br />

add = enter<br />

remove = leave<br />

element = front<br />

size = length<br />

isEmpty = empty<br />

LinkedList class<br />

generic in element type (E)<br />

implements Queue as symmetrically-linked structure<br />

also implements List<br />

<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong> <strong>18.1</strong>0<br />

package java.util;<br />

public interface Queue < E > extends Collection {<br />

public boolean add ( E o ); // from Collection<br />

public void clear ( ); // from Collection<br />

public boolean contains ( Object o ); // from Collection<br />

public E element ( );<br />

public boolean equals ( Object o ); // from Collection<br />

public boolean isEmpty ( ); // from Collection<br />

public Iterator iterator ( ); // from Collection<br />

public boolean offer ( E o );<br />

public E peek ( );<br />

public E poll ( );<br />

public E remove ( );<br />

public boolean remove ( Object o ); // from Collection<br />

public int size ( ); // from Collection<br />

public Object[] toArray ( ); // from Collection<br />

} // Queue<br />

18.4

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!