0
package list;

public class LinkedList implements List {

    //Datenfeld
    private Element item;

    //Zeigerfeld
    private LinkedList next;

    //Konstruktor, erzeugt leere Liste
    public LinkedList() {
        item = null;
        next = null;
    }

    //Selektoren
    public Object getItem() {
        return item;
    }

    public LinkedList next() {
        return next;
    }

    //ist die Liste leer?
    public boolean isEmpty() {
        return next == null;
    }

    public Object firstElement() {
        if (isEmpty()) 
            return null;
        else
            return next.item;
    }

    public int length() {
        if (isEmpty())
            return 0;
        else
            return 1 + next.length();
    }

    //fügt x am Kopf ein
    public LinkedList insert(Element x) {
        LinkedList l = new LinkedList();
        l.item = x;
        l.next = next;
        next = l;
        return this;
    }

    //hängt das x ans Ende der Liste und liefert Teilliste
    public LinkedList append (Element x) {
        if (isEmpty())
            return insert(x);
        else
            return next.append(x);
    }

    //liefert Null, falls x nicht in Liste
    //sonst Teilliste
    private LinkedList find(Element x) {
        if (isEmpty()) 
            return null;
        else
            if (firstElement().equals(x))
                return this;
            else
                return next.find(x);
    }

    //entfertn erstes x der Liste
    public LinkedList delete(Element x) {
        LinkedList l = find(x);
        if (l != null)
            return l.next = l.next.next;
        else
            return this;
    }

    //entfernt das erste Element der Liste
    public LinkedList delete() {
        if (!isEmpty()) 
            next = next.next;
        return this;    
    }

    public boolean isInList(Element x) {
        return(find(x) != null);
    }

    public String toString() {
        return (next == null ? " |--" : " --> " + next.item + next);
    }

    static void println(Element x) {
        System.out.println(x.toString());
    }

    public LinkedList add(Element x, int n) {
        return null;
    }

    public LinkedList remove(int n) {
        return null;
    }

    public Element get(int n) {
        return null;
    }

    public int firstIndexOf(Element x) {
        return 1;
    }

    public int lastIndexOf(Element x) {
        return 1;
    }


    LinkedList l1 = new LinkedList();

    l1.insert("AA");
}

在最后一行(l1.insert("AA");我得到了错误

Type Syntax error on token(s), misplaced construct(s).

需要帮忙。找不到问题。

4

1 回答 1

4

在方法之外,您不能有这样的随机语句。您需要将该语句放在一个方法中,或者构建一个使用您的链表并进行插入的类。

于 2012-05-06T10:19:36.400 回答