linked list里删除一个数字的问题
有3个问题。1) [1 2 3 4],比如我要删除linked list里的数字2, 帮我看看我写的哪里需要改(method remove我用/* */标出来了)。
2)我写的copy method 拷贝[1 2 3 4]结果出来怎么是[4 3 2 1]? 怎么改
3)如何把两个linked list组合起来
高手帮忙回答以下,谢谢了
public class ILS
{
private int data;
private ILS head;
private int n;
public ILS(int data, ILS head)
{
this.data = data;
this.head = head;
}
public int getData()
{
return data;
}
public ILS getNext()
{
return head;
}
public void setData(int data)
{
this.data = data;
}
public void setNext(ILS next)
{
head = next;
}
public void add(int number)
{
head=new ILS(number, head);
n++;
}
public void add(int number1, int number2, int number3, int number4)
{
head=new ILS(number1, head);
n++;
head=new ILS(number2, head);
n++;
head=new ILS(number3, head);
n++;
head=new ILS(number4, head);
n++;
}
public void clear()
{
head = null;
n=0;
}
public boolean contains(int number)
{
for (ILS temp=head; temp!=null; temp=temp.getNext())
if (temp.getData()==number)
return true;
return false;
}
public ILS copy()
{
ILS copy=new ILS(0, null);
for (ILS temp=head; temp!=null; temp=temp.getNext())
{
copy.add(temp.getData());
}
return copy;
}
public boolean isEmpty()
{
ILS temp=head;
while(temp!=null)
return false;
return true;
}
/*
public void remove(int number)
{
IntNode prev = null;
if (n==0) throw
new IllegalStateException("the list is empty");
for (ILS temp=head; temp!=null; temp=temp.getNext())
if (temp.getData()==number)
prev.next = temp.next;
temp = temp.next;
n--;
}*/
public int size()
{
return n;
}
public String toString()
{
ILS temp=head;
String str="[ ";
while(temp!=null)
{
str+=temp.getData()+" ";
temp=temp.getNext();
}
return str+"]";
}
}