带接口的链表实现

时间:2017-04-26 19:47:50

标签: java list implementation

我在这个任务中需要做的是从界面实现给定的方法。如果您可以检查并给我一些关于我到目前为止所交付的内容的反馈,我将非常感激。

遗憾的是,我仍然缺少一种方法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++;
    }


}

1 个答案:

答案 0 :(得分:0)

这就是你想要的吗?

@Override
public T getNext8thElementOf(int pos) {
    int eight = 8;
    Node nodo = this.nodeAt(pos);
    while(eight > 0) {
        eight--;
        nodo = nodo.next;
    }
    return nodo.item;
}