Add a new ListStack class

This commit is contained in:
Manuel Thalmann 2022-09-27 12:58:17 +02:00
parent 9d200788d0
commit b13632ccc8

View file

@ -0,0 +1,57 @@
package ch.nuth.zhaw.exbox;
import java.util.ArrayList;
import java.util.List;
public class ListStack implements Stack {
private List<Object> innerList = new ArrayList<>();
@Override
public void push(Object x) throws StackOverflowError {
try {
if (innerList.add(x)) {
return;
}
}
catch (Exception e) {
throw new StackOverflowError();
}
}
@Override
public Object pop() {
if (!isEmpty()) {
int index = innerList.size() - 1;
Object result = innerList.get(index);
innerList.remove(index);
return result;
} else {
return null;
}
}
@Override
public boolean isEmpty() {
return innerList.size() == 0;
}
@Override
public Object peek() {
if (!isEmpty()) {
return innerList.get(innerList.size() - 1);
} else {
return null;
}
}
@Override
public void removeAll() {
innerList.clear();
}
@Override
public boolean isFull() {
return innerList.size() == Integer.MIN_VALUE;
}
}