|
net.datastructures - version 5.0 | |||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||
java.lang.Objectnet.datastructures.ArrayStack<E>
public class ArrayStack<E>
Implementation of the stack ADT using a fixed-length array. An exception is thrown if a push operation is attempted when the size of the stack is equal to the length of the array. This class includes the main methods of the built-in class java.util.Stack. //end#fragment ArrayStack
//begin#fragment ArrayStack| Field Summary | |
|---|---|
protected int |
capacity
Length of the array used to implement the stack. |
static int |
CAPACITY
Default array capacity. |
protected E[] |
S
Array used to implement the stack. |
protected int |
top
Index of the top element of the stack in the array. |
| Constructor Summary | |
|---|---|
ArrayStack()
Initializes the stack to use an array of default length. |
|
ArrayStack(int cap)
Initializes the stack to use an array of given length. |
|
| Method Summary | |
|---|---|
boolean |
isEmpty()
Testes whether the stack is empty. |
static void |
main(String[] args)
Test our program by performing a series of operations on stacks, printing the operations performed, the returned elements and the contents of the stack involved, after each operation. |
E |
pop()
Removes the top element from the stack. |
void |
push(E element)
Inserts an element at the top of the stack. |
int |
size()
Returns the number of elements in the stack. |
void |
status(String op,
Object element)
Prints status information about a recent operation and the stack. |
E |
top()
Inspects the element at the top of the stack. |
String |
toString()
Returns a string representation of the stack as a list of elements, with the top element at the end: [ ... , prev, top ]. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
protected int capacity
public static final int CAPACITY
protected E[] S
protected int top
| Constructor Detail |
|---|
public ArrayStack()
public ArrayStack(int cap)
cap - length of the array.| Method Detail |
|---|
public int size()
size in interface Stack<E>public boolean isEmpty()
isEmpty in interface Stack<E>
public void push(E element)
throws FullStackException
push in interface Stack<E>element - element to be inserted.
FullStackException - if the array storing the elements is full.
public E top()
throws EmptyStackException
top in interface Stack<E>EmptyStackException - if the stack is empty.
public E pop()
throws EmptyStackException
pop in interface Stack<E>EmptyStackException - if the stack is empty.public String toString()
toString in class Object
public void status(String op,
Object element)
op - operation performedelement - element returned by the operationpublic static void main(String[] args)
|
net.datastructures - version 5.0 | |||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||