solved ex2
This commit is contained in:
parent
68ce7543f4
commit
f6699b861c
72
src/main/java/ch/zhaw/ads/ListStack.java
Normal file
72
src/main/java/ch/zhaw/ads/ListStack.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
7
src/main/java/ch/zhaw/ads/StackOverflowException.java
Normal file
7
src/main/java/ch/zhaw/ads/StackOverflowException.java
Normal file
@ -0,0 +1,7 @@
|
||||
package ch.zhaw.ads;
|
||||
|
||||
public class StackOverflowException extends Exception {
|
||||
public StackOverflowException(String errorMessage) {
|
||||
super(errorMessage);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user