diff --git a/app/src/main/java/ch/nuth/zhaw/exbox/ListStack.java b/app/src/main/java/ch/nuth/zhaw/exbox/ListStack.java new file mode 100644 index 0000000..85f2b38 --- /dev/null +++ b/app/src/main/java/ch/nuth/zhaw/exbox/ListStack.java @@ -0,0 +1,57 @@ +package ch.nuth.zhaw.exbox; + +import java.util.ArrayList; +import java.util.List; + +public class ListStack implements Stack { + private List 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; + } + +}