/** * Copyright (c): Uwe Schmidt, FH Wedel * * You may study, modify and distribute this source code * FOR NON-COMMERCIAL PURPOSES ONLY. * This copyright message has to remain unchanged. * * Note that this document is provided 'as is', * WITHOUT WARRANTY of any kind either expressed or implied. */ /** * * @author Uwe Schmidt * * ein einfacher stack * der das interface implementiert * * Implementierung aus der SimpleStack Klasse * uebernommen */ //-------------------- public class Stack1 implements StackInterface { //-------------------- // die Datenfelder fuer die Implementierung private Object[] elems; private int top; //-------------------- // die Konstruktoren public Stack1(int max) { elems = new Object[max]; top = 0; } public Stack1() { this(20); } //-------------------- // alle Funktionsruempfe //-------------------- public boolean isEmpty() { return top == 0; } //-------------------- public Object top() { assert top > 0 : "Stack1.top: empty stack" ; return elems[top - 1]; } //-------------------- public void push(Object o) { elems[top++] = o; } //-------------------- public void pop() { assert top > 0 : "Stack1.pop: empty stack"; elems[--top] = null; } //-------------------- // // auch die Vorbedingungen muessen implementiert werden // Gefahr: Codeverdopplung public boolean prePop() { return ! isEmpty(); } public boolean preTop() { return ! isEmpty(); } } //--------------------