【发布时间】:2017-10-18 18:48:45
【问题描述】:
我试图从 LinkedNode 队列中取出特定元素。这就是我到目前为止从另一个方法中得到的,它只是删除并返回队列中的第一个节点(因为它应该在队列中)。我的问题是如何编辑以便删除索引 x 处的元素?我包含了 javadoc psudocode,希望能有所帮助。
/**
* Removes and returns the element that is at place x in the queue.
* Precondition: x must be less than 5, x must be less than size
* Note: indexing from 0: 0 == front element, 1 == second element, etc.
* @param x the passed in index of the element to be removed
* @return the element removed from the queue
* @throws EmptyCollectionException if the queue is empty
* @throws InvalidArgumentException if x > 4, or x > size of collection
*
*/
public T dequeue(int x) throws EmptyCollectionException {
if (numNodes == 0) {
throw new EmptyCollectionException("Work Ahead Queue");
}
T element = front.getElement();
front = front.getNext();
firstFive.remove(0);
numNodes--;
if (numNodes >= 5) {
firstFive.add(firstFive.get(3).getNext());
}
return element;
}
【问题讨论】:
标签: java data-structures linked-list queue