【发布时间】:2019-01-21 18:33:53
【问题描述】:
我有一个具有以下方法的 MyList 类:
public class MyList{
ArrayList<Object> list;
MyList(int a, int b)
{
list = new ArrayList<Object>();
for(;a<=b;a++)
list.add(a);
}
public void add(int index, Object o)
{
list.add(index, o);
}
public Object remove(int index) throws isEmptyException
{
if(isEmpty())
throw new isEmptyException();
else
return list.remove(index);
}
public boolean isEmpty()
{
return list.isEmpty();
}
这是我的班级队列。我必须仅使用 MyList 中的上述方法来实现以下方法。
public class Queue extends MyList{
public void enqueue(Object o)
{
//adds a new Object to the queue
}
public Object dequeue()
{
//removes the next Object from the queue and returns it
}
public boolean empty()
{
//Checks if the queue is empty
}
我真的不知道从哪里开始,因为我不知道队列的大小。有人可以给我一个提示如何解决这个问题吗?递归方法在这里有用吗?
提前致谢!
【问题讨论】:
-
将列表用作 FIFO 队列可以通过在末尾添加并从开头删除,或在开头添加并从末尾删除。跨度>
-
仅供参考:
LinkedList实现了Queue接口。