Java Reference
In-Depth Information
The definition of a simple Queue class that is based on a linked list is given in Dis-
play 15.29. A short demonstration program is given in Display 15.30. We have not
made our queue a generic queue to keep the definition simple, but it would be routine
to replace the data type String with a type parameter.
Queue
A queue is a first-in/first-out data structure; that is, the data items are removed from the
queue in the same order that they were added to the queue.
Self-Test Exercise
18. Complete the definition of the method addToBack in Display 15.29.
Display 15.29 A Queue Class (part 1 of 2)
1 public class Queue
2{
3
private class Node
4
{
5
private String item;
6
private Node link;
7
public Node( )
8
{
9
item = null ;
10
link = null ;
11
}
12
public Node(String newItem, Node linkValue)
13
{
14
item = newItem;
15
link = linkValue;
16
}
17
} //End of Node inner class
18
private Node front;
19
private Node back;
20
public Queue( )
21
{
22
front = null ;
23
back = null ;
24
}
(continued)
Search WWH ::




Custom Search