All Packages  Class Hierarchy  Previous  Next  Index

Stack Examples

Stack 1 - Construction, enumeration, pushing, popping.

Stack 2 - Using a Vector as the underlying Sequence.


Stack1 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.

import java.util.Enumeration;
import COM.objectspace.jgl.*;

/**
 * Construction, enumeration, pushing, popping.
 *
 * @see COM.objectspace.jgl.Stack
 * @version 2.0.2
 * @author ObjectSpace, Inc.
 */

public class Stack1
  {
  public static void main( String[] args )
    {
    // Use an Slist as the underlying data structure.
    Stack stack = new Stack();
    stack.push( "bat" );
    stack.push( "cat" );
    stack.push( "dog" );

    System.out.println( "Print the Stack." );
    System.out.println( stack );
    System.out.println();

    System.out.println( "Non-destructively enumerate the Stack." );
    Enumeration e = stack.elements();
    while ( e.hasMoreElements() )
      System.out.println( e.nextElement() );
    System.out.println();

    System.out.println( "Pop and print each element." );
    while ( !stack.isEmpty() )
      System.out.println( stack.pop() );
    }
  }

Stack1 Example Output

Print the Stack.
Stack( Array( bat, cat, dog ) )

Non-destructively enumerate the Stack.
bat
cat
dog

Pop and print each element.
dog
cat
bat

Stack2 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.

import COM.objectspace.jgl.*;

/**
 * Using a Vector as the underlying Sequence.
 *
 * @see COM.objectspace.jgl.Stack
 * @version 2.0.2
 * @author ObjectSpace, Inc.
 */

public class Stack2
  {
  public static void main( String[] args )
    {
    // Use a Array as the underlying data structure.
    Stack stack = new Stack( new Array() );
    stack.push( "bat" );
    stack.push( "cat" );
    stack.push( "dog" );

    System.out.println( "Print the Stack." );
    System.out.println( stack );
    }
  }

Stack2 Example Output

Print the Stack.
Stack( Array( bat, cat, dog ) )

All Packages  Class Hierarchy  Previous  Next  Index