Iz E-študij, proste zakladnice študentskega znanja
//Sklad s poljem
public class StackArray implements Stack {
static final int DEFAULT_LENGTH = 100 ;
private Object elements[] ;
private int stTop ; ;
public StackArray() {
this(DEFAULT_LENGTH) ;
}
public StackArray(int noElem) {
elements = new Object[noElem] ;
makenull() ;
}
public void makenull() {
stTop = -1 ;
}
public boolean empty() {
return stTop ==-1 ;
}
public Object top() {
if (empty()) // error
; // throw new UnderflowException ;
return elements[stTop] ;
}
public void push(Object x) {
stTop++ ;
if (stTop >= elements.length) //error: full
; // throw new OverflowException ;
else {
elements[stTop] = x ;
}
}
public void pop() {
if (empty()) // error
; // throw new UnderflowException ;
else stTop-- ;
}
public static void main(String[] args) {
StackArray s = new StackArray();
System.out.println("Testing class "+s.getClass() );
int i ;
for (i=1 ; i <= 5 ; i++) {
s.push(new Integer(i));
System.out.println("Pushing " + i) ;
}
while (!s.empty()) {
System.out.println("Poping "+s.top());
s.pop() ;
}
}
}