Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Semester Genap 2010/2011: Beni Suranto, ST

Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

Semester Genap 2010/2011

Beni Suranto, ST.


beni.suranto@fti.uii.ac.id
The Abstract Data Type Queue

• A queue
– New items enter at the back, or rear, of the queue
– Items leave from the front of the queue
– First-in, first-out (FIFO) property
• The first item inserted into a queue is the first
item to leave
• This is the same as waiting in a line. First
person in line gets served first.

8 A-2
The Abstract Data Type Queue
• ADT queue operations
– Create an empty queue
– Determine whether a queue is empty
– Add a new item to the queue
– Remove from the queue the item that was added
earliest
– Remove all the items from the queue
– Retrieve from the queue the item that was added
earliest

8 A-3
The Abstract Data Type Queue
• Queues
– Are appropriate for many real-world
situations
• Example: A line to buy a movie ticket
– Have applications in computer science
• Example: A request to print a document
• A simulation
– A study to see how to reduce the wait involved in
an application

8 A-4
The Abstract Data Type Queue

• Pseudocode for the ADT queue operations


createQueue()
// Creates an empty queue.

isEmpty()
// Determines whether a queue is empty

enqueue(newItem) throws QueueException


// Adds newItem at the back of a queue. Throws
// QueueException if the operation is not
// successful

8 A-5
The Abstract Data Type Queue
• Pseudocode for the ADT queue operations
(Continued)
dequeue() throws QueueException
// Retrieves and removes the front of a queue.
// Throws QueueException if the operation is
// not successful.

dequeueAll()
// Removes all items from a queue

peek() throws QueueException


// Retrieves the front of a queue. Throws
// QueueException if the retrieval is not
// successful

8 A-6
Recognizing Palindromes
• A palindrome
– A string of characters that reads the same from left
to right as its does from right to left
• To recognize a palindrome, a queue can be
used in conjunction with a stack
– A stack can be used to reverse the order of
occurrences
– A queue can be used to preserve the order of
occurrences

8 A-7
Recognizing Palindromes

• A nonrecursive
recognition algorithm for
palindromes
– As you traverse the
character string from left
to right, insert each
character into both a
queue and a stack
– Compare the characters
at the front of the queue
and the top of the stack

8 A-8
Implementations of the ADT
Queue
• A queue can have either
– An array-based implementation
– A reference-based implementation
A Reference-Based
Implementation
• Possible implementations of a queue
– A linear linked list with two external references
• A reference to the front
• A reference to the back

8 A-10
A Reference-Based
Implementation
• Possible implementations of a queue
(Continued)
– A circular linked list with one external reference
• A reference to the back

8 A-11
A Reference-Based
Implementation
Inserting an item into a nonempty queue

8 A-12
A Reference-Based
Implementation
Inserting an item into an empty queue: a) before insertion; b) after insertion

8 A-13
A Reference-Based
Implementation
Deleting an item from a queue of more than one item

8 A-14
An Array-Based Implementation

a) A naive array-based implementation of a queue; b) rightward drift can cause


the queue to appear full

8 A-15
Array implementation of queues

front = 0 rear = 3

Initial queue: 17 23 97 44

After insertion: 17 23 97 44 333

After deletion: 23 97 44 333

front = 1 rear = 4
• Notice how the array contents “crawl” to the right as elements are inserted
and deleted
• This will be a problem after a while!
16
Circular arrays
• We can treat the array holding the queue elements as circular (joined
at the ends)

0 1 2 3 4 5 6 7
myQueue: 44 55 11 22 33

rear = 1 front = 5
• Elements were added to this queue in the order 11, 22, 33, 44, 55,
and will be removed in the same order
• Use: front = (front + 1) % myQueue.length;
and: rear = (rear + 1) % myQueue.length;
Full and empty queues
• If the queue were to become completely full, it would look like this:
0 1 2 3 4 5 6 7
myQueue: 44 55 66 77 88 11 22 33

rear = 4 front = 5
• If we were then to remove all eight elements, making the queue completely
empty, it would look like this:
0 1 2 3 4 5 6 7
myQueue:

rear = 4 front = 5

This is a problem!
18
Full and empty queues: solutions

• Solution #1: Keep an additional variable


0 1 2 3 4 5 6 7
myQueue: 44 55 66 77 88 11 22 33

count = 8 rear = 4 front = 5

• Solution #2: (Slightly more efficient) Keep a gap between elements: consider
the queue full when it has n-1 elements

0 1 2 3 4 5 6 7
myQueue: 44 55 66 77 11 22 33

rear = 3 front = 5

19
Enqueueing a node

Node to be
last enqueued
first

44 97 23 17

To enqueue (add) a node:


Find the current last node
Change it to point to the new last node
Change the last pointer in the list header
Dequeueing a node

last
first

44 97 23 17

• To dequeue (remove) a node:


– Copy the pointer from the first node into the header

21
Queue implementation details

• With an array implementation:


– you can have both overflow and underflow
– you should set deleted elements to null

• With a linked-list implementation:


– you can have underflow
– overflow is a global out-of-memory condition
– there is no reason to set deleted elements to null

22
An Array-Based Implementation

• A circular array
eliminates the
problem of
rightward drift

A circular implementation of a queue

8 A-23
An Array-Based Implementation

The effect of some operations of the queue in Figure 8-8

8 A-24
An Array-Based Implementation

• A problem with the circular array


implementation
– front and back cannot be used to distinguish
between queue-full and queue-empty conditions

8 A-25
An Array-Based Implementation

a) front passes back when the queue becomes empty

8 A-26
An Array-Based Implementation

b) back catches up to front when the queue becomes full

8 A-27
An Array-Based Implementation

• To detect queue-full and queue-empty


conditions
– Keep a count of the queue items
• To initialize the queue, set
– front to 0
– back to MAX_QUEUE – 1
– count to 0

8 A-28
So, how do I enqueue and
dequeue from a circular array-
based queue?

8 A-29
An Array-Based Implementation

• Inserting into a queue


back = (back+1) % MAX_QUEUE;
items[back] = newItem;
++count;
• Deleting from a queue
front = (front+1) % MAX_QUEUE;
--count;

8 A-30
Using a List (Vector or ArrayList)
How do I implement enqueue
and dequeue?

8 A-31
An Implementation That Uses the ADT List

• If the item in position 1 of a list list


represents the front of the queue, the
following implementations can be used
– dequeue()
list.remove(1)
– peek()
list.get(1)

8 A-32
An Implementation That Uses the ADT List

• If the item at the end of the list represents the


back of the queue, the following
implementations can be used
– enqueue(newItem)
list.add(list.size()+1, newItem)

An implementation that uses the ADT list

8 A-33
The Java Collections Framework
Interface Queue
• JCF has a queue interface called Queue
• Derived from interface Collection
• Adds methods:
– element: retrieves, but does not remove head
– offer: inserts element into queue
– peek: retrieves, but does not remove head
– poll: retrieves and removes head
– remove: retrieves and removes head

You might also like