当前位置: 代码迷 >> Java相关 >> linked list里删除一个数字的问题
  详细解决方案

linked list里删除一个数字的问题

热度:739   发布时间:2010-10-22 07:37:03.0
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+"]";
   }
  
}
搜索更多相关的解决方案: list  linked  数字  删除  

----------------解决方案--------------------------------------------------------
有人能改吗
----------------解决方案--------------------------------------------------------
我改了一下我的remove, 为什么数字还是删不了
public void remove(int number)
    {
       ILS temp=head;
       ILS next = temp.getNext();
       if (n==0) throw
         new IllegalStateException("the list is empty");  
        for (; next!=null; temp=temp.getNext())
        {
         if (next.getData()==number)
            {
            temp = next.getNext();
                n--;
            next=null;
            }
            else
            
               next=next.getNext();
        }
    }

----------------解决方案--------------------------------------------------------
  相关解决方案