Program 21.3: A generic Queue class
Stacks are last-in, first-out, LIFO for short. Another common data structure is the queue. The queue is like a stack except that elements are put in at the bottom of the list and come off the top of the list. Itıs first-in, first out or FIFO. There is no built-in queue data structure in java but itıs easy to implement one as a subclass of Vector. Program 21.3 is one such implementation.
import java.util.Vector;
import java.util.EmptyStackException;
public class Queue extends Vector {
public void add(Object o) {
addElement(o);
}
public Object remove() {
Object o = peek();
removeElementAt(0);
return o;
}
public Object peek() {
if (size() == 0) throw new EmptyStackException();
return elementAt(0);
}
public boolean empty() {
return size() == 0;
}
}
Copyright 1996 Elliotte Rusty Harold
elharo@sunsite.unc.edu
This Chapter
Examples
Home