11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Sec. 4.2 Stacks 119/** Array-based stack implement<strong>at</strong>ion */class AStack implements Stack {priv<strong>at</strong>e st<strong>at</strong>ic final int defaultSize = 10;priv<strong>at</strong>e int maxSize;priv<strong>at</strong>e int top;priv<strong>at</strong>e E [] listArray;// Maximum size of stack// Index for top Object// Array holding stack/** Constructors */AStack() { this(defaultSize); }@SuppressWarnings("unchecked") // Generic array alloc<strong>at</strong>ionAStack(int size) {maxSize = size;top = 0;listArray = (E[])new Object[size]; // Cre<strong>at</strong>e listArray}/** Reinitialize stack */public void clear() { top = 0; }/** Push "it" onto stack */public void push(E it) {assert top != maxSize : "Stack is full";listArray[top++] = it;}/** Remove <strong>and</strong> top element */public E pop() {assert top != 0 : "Stack is empty";return listArray[--top];}/** @return Top element */public E topValue() {assert top != 0 : "Stack is empty";return listArray[top-1];}/** @return Stack size */public int length() { return top; }Figure 4.19 Array-based stack class implement<strong>at</strong>ion.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!