我想为图灵机实现一个名为 Tape 的通用类,但我不确定如何实现磁带的空白部分。到目前为止,它看起来像:
public class Tape<T>{
private Stack<T> left = new Stack<T>(); // left part of tape
private Stack<T> right = new Stack<T>(); // right part of tape
private T current;
private T BLANK = null;
public Tape(){
right.push(BLANK);
current = BLANK;
}
public void move(Direction direction) {
if (direction == Direction.LEFT) {
right.push(current);
if (left.isEmpty()) left.push(BLANK); // increase size of tape if necessary
current = left.pop();
} else if (direction == Direction.RIGHT) {
left.push(current);
if (right.isEmpty()) right.push(BLANK); // increase size of tape if necessary
current = right.pop();
}
}
主要问题是,我不知道如何处理这个空白信号。目前它设置为空,但至少因为我想调用 move 并且堆栈为空,push() 和 pop() 不会工作。
任何想法如何标记空白,因为我不知道 T 的类型?