Add a new ListStack
class
This commit is contained in:
parent
9d200788d0
commit
b13632ccc8
1 changed files with 57 additions and 0 deletions
57
app/src/main/java/ch/nuth/zhaw/exbox/ListStack.java
Normal file
57
app/src/main/java/ch/nuth/zhaw/exbox/ListStack.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue