package VL13.Aufgabe01; /** * A generic stack. */ public class GenericStack { StackItem start; /** * A stack item to store a single item on the stack. */ private class StackItem { E value; StackItem next; StackItem prev; StackItem(E value) { this.value = value; } } /** * Method to push a value to the stack. * * @param value The value to store on the stack. */ public void push(T value) { if (start == null) { start = new StackItem(value); } else { StackItem item = start; // get the last item of the stack. while (item.next != null) { item = item.next; } // create a new stack item to store the value. item.next = new StackItem(value); item.next.prev = item; } } /** * Method to pop a value from the stack. * * @return The value from stack. */ public T pop() { StackItem item = start; // get the last item of the stack. while (item.next != null) { item = item.next; } // remove last item from stack. if (item.prev == null) { start = null; } else { item.prev.next = null; } // return the value of the last item of the stack. return item.value; } }