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 - Fast In First Out The first (most distant) item.

Similar presentations


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

1 A queue is a linear, homogeneous, container that stores and dispenses its content in a FIFO manner. FIFO - Fast In First Out The first (most distant) item inserted, and not yet removed, will be the first (next) item dispensed. Real World Examples  assembly lines  waiting line 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 front dequeue enqueue The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

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

4 Queue + Queue() + boolean isEmpty() + void enqueue(Object z) + void dequeue() + Object front() Example Queue q1, q2; String str; q1 = new Queue(); q1.enqueue( “droopy” ); q1.enqueue( “rupie” ); q1.enqueue( “snoopy” ); q1.dequeue(); q1.enqueue( “soupy” ); str = (String) 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(); } The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

5 In the computing environment...  Email is queued.  Graphical User Interfaces (GUIs) depend upon event queues.  Documents sent to the printer are spooled (queued).  Data transferred to a stream are buffered (queued).  Machine instructions are executed using a sophisticated queue, known as a pipeline. 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 - Fast In First Out The first (most distant) item."

Similar presentations


Ads by Google