我希望有人可以帮助我解决我的小问题。我按照我的接口不可变队列以这种方式定义了我的 EmptyQueue 和我的 NotEmptyQueue。主要问题是应该向 myQueue 添加元素的方法 enQueue 不起作用。请帮助我:
界面:
public interface ImmutableQueue<E> extends Iterable<E> {
boolean isEmpty();
int size();
ImmutableQueue<E> enQueue(E e);
ImmutableQueue<E> deQueue();
E getTop();
}
空队列:
import java.util.Iterator;
import java.util.NoSuchElementException;
public class EmptyQueue<E> implements ImmutableQueue <E>, Iterable <E> {
@Override
public boolean isEmpty() {
return true;
}
@Override
public int size() {
return 0;
}
@Override
public ImmutableQueue<E> enQueue(E e) {
NotEmptyQueue<E> q= new NotEmptyQueue <>(e,this);
return q;
}
@Override
public ImmutableQueue<E> deQueue() {
throw new NoSuchElementException();
}
@Override
public E getTop() {
throw new NoSuchElementException();
}
@Override
public Iterator<E> iterator() {
return new Iterator<E>(){
@Override
public boolean hasNext() {
return false;
}
@Override
public E next() {
throw new NoSuchElementException();
}
};
}
}
NotEmptyQueue:
import java.util.Iterator;
public class NotEmptyQueue<E> implements ImmutableQueue<E>, Iterable <E>{
public E e;
public ImmutableQueue<E> tail;
public NotEmptyQueue(E e, ImmutableQueue<E> tail){
this.e = e;
this.tail = tail;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public int size() {
return tail.size() + 1;
}
@Override
public ImmutableQueue<E> enQueue(E e) {
return new NotEmptyQueue<>(e,this);
}
@Override
public ImmutableQueue<E> deQueue() {
return tail;
}
@Override
public E getTop(){
return e;
}
public static void main (String [] args){
NotEmptyQueue<Integer> myQueue= new NotEmptyQueue<>(new Integer(1),null);
myQueue.enQueue(9);
myQueue.enQueue(7);
System.out.println(myQueue.size());
System.out.println(myQueue.getTop());
for(Integer i : myQueue){
System.out.println(i);
}
}
@Override
public Iterator<E> iterator() {
return new Iterator<E>(){
ImmutableQueue<E> queue;
@Override
public boolean hasNext() {
return (!tail.isEmpty());
}
@Override
public E next() {
E res = queue.getTop();
queue = queue.deQueue();
return res;
}
Iterator<E> setQueue(ImmutableQueue<E> queue){
this.queue = queue;
return this;
}
}.setQueue(this);
}
}