Vorlesung 13 / Aufgabe 1

This commit is contained in:
Sebastian Brosch 2024-05-07 20:43:58 +02:00
parent 12997ba989
commit f6a3b314cb
3 changed files with 205 additions and 0 deletions

View File

@ -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<Integer> gIntegerStack = new GenericStack<Integer>();
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<String> gStringStack = new GenericStack<String>();
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<Integer> nIntegerStack = new Stack<Integer>();
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<String> stringStack = new Stack<String>();
stringStack.push("Hallo");
stringStack.push("Welt");
stringStack.push("!");
System.out.println(stringStack.pop());
System.out.println(stringStack.pop());
System.out.println(stringStack.pop());
}
}

View File

@ -0,0 +1,67 @@
package VL13.Aufgabe01;
/**
* A generic stack.
*/
public class GenericStack<T> {
StackItem<T> start;
/**
* A stack item to store a single item on the stack.
*/
private class StackItem<E> {
E value;
StackItem<E> next;
StackItem<E> 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<T>(value);
} else {
StackItem<T> 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<T>(value);
item.next.prev = item;
}
}
/**
* Method to pop a value from the stack.
*
* @return The value from stack.
*/
public T pop() {
StackItem<T> 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;
}
}

View File

@ -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;
}
}