<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">/**
 * The Stack interface and some Stack implementations.
 * 
 * @author Paul Chew for CS211, Oct 2006
 */

public interface Stack {
    public void push (Object x);
    public Object pop ();
    public Object peek ();
    public boolean isEmpty ();
    public void makeEmpty ();
}

class ArrayStack implements Stack {
    
    private Object[] array;    // Array that holds the Stack
    private int index = 0;     // First empty slot in Stack
    
    public ArrayStack (int maxSize) {array = new Object[maxSize];}
    
    public void push (Object x) {array[index++] = x;}
    public Object pop () {return array[--index];}
    public Object peek () {return array[index-1];}
    public boolean isEmpty () {return index == 0;}
    public void makeEmpty () {index = 0;}
}

class ListStack implements Stack {
    
    private Node head = null;  // Head of list that holds the Stack
    
    public void push (Object x) {head = new Node(x, head);}
    public Object pop () {Node temp = head; head = head.next; return temp.data;}
    public Object peek () {return head.data;}
    public boolean isEmpty () {return head == null;}
    public void makeEmpty () {head = null;}
}

class Node {
    public Object data;       // Data field
    public Node next;         // Next node of list
    
    public Node (Object data, Node next) {this.data = data; this.next = next;}
}</pre></body></html>