Queue Data Structure
Queue Data Structure
23/11/09 http://technoexplore.blogspot.com 1
What is queue?
A queue is a linier data structure.
The concept is quite similar with
stack.
additions are made at the end or tail
of the queue while removals are
made from the front or head of the
queue.
Access system a queue is referred to
a FIFO structure (First-In First-Out)
23/11/09 http://technoexplore.blogspot.com 2
Queue operations
Add : adds a new node
Add(X,Q) add the value X to the tail of queue
Remove : removes a node
Remove(Q) removes the head node and returns
its value
IsEmpty : reports whether the queue is
empty
IsEmpty(Q) report whether the queue Q is empty
IsFull : reports whether the queue is
full
IsFull(Q) report whether the queue Q is full
Initialize : creates/initializes the queue
Initialize(Q) create a new empty queue named Q
Destroy : deletes the contents of the
queue (may be implemented by re-
initializing the queue)
23/11/09 Destroy(Q) deletes the contents of the queue Q
http://technoexplore.blogspot.com 3
Illustration/example
Operation Queue’s contents Return value
1. Initialiaze(S) <empty> -
2. Add(A,Q) A -
3. Add(B,Q) A B -
4. Add(C,Q) A B C -
5. Remove(Q) B C A
6. Add(D,Q) B C D -
7. Remove(Q) C D B
8. Remove(Q) D C
9. Remove(Q) <empty> D
23/11/09 http://technoexplore.blogspot.com 4
Exercise: Queue Operation
What would the contents of a queue be
after the following operations?
Initialise(Q)
Add(A,Q)
Add(F,Q)
Add(X,Q)
Remove(Q)
Add(B,Q)
Remove(Q)
Remove(Q)
B
23/11/09 http://technoexplore.blogspot.com 5
Storing a queue in a static data
structure
This implementation stores the queue in an array.
The array indices at which the head and tail of the
queue are currently stored must be maintained.
The head of the queue is not necessarily at index 0.
The array can be a “circular array” – the queue “wraps
round” if the last index of the array is reached.
Example – storing a queue in an array of length 5
23/11/09 http://technoexplore.blogspot.com 6
Storing a queue in a static data
structure (2)
23/11/09 http://technoexplore.blogspot.com 8
Adding a node (Add) in a dynamic
data structure
The new node is to be added at the tail of the queue.
The reference Queue.Tail should point to the new
node, and the NextNode reference of the node
previously at the tail of the queue should point to
the DataItem of the new node.
23/11/09 http://technoexplore.blogspot.com 9
Removing a node (Remove) in a
dynamic data structure
The value of Queue.Head.DataItem is returned. A
temporary reference Temp is declared and set to point
to head node in the queue (Temp = Queue.Head).
Queue.Head is then set to point to the second node
instead of the top node.
The only reference to the original head node is now
Temp and the memory used by this node can then be
freed.
23/11/09 http://technoexplore.blogspot.com 10
Queue Implementation
23/11/09 http://technoexplore.blogspot.com 11
Queue Implementation in Java
The Java Collections Framework
in the most recent version of Java
now includes queue classes.
As you did for the stack, you will
create your own Queue class in
order to learn how a queue is
implemented.
Your class will again be a bit
simpler than the Collections
Framework one but it will do
essentially the same job
23/11/09 http://technoexplore.blogspot.com 12
The Queue Class
Since you implemented your stack as a static
structure, you will learn how to implement a
dynamic structure for your Queue
23/11/09 http://technoexplore.blogspot.com 16
Using a Queue
To use the Queue class, you need to know how to
write code to call the Queue operations, for
example to add data to the Queue.
Remember that the Queue can hold any kind of
data. The following test class shows how to use a
/**
Queue to hold String objects.
/
/*class QueueTester. */
public class QueueTester
{
private Queue queue;
public QueueTester(){
queue = new Queue();
}
public QueueTester(Queue queue){
this.queue = queue;
} void addString(String str)
void removeString()
} void checkIfEmpty()
23/11/09 http://technoexplore.blogspot.com void listStringsInQueue() 17
Using a Queue (2)
/* check if queue is empty */
/* add item to queue */ public void checkIfEmpty() {
public void addString(String if (queue.isEmpty())
str) { System.out.println("Queue empty");
queue.add(str); else
System.out.println("Queue is not
System.out.println("Added empty");
new string"); }
}
/* list the strings in queue */
public void listStringsInQueue() {
/* remove item from queue */ if (queue.isEmpty()) {
public void removeString() { System.out.println("Queue
String result = (String) empty");
queue.remove(); }
else {
if (result!=null) System.out.println("Strings in
System.out.println("String is queue are: ");
:" + result); System.out.println();
Node node = queue.head;
else
while (node != null){
String item =
System.out.println("Remove (String)node.dataItem;
was unsuccessful"); System.out.println(item);
} node = node.nextNode;
}
23/11/09 http://technoexplore.blogspot.com
System.out.println(); 18
}
}
Exercise: Using a Queue
Create a new BlueJ project called queues and create new
classes Node, Queue and QueueTester using the above
code.
Create a new instance of Queue.
Create a new instance of QueueTester and select your
Queue instance in the object bench as the parameter in the
constructor. This means that you will be testing the Queue
you created in the previous step.
Call the checkIfEmpty method of your QueueTester.
23/11/09 http://technoexplore.blogspot.com 20
EXERCISE: A practical
application of the Queue class
A queue is a useful data structure for holding data which
should be processed in the order it is created, but which
cannot always be processed straight away. A typical
application might be a messaging system. In the following
example, messages are received in the order they were
sent.
The classes involved are Message, MessageSender and
MessageReceiver:
A Message object has a sender, a recipient, a content string
and a date.
A Message is placed in a Queue by a MessageSender
object.
A Message is removed from the queue by a
MessageReceiver object, which can also display the
contents of the Queue.
The Queue class you have created in this chapter can
hold any type of object, including Messages, so you can
use it in this example as it is.
Add the following classes to your queues project:
23/11/09 http://technoexplore.blogspot.com 21
EXERCISE: A practical application (the code)
Message.Java
Message.Java
import java.text.*;
import java.util.Date;
/** class Message */
public class Message
{
public String sender;
public String recipient;
public String content;
public Date date;
/**class MessageSender. */
public class MessageSender
{
/** places a message on a specified queue */
public void sendMessage(String sender, String recipient, String content, Queue q)
{
Message m = new Message(sender, recipient, content);
if(!q.isFull()){
q.add(m);
System.out.println("Message placed on queue");
}
else
System.out.println("Cannot send - queue is full");
}
}
23/11/09 http://technoexplore.blogspot.com 23
EXERCISE: A practical application (the code)
MessageReceiver.Java
23/11/09 http://technoexplore.blogspot.com 25