Hallo,
ich möchte eine Methode schreiben um einen Stack zu reversen. Leider funktioniert mein Code noch nicht ganz:
Was ist in der reverse Methode falsch?
ich möchte eine Methode schreiben um einen Stack zu reversen. Leider funktioniert mein Code noch nicht ganz:
Java:
public class Stack<E> {
private final E value;
private final Stack<E> next;
private Stack(E value, Stack<E> next) {
this.value = value;
this.next = next;
}
public static <E> Stack<E> create() {
return null;
}
public static <E> Stack<E> push(Stack<E> a, E e) {
Stack<E> erg = new Stack<E>(e,a);
return erg;
}
public static <E> Stack<E> pop(Stack<E> a) { //pop(push(s,e) = s
if (a == null) {
return Stack.<E>create();
}
else if (a.value == null){
return Stack.<E>create();
}
else {
return a.next;
}
}
public static <E> E top(Stack<E> a) {
if (a == null) {
return null;
}
else if (a.value == null){
return null;
}
else {
return a.value;
}
}
public static <E> boolean isEmpty(Stack<E> a) {
if (a == null) {
return true;
}
else if (a.value == null) {
return true;
}
else {
return false;
}
}
public static <E> Stack<E> reverse(Stack<E> s) {
Stack<E> top = Stack.<E>pop(s);
if (Stack.<E>isEmpty(s)) {
return top;
} else {
Stack<E> bottom = reverse(s);
Stack.<E>push(top, null);
return bottom;
}
}
}
Was ist in der reverse Methode falsch?