链接列表未初始化

时间:2017-12-10 19:19:48

标签: java linked-list

我无法使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);


    }
}

2 个答案:

答案 0 :(得分:1)

main方法有一个没有字母的变量名。将11更改为tt,它将起作用

答案 1 :(得分:0)

更改 LinkedList1 11 = new LinkedList1();LinkedList 11 = new LinkedList();