Presentation is loading. Please wait.

Presentation is loading. Please wait.

A queue is a linear, homogeneous, container that stores and dispenses its content in a FIFO manner. FIFO - First In First Out The first (most distant)

Similar presentations


Presentation on theme: "A queue is a linear, homogeneous, container that stores and dispenses its content in a FIFO manner. FIFO - First In First Out The first (most distant)"— Presentation transcript:

1 A queue is a linear, homogeneous, container that stores and dispenses its content in a FIFO manner. FIFO - First In First Out The first (most distant) item inserted, and not yet removed, will be the first (next) item dispensed. Real World Examples The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

2 enqueue insert a new item into the queue dequeue remove one item from the container front inspect one item from the container, but don’t remove it An Abstract Picture The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

3 Class Specification The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub. Domain sequence of ItemType Constructor public Queue() post: this == sequence{} Query methods public boolean isEmpty() post: result == ( this-> size() == 0) public ItemType front() pre: ! isEmpty() post: result == this-> first() Update methods public void enqueue(ItemType z) post: this == -> append( z ) public void dequeue() pre: ! isEmpty() (throws java.util.NoSuchElementException) post: this == -> subSequence( 2, -> size() ) Queue

4 Example The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub. Queue + Queue() + boolean isEmpty() + void enqueue(ItemType z) + void dequeue() + ItemType front() Queue q1, q2; String str; q1 = new Queue (); q1.enqueue( “droopy” ); q1.enqueue( “rupie” ); q1.enqueue( “snoopy” ); q1.dequeue(); q1.enqueue( “soupy” ); str = q1.front(); q1.enqueue( “loopy” ); q1.enqueue( “goopy” ); q1.dequeue(); q1.enqueue( q1.front() ); q2 = new Queue(); while ( !q1.isEmpty() ) { q2.enqueue( q1.front() ); q1.dequeue(); }

5 In the computing environment...  is queued.  Graphical User Interfaces (GUIs) depend upon ______________.  Documents sent to the printer are ___________ (queued).  Data transferred to a stream are ____________ (queued).  Machine instructions are executed using a sophisticated queue, known as a ____________. The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

6 Suppose you inherit LinkedList. Which end of the list is the front of the queue? How does the code for enqueue differ from dequeue? How many iterators? Double or single linking? Suppose you use an array to store the queue content. What happens when the number of enqueues exceeds the array length? The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

7 Bounded List Representation This is best accomplished by treating an array like it is a circular structure, also called a ring buffer. [0] [1] [2] [3] [size] 1 3 newest oldest oldest is the index of the queue’s front newest is the index of the queue’s rear Note: This representation requires n+1 cells for a queue of length n. The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.


Download ppt "A queue is a linear, homogeneous, container that stores and dispenses its content in a FIFO manner. FIFO - First In First Out The first (most distant)"

Similar presentations


Ads by Google