Semester Genap 2010/2011: Beni Suranto, ST
Semester Genap 2010/2011: Beni Suranto, ST
Semester Genap 2010/2011: Beni Suranto, ST
• 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
isEmpty()
// Determines whether a queue is empty
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
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
8 A-15
Array implementation of queues
front = 0 rear = 3
Initial queue: 17 23 97 44
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 #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
last
first
44 97 23 17
21
Queue implementation details
22
An Array-Based Implementation
• A circular array
eliminates the
problem of
rightward drift
8 A-23
An Array-Based Implementation
8 A-24
An Array-Based Implementation
8 A-25
An Array-Based Implementation
8 A-26
An Array-Based Implementation
8 A-27
An Array-Based Implementation
8 A-28
So, how do I enqueue and
dequeue from a circular array-
based queue?
8 A-29
An Array-Based Implementation
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
8 A-32
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