BELOW YOU WILL FIND THE CODE FOR THE
Stack 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 Stack
*
* @author Appears in Litvin's "Be Prepared"
*/
public interface Stack
{
boolean isEmpty();
void push(Object x);
Object pop();
Object peekTop();
}
//===============================================
import java.util.ArrayList;
/**
* Implementation of a Stack using an ArrayList
*
* @author Appears in Litvin's "Be Prepared"
*/
public class ArrayStack implements Stack
{
private ArrayList items;
// constructor
public ArrayStack() { items = new ArrayList(); }
public boolean isEmpty() { return items.size() == 0; }
public void push(Object obj) { items.add(obj); }
public Object pop() { return items.remove(items.size() - 1); }
public Object peekTop() { return items.get(items.size() -1); }
}