2017-12-10 4 views
-4

Ich kann LinkedList1 nicht initialisieren, der Fehler, den ich bekomme, ist "Syntaxfehler, Einfügen"; "BlockStatements abzuschließen. Ich weiß, es gibt andere Möglichkeiten, eine verknüpfte Liste zu erstellen, aber ich möchte auf diese Weise erhalten nach unten vor den anderen. jede Hilfe wäre mir ein sehr dankbar Coder machen :)Verknüpfte Liste nicht initialisiert

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

Also es nicht nicht initialisiert, aber nicht kompiliert .... – Antoniossss

+0

versucht erfrischend ...? – Taslim

+2

Denkst du, es könnte hilfreich sein, wenn du uns gesagt hast, wo in deinem Code der Fehler angezeigt wird? Wenn Sie Leute um Hilfe bitten, machen Sie es einfach, indem Sie grundlegende Informationen bereitstellen. So wie es jetzt aussieht, lautet die Frage _ "Hier sind mehr als 100 Zeilen Code, irgendwo ist ein Syntaxfehler, bitte finde ihn" _ - für niemanden wirklich motivierend. –

Antwort

1

Die wichtigste Methode ohne Buchstaben einen Variablennamen hat. 11 bis tt ändern und es wird

0

ändern LinkedList1 11 = new LinkedList1(); arbeiten LinkedList 11 = new LinkedList();.