0
我在這個任務中需要做的是從接口實現給定的方法。如果您可以檢查並且就我迄今爲止所提供的信息給予一些反饋,將非常感激。鏈接列表實現與接口
不幸的是,我仍然缺少一種方法,public T getNext8thElementOf(int pos)
。有沒有人有如何實現它的想法?
/**
* A simple list interface
* @param <T> The type of list element
*/
public interface ISpeedList<T> {
/**
* Returns the current number of elements in the list
*
* @return Current number of elements in the list
*/
public int size();
/**
* Inserts an element at the beginning of the list
*
* @param item Item to be inserted
*/
public void prepend(T item);
/**
* Returns the element at the specified position in the list
*
* @param pos The position of the element in the list starting from 0
*
* @return The specified element in the list
*
* @throws IndexOutOfBoundsException If the requested element is out of
* range
*/
public T getElementAt(int pos);
/**
* Returns the next 8th element of the specified element in the list
*
* @param pos The position of the specified element in the list starting
* from 0
*
* @return The next 8th element of the specified element
*
* @throws IndexOutOfBoundsException If the requested element is out of
* range
*/
public T getNext8thElementOf(int pos);
}
public class SpeedList<T> implements ISpeedList<T> {
/**
* Doubly-linked node class, completely private to the List class,
* as clients don't care about the implementation of the list.
*/
private class Node {
T item;
Node next;
Node previous;
Node(T item, Node next, Node previous) {
this.item = item;
this.next = next;
this.previous = previous;
}
}
/**
* The list itself maintains only a reference to its "header" node.
* The header is a node that does not store any data. Its 'next'
* field points to the first item in the list and its 'previous'
* field points to the last item. This makes all insertions and
* deletions uniform, even at the beginning and the end of the list!
*/
private Node header = new Node(null, null, null);
/**
* The number of items in the list, stored to make size() O(1).
*/
private int size = 0;
/**
* Returns the number of items in the list.
*/
@Override
public int size() {
return size;
}
/**
* Inserts <code>item</code> as the new first item.
*/
@Override
public void prepend(T item) {
addBefore(item, header.next);
}
/**
* Returns the item at the given index position.
*
* @throws IndexOutOfBoundsException
* if index not in [0,size).
*/
@Override
public T getElementAt(int pos) {
return nodeAt(pos).item;
}
@Override
public T getNext8thElementOf(int pos) {
// TODO Auto-generated method stub
return null;
}
//
// PRIVATE HELPER METHODS
//
private Node nodeAt(int pos) {
if (pos < 0 || pos >= size) {
throw new IndexOutOfBoundsException(pos + " for size " + size);
}
Node n = header;
for (int i = 0; i <= pos; i++) {
n = n.next;
}
return n;
}
private void addBefore(T o, Node n) {
Node newNode = new Node(o, n, n.previous);
newNode.previous.next = newNode;
newNode.next.previous = newNode;
size++;
}
}
不知道什麼*「下一個第八元素」*在這方面的含義。 – Andreas
也許這會更好[代碼評論。](https://codereview.stackexchange.com/)你有沒有寫過任何測試?你可以展示他們嗎? – markspace
「下一個第八元素」是什麼意思?你的意思是「元素8在'pos'後面嗎?如果是這樣,爲什麼不簡單地實現爲'return getElementAt(pos + 8);'? –