【发布时间】:2017-12-10 19:19:48
【问题描述】:
我无法初始化 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);
}
}
【问题讨论】:
-
所以它没有初始化但没有编译....
-
试过刷新...?
-
如果您告诉我们您的代码在哪里指示了错误,您认为这可能会有所帮助吗?如果您要向他人寻求帮助,请提供基本信息以方便帮助。就目前而言,问题是“这里有 100 多行代码,某处存在语法错误,请查找”——对任何人都没有真正的激励作用。
-
你把字母
l和数字1混在一起了。 -
@JimGarrison 错误发生在 main 方法的第一行。抱歉,第一次在这里发帖并认为我做得很好,但希望下次我的下一篇文章不会那么糟糕:)
标签: java linked-list