diff --git a/VL13/Aufgabe01/Aufgabe01.java b/VL13/Aufgabe01/Aufgabe01.java new file mode 100644 index 0000000..ff784e7 --- /dev/null +++ b/VL13/Aufgabe01/Aufgabe01.java @@ -0,0 +1,74 @@ +package VL13.Aufgabe01; + +import java.util.Stack; + +public class Aufgabe01 { + public static void main(String[] args) { + + // Integer Stack + System.out.printf("\n%s:\n", "Integer Stack"); + + IntegerStack integerStack = new IntegerStack(); + integerStack.push(7); + integerStack.push(3); + integerStack.push(8); + integerStack.push(2); + System.out.println(integerStack.pop()); + System.out.println(integerStack.pop()); + integerStack.push(7); + System.out.println(integerStack.pop()); + System.out.println(integerStack.pop()); + + // Generic Stack (Integer) + System.out.printf("\n%s:\n", "Generic Stack (Integer)"); + + GenericStack gIntegerStack = new GenericStack(); + gIntegerStack.push(7); + gIntegerStack.push(3); + gIntegerStack.push(8); + gIntegerStack.push(2); + System.out.println(gIntegerStack.pop()); + System.out.println(gIntegerStack.pop()); + gIntegerStack.push(7); + System.out.println(gIntegerStack.pop()); + System.out.println(gIntegerStack.pop()); + + // Generic Stack (String) + System.out.printf("\n%s:\n", "Generic Stack (String)"); + + GenericStack gStringStack = new GenericStack(); + gStringStack.push("Hallo"); + gStringStack.push("Welt"); + gStringStack.push("!"); + + System.out.println(gStringStack.pop()); + System.out.println(gStringStack.pop()); + System.out.println(gStringStack.pop()); + + // Stack (Integer) + System.out.printf("\n%s:\n", "Stack (Integer)"); + + Stack nIntegerStack = new Stack(); + nIntegerStack.push(7); + nIntegerStack.push(3); + nIntegerStack.push(8); + nIntegerStack.push(2); + System.out.println(nIntegerStack.pop()); + System.out.println(nIntegerStack.pop()); + nIntegerStack.push(7); + System.out.println(nIntegerStack.pop()); + System.out.println(nIntegerStack.pop()); + + // Stack (String) + System.out.printf("\n%s:\n", "Stack (String)"); + + Stack stringStack = new Stack(); + stringStack.push("Hallo"); + stringStack.push("Welt"); + stringStack.push("!"); + + System.out.println(stringStack.pop()); + System.out.println(stringStack.pop()); + System.out.println(stringStack.pop()); + } +} diff --git a/VL13/Aufgabe01/GenericStack.java b/VL13/Aufgabe01/GenericStack.java new file mode 100644 index 0000000..9cfa5e2 --- /dev/null +++ b/VL13/Aufgabe01/GenericStack.java @@ -0,0 +1,67 @@ +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; + } +} diff --git a/VL13/Aufgabe01/IntegerStack.java b/VL13/Aufgabe01/IntegerStack.java new file mode 100644 index 0000000..f1a06b8 --- /dev/null +++ b/VL13/Aufgabe01/IntegerStack.java @@ -0,0 +1,64 @@ +package VL13.Aufgabe01; + +/** + * A integer stack. + */ +public class IntegerStack { + StackItem start; + + private class StackItem { + int value; + StackItem next; + StackItem prev; + + StackItem(int value) { + this.value = value; + } + } + + /** + * Method to push a value to the stack. + * + * @param value The value to store on the stack. + */ + public void push(int 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 int 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 && item == start) { + start = null; + } else { + item.prev.next = null; + } + + // return the value of the last item of the stack. + return item.value; + } +}