2017-12-10 5 views
-4

초기화 할 LinkedList1을 가져올 수 없습니다. "구문 오류, 삽입"; "BlockStatements를 완료하는 중입니다. 연결된 목록을 만드는 다른 방법이 있지만이 방법을 원합니다. 아래 다른 사람 전에. 어떤 도움이. 나에게 매우 감사 코더 :링크 된 목록이 초기화되지 않았습니다.

import java.util.*; 

public class LinkedList1 { 

    private class Node 
    { 
     String value; 
     Node next; 

     Node(String val, Node n) 
     { 
      value=val; 
      next=n; 
     } 

     Node(String val) 
     { 
      this(val,null); 
     } 
    } 

    private Node first; 
    private Node last; 

    public LinkedList1() 
    { 
     first=null; 
     last=null; 
    } 

    public boolean isEmpty() 
    { 
     return first == null; 
    } 

    public int size() 
    { 
     int count=0; 
     Node p = first; 
     while(p!=null) 
     { 
      count++; 
      p=p.next; 
     } 
     return count; 
    } 

    public void add(String e) 
    { 
     if(isEmpty()) 
     { 
      first = new Node(e); 
      last = first; 
     } 
     else 
     { 
      last.next = new Node(e); 
      last = last.next; 
     } 
    } 
    public void add(int index, String e) 
    { 
     if(index<0 || index>size()) 
     { 
      String message = String.valueOf(index); 
      throw new IndexOutOfBoundsException(message); 
     } 
     if(index==0) 
     { 
      first=new Node(e, first); 
      if(last==null) 
      { 
       last=first; 
       return; 
      } 
      Node pred = first; 
      for(int k = 1; k<=index-1;k++) 
      { 
       pred=pred.next; 
      } 
      //splice in a node containging the new element 
      pred.next = new Node(e, pred.next); 
      //is theere a new last element? 
      if(pred.next.next==null) 
      { 
       last = pred.next; 
      } 
     } 
    } 

    public String toString() 
    { 
     StringBuilder strBuilder = new StringBuilder(); 
     //use p to walk down linked list 
     Node p = first; 
     while(p!=null) 
     { 
      strBuilder.append(p.value+ "\n"); 
      p=p.next; 
     } 
     return strBuilder.toString(); 
    } 

    public String remove(int index) 
    { 
     if(index<0||index>=size()) 
     { 
      String message = String.valueOf(index); 
      throw new IndexOutOfBoundsException(message); 
     } 
     String element;//element to return 
     if(index==0) 
     { 
      //removal of the first element 
      element = first.value; 
      first = first.next; 
      if(first==null) 
       last=null; 
     } 
     else 
     { 
      //to remove an element other than the first, find the pred of the element to be removed 
      Node pred = first; 
      //move pred forward index-1 times 
      for(int k = 1; k<=index-1;k++) 
       pred=pred.next; 
      //store the value to return 
      element = pred.next.value; 
      //Route link around the node to be removed 
      pred.next = pred.next.next; 
      //check if pred is now last 
      if(pred.next==null) 
       last=pred; 
     } 
     return element; 
    } 
    public boolean remove(String element) 
    { 
     if(isEmpty()) 
      return false; 

     if(element.equals(first.value)) 
     { 
      first = first.next; 
      if(first==null) 
       last=null; 
      return true; 
     } 
     //find the pred of the element to remove 
     Node pred = first; 
     while(pred.next!= null && !pred.next.value.equals(element)) 
     { 
      pred = pred.next; 
     } 

     //pred.next==null or pred.next.value is element 
     if(pred.next==null) 
      return false; 
     //pred.next.value is element 
     pred.next = pred.next.next; 
     //check if pred is now last 
     if(pred.next==null) 
      last=pred; 

     return true; 
    } 
    public static void main(String[] args) 
    { 
     LinkedList1 11 = new LinkedList1(); 
     11.add("Amy"); 
     11.add("Bob"); 
     11.add(0,"Al"); 
     11.add(2,"Beth"); 
     11.add(4,"Carol"); 
     System.out.println("The members of the list are: "); 
     System.out.println(11); 


    } 
} 
+1

초기화되지 않았지만 컴파일되지 않습니다 ... – Antoniossss

+0

새로 고침 ...? – Taslim

+2

코드에서 오류의 위치를 ​​알려 주시면 도움이 될 것이라고 생각하십니까? 사람들에게 도움을 요청할 경우 기본 정보를 제공하여 도움을 청하십시오. 지금 당장은 질문이 _ "여기에 100 개 이상의 코드 라인이 있습니다. 어딘가에서 구문 오류가 있습니다."_ 실제로는 아무에게도 동기 부여가되지 않습니다. –

답변

1

주요 방법은없는 문자 변수 이름이 만들 TT 11을 변경하고 그것은 작동 할

0

변경 LinkedList1 11 = new LinkedList1(); ~ LinkedList 11 = new LinkedList();.