Java Reference
In-Depth Information
s.push(ÐAÑ);
s.push(ÐBÑ);
s.push(ÐCÑ);
// The following loop prints C, B, and A
while (s.size() > 0)
System.out.println(s.pop());
The Stack class in the Java library uses an array to implement a stack. Exercise
P15.11 shows how to use a linked list instead.
687
688
The implementations of a queue in the standard library are designed for use with
multithreaded programs. However, it is simple to implement a basic queue yourself:
public class LinkedListQueue
{
/**
Constructs an empty queue that uses a linked list.
*/
public LinkedListQueue()
{
list = new LinkedList();
}
/**
Adds an element to the tail of the queue.
@param element the element to add
*/
public void add(Object element)
{
list.addLast(element);
}
/**
Removes an element from the head of the queue.
@return the removed element
*/
public Object remove()
{
return list.removeFirst();
}
/**
Gets the number of elements in the queue.
@return the size
*/
int size()
Search WWH ::




Custom Search