2012-10-08 32 views
-2

所以我想編譯我的LinkedList的一個程序,我已經創建,它回來的錯誤,我覺得是真的錯誤(如果這使得)。我想知道如果任何人都可以看到這一點,並把他們我爲什麼當我編譯可能會得到錯誤的想法,如果他們對我應該做的任何建議,以擺脫他們:Java LinkedList程序

import java.util.Iterator; 
import java.util.NoSuchElementException; 

public class LinkedListDS<E> implements ListADT<E> { 
    class Node<E> { 
     E data; 
     Node<E> next; 

     public Node(E data) { 
      this.data = data; 
      next = null; 
      } 
     } 

    private Node<E> head, tail; 
    private int currentSize; 

    public LinkedListDS() { 
     head = tail = null; 
     currentSize = 0; 
     } 

    public void addFirst(E obj) { 
     if(head == null) 
      head = tail = newNode; 
     else { 
      newNode.next = head; 
      head = newNode; 
      } 
     currentSize++; 
     } 

// Adds the Object obj to the end of the list 
    public void addLast(E o) { 
     if(head == null) 
      head = newNode; 
      tail = newNode; 
     currentSize++; 
     return; 
     { 
     tail.next = newNode; 
     tail = newNode; 
     currentSize++; 
     } 
    } 

// Removes the first Object in the list and returns it. 
// Returns null if the list is empty. 
    public E removeFirst() { 
     if(head == null) 
      return null; 
     E tmp = head.data; 
     head = head.next; 
     { 
     if (head == null) 
      tail = null; 
      currentSize++; 
     return tmp; 
     } 
    } 

// Removes the last Object in the list and returns it. 
// Returns null if the list is empty. 
    public E removeLast() { 
     Node<E> previous = null; 
     Node<E> current = head; 
      if (current == null) 
       return null; 
      while(current.next != null) { 
       previous = current; 
       current = current.next; 
      } 
      if(previous = null) 
       return removeFirst; 
       previous.next = null; 
       tail = previous; 
       currentSize--; 
      return current.data; 
     } 

// Returns the first Object in the list, but does not remove it. 
// Returns null if the list is empty. 
    public E peekFirst() { 
     if(head == null) 
      return null; 
     return head.data; 
     } 

// Returns the last Object in the list, but does not remove it. 
// Returns null if the list is empty. 
    public E peekLast() { 
     if(tail == null) 
      return null; 
     return tail.data; 
     } 

// Removes the specific Object obj from the list, if it exists. 
// Returns true if the Object obj was found and removed, otherwise false 
    public boolean remove(E obj) { 
     if(head == null) 
      return false; 
      { 
     if(head.data.equals(obj)) 
      head = head.next; 
      return true; 
      } 
    Node<E> current = head; 
    while(current.next != null) 
    { 
     if(current.next.data.equals(obj)) 
     { 
      current.next = current.next.next; 
      return true; 
      } 
     current - current.next; 
     } 
    currentSize--; 
    return false, 
    } 
}  

// The list is returned to an empty state. 
    public void makeEmpty() { 
     head = tail = null; 
     currentSize = 0; 


// Returns true if the list contains the Object obj, otherwise false 
    public boolean contains(E obj) { 
     Node<E> current = head; 
     while(current != null) 
     { 
      if(current.data.equals(obj)) 
      { 
       return true; 
       } 
      current - current.next; 
      } 
     return false; 
     } 


// Returns true if the list is empty, otherwise false 
    public boolean isEmpty() { 
     return = null; 
     } 

// Returns true if the list is full, otherwise false 
    public boolean isFull() { 
     return false; 
     } 

// Returns the number of Objects currently in the list. 
    public int size() { 
     return currentSize; 
     } 

    public iterator<E> iterator() { 
     return new iterator; 
      } 

    class IteratorHelper implements Iterator<E> { 
     Node<E> iteratorPointer 

     public IteratorHelper() { 
      iteratorPointer = head; 
      } 
     public boolean hasNext() { 
      return iteratorPointer != null; 
      } 
     public E next() { 
      if(!hasNext()) 
       throw new NoSuchElementException(); 
      E tmp = iteratorPointer.data; 
      iteratorPointer = iteratorPointer.next; 
      return tmp; 
      } 
     public void remove() { 
      throw new UnsupportedOperationException(); 
      } 
     } 

} 

而且,有沒有一種方法可以讓這段代碼更容易閱讀,理解,而不是更長,但仍然有完成相同的任務?請隨時分享。

+1

如果你發佈這個代碼給你的*錯誤*會更好。 –

+0

你正在界面中定義方法..使它成爲一個類..你有一個接口的構造函數.. –

+0

@RohitJain看起來像答案。在任何人做它之前張貼它! –

回答

4

您已經定義了一個接口,並使用它作爲類..

public interface LinkedListDS<E> implements ListADT<E> { 

問題在上面的聲明: -

  • 接口沒有實現其他的接口,它們擴展,就像一類延伸另一類..
  • 接口沒有定義methodsconstructors

所以,你想用實際類..

更改上述聲明: -

public class LinkedListDS<E> implements ListADT<E> { 

此外,你還沒有關閉了iterator方法..

public iterator<E> iterator() { 
     return new iterator; 

加一個右括號.. 其實,你有這麼多...... Re-inde nt你的代碼來檢查相匹配的大括號。這是你在縮進代碼時面臨的一個主要問題。

+0

謝謝!另外,他們是一種方式,我可以使用上面的程序作爲一個接口,而不是一個類? – user1698560

+0

你實際上正在實現一個預定義的接口..來定義功能..爲什麼你不想要一個類? –

+0

在這種情況下,定義一個接口實際上並不是必需的。 –