APCS Java Subset

ap
Class ArrayStack

java.lang.Object
  extended byap.ArrayStack
All Implemented Interfaces:
Stack

public class ArrayStack
extends java.lang.Object
implements Stack

A simple yet completely functional implementation of the Stack interface. The interface is part of the AP subset and is testable. This implementation is not part of the subset, but is useful in a classroom setting.

All stack functions execute in O(1) or constant time amortized over several stack operations. This is because the underlying storage is java.util.ArrayList which supports constant time access, add (to end), and remove (from end).

This implementation is provided at apcentral.


Constructor Summary
ArrayStack()
          Construct an initially empty stack.
 
Method Summary
 boolean isEmpty()
          Returns true if this stack is empty, otherwise returns false.
 java.lang.Object peekTop()
          Returns the top element of the stack without popping it.
 java.lang.Object pop()
          Pops and returns the top element of the stack.
 void push(java.lang.Object x)
          Push an element onto the top of this stack.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ArrayStack

public ArrayStack()
Construct an initially empty stack.

Method Detail

push

public void push(java.lang.Object x)
Description copied from interface: Stack
Push an element onto the top of this stack.

Specified by:
push in interface Stack
Parameters:
x - is the object pushed onto this stack.

pop

public java.lang.Object pop()
Description copied from interface: Stack
Pops and returns the top element of the stack.

Specified by:
pop in interface Stack
Returns:
the just-popped element of the stack
See Also:
Stack.peekTop()

peekTop

public java.lang.Object peekTop()
Description copied from interface: Stack
Returns the top element of the stack without popping it.

Specified by:
peekTop in interface Stack
Returns:
the top element of the stack
See Also:
Stack.pop()

isEmpty

public boolean isEmpty()
Description copied from interface: Stack
Returns true if this stack is empty, otherwise returns false.

Specified by:
isEmpty in interface Stack
Returns:
true if this stack is empty, otherwise return false.

unofficial documentation for the APCS Java Subset