2017-04-26 8 views
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++; 
    } 


} 
+2

단서 없음 * "다음 8 번째 요소"*는 이러한 맥락에서의 의미입니다. – Andreas

+2

아마도 [코드 검토]에서 더 좋을 것입니다. (https://codereview.stackexchange.com/) 테스트를 작성 했습니까? 그들을 보여줄 수 있습니까? – markspace

+3

"다음 8 번째 요소"란 무엇을 의미합니까? "pos"뒤에 요소 8을 의미합니까? 그렇다면 단순히 return getElementAt (pos + 8);로 구현하면 안됩니까? –

답변

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; 
}