diff --git a/src/main/java/ch/zhaw/ads/ListStack.java b/src/main/java/ch/zhaw/ads/ListStack.java new file mode 100644 index 0000000..c476fbb --- /dev/null +++ b/src/main/java/ch/zhaw/ads/ListStack.java @@ -0,0 +1,72 @@ +/** + * AnyServer -- Praktikum Experimentierkasten -- ADS + * + * @author E. Mumprecht + * @version 1.0 -- Geruest fuer irgendeinen Server + */ +package ch.zhaw.ads; + +public class ListStack implements CommandExecutor { + + @Override + public String execute(String command) { + StringBuffer result = new StringBuffer(100); + result.append("Die Eingabe war \""); + result.append(command); + result.append("\"\n"); + return result.toString(); + } + + Object[] data; + private int top; + private int capacity; + private static final int DEFAULT_CAPACITY = 100; + + public ListStack() { + this(DEFAULT_CAPACITY); + } + + public ListStack(int capacity) { + this.capacity= capacity; + removeAll(); + } + + public void removeAll() { + data = new Object[capacity]; + top = 0; + } + + public void push(Object x) { + // public void push(Object x) throws StackOverflowException { + // if (isFull()){ + // throw new StackOverflowException(); + // } + data[top] = x; + top++; + } + + public Object pop() { + if (isEmpty()){ + return null; + } + top--; + Object topItem= data [top]; + data [top] = null; + return topItem; + } + + public Object peek() { + if (isEmpty()){ + return null; + } + return data [top-1]; + } + + public boolean isEmpty() { + return (top == 0); + } + + public boolean isFull() { + return top == data.length; + } +} diff --git a/src/main/java/ch/zhaw/ads/StackOverflowException.java b/src/main/java/ch/zhaw/ads/StackOverflowException.java new file mode 100644 index 0000000..4aae96e --- /dev/null +++ b/src/main/java/ch/zhaw/ads/StackOverflowException.java @@ -0,0 +1,7 @@ +package ch.zhaw.ads; + +public class StackOverflowException extends Exception { + public StackOverflowException(String errorMessage) { + super(errorMessage); + } +}