/*
 * File: Stack.java
 * 
 * Date		Author		Changes
 * ----------------------------------------------------------------
 * 12 Feb 1998	M Ravishankar	Created
 */

/**
 * Class defining a stack of characters, of arbitrary depth:
 * <pre>
 * Usage:
 *   Stack s = new Stack();
 *   char c;
 * 
 *   s.push ('a');	// push 'a' onto the stack
 *   s.push ('b');	// push 'b' onto the stack
 *   c = s.pop();	// c contains 'b'
 *   c = s.pop();	// c now contains 'a'
 * </pre>
 * 
 * (This is a simplified version of class Stack in java.util.  Note that
 * the above interface has many holes.  For example, it might be useful to
 * provide a <code>count</code> method that returns the current number of
 * items that have been pushed onto a given Stack.
**/

class Stack
{
  /**
   * A Stack is represented by the variable <code>buf</code> of class
   * <code>StringBuffer</code>, which is defined in package <code>java.lang</code>.
   **/
  private StringBuffer buf;	// A <em>reference</em> to a <code>StringBuffer</code>.
  
  /**
   * Push the given character argument onto the top of a stack (Q: which stack??)
   **/
  public void push (char item)
    {
      buf.append (item);
    }
  
  /**
   * Pop the topmost character off a Stack and return it.
   * Bug-note: No check for attempting to pop off an already empty Stack!!
   **/
  public char pop ()
    {
      int k;
      
      k = buf.length() - 1;
      char c = buf.charAt(k);	// Extract char at the end of buffer
      buf.setLength (k);	// Reduce buffer length by 1
      
      return c;
    }

  /**
   * Constructor method called whenever a new Stack is created.  Need to initialize
   * member variable <code>buf</code> with a new object of type <code>StringBuffer</code>.
   **/
  public Stack () 
    {
      buf = new StringBuffer();
    }
}
