2012-10-08 7 views
-2

그래서 내가 만든 프로그램에 대한 내 LinkedList를 컴파일하려고하는데 실제로 오류라고 생각되는 오류가 다시 발생합니다 (그 오류가 발생한 경우). 나는 사람이 보면 내가 컴파일 할 때 오류가지고있을 이유 자신의 생각에 넣을 수 있는지 궁금하고 내가해야 할 일에 대한 어떤 제안이 있다면 그들을 제거하기 :Java LinkedList Program

또한
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 일치하는 중괄호를 검사하기 위해 코드를 삽입하지 마십시오. 코드를 들여 쓰지 않을 때 직면하는 주요 문제는 ..

+0

고마워요! 또한, 위의 프로그램을 클래스가 아닌 인터페이스로 사용할 수있는 방법은 무엇입니까? – user1698560

+0

기능을 정의하기 위해 실제로 미리 정의 된 인터페이스를 구현하고 있습니다. 왜 클래스를 원하지 않습니까 ?? –

+0

이 경우 인터페이스 정의는 실제로 필요하지 않습니다. –