BELOW YOU WILL FIND THE CODE FOR THE
Queue Interface and a class that implements that Interface
- Copy the Interface code into a new Interface
- Copy the Class code into a new Class
//=============================================================
/**
* Interface for a Queue
*
* @author Appears in Litvin's "Be Prepared"
*/
public interface Queue
{
boolean isEmpty();
void enqueue(Object x);
Object dequeue();
Object peekFront(); // returns the first item without removing it
}
//===============================================
import java.util.LinkedList;
/**
* This class implements the Queue interface using a linked list.
*
* @author Appears in Litvin's "Be Prepared"
*/
public class ListQueue implements Queue
{
private LinkedList items;
// Constructor initializes a new linked list
public ListQueue() { items = new LinkedList(); }
public boolean isEmpty() { return items.size() == 0;}
public void enqueue(Object obj) { items.addLast(obj); }
public Object dequeue() { return items.removeFirst(); }
public Object peekFront() { return items.getFirst(); }
}