2012-05-28 50 views
0

我想通過Eclipse 製作LinkedList,但我不知道如何製作LinkedList。首先,我想比較int的數據和參考。 但是,我不知道如何比較。製作鏈接列表

package List; 

public class DoubleLinkedList 
{ 
    CellDouble x; 
    CellDouble head;//List's head 
    public DoubleLinkedList() 
    { 
     //리스트의 헤더를 할당한다. 
     head=new CellDouble(0); 
     head.prev=head.next=head; 
    } 

    public void insertAfter(CellDouble p,int data) 
    { 
     CellDouble x=new CellDouble(data); 
     x.prev=p; 
     x.next=p.next; 
     p.next.prev=x; 
     p.next=x; 


    } 

    public void insertFirst(int data) 
    { 
     //List's next to insert 
     insertAfter(head.next,data); 
    } 
    public void insertLast(int data) 
    { 
     //list's last's next to insert. 
     insertAfter(head.prev,data); 
    } 
    public void removeCell(CellDouble p) 
    { 

     p.prev.next=p.next; 
     p.next.prev=p.prev; 
    } 

    public Object removeFirst() 
    { 

     if(isEmpty()) 
     { 
      return null; 
     } 
     CellDouble cell=head.next; 
     removeCell(cell); 
     return cell.data; 
    } 
    public Object removeLast() 
    { 
     // 요소가 없다면 null 반환 
     if(isEmpty()) 
     { 
      return null; 
     } 
     CellDouble cell=head.prev; 
     removeCell(cell); 
     return cell.data;  
    } 
    public boolean isEmpty() 
    { 
     return head.next==head; 
    } 

    public void FindNumber(int data) 
    { 
     if(x==null) 
     { 
      insertFirst(data); 
     } 
     else if(x.data<data) 
     { 
      insertAfter(x,data); 
     } 
    } 
    public void Findnumber(int data) 
    { 
     if(x.data==data) 
     { 
      removeCell(x); 
     } 
     else if(x.data!=data) 
     { 
      System.out.println("like this number is nothing"); 
     } 
    } 

} 

而且,我完成了我的編程。但是,其結果'[email protected]'

+0

你檢查了嗎? http://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html – Teg

+0

由於您的代碼站立,它不會編譯,因爲'FindNumber'被定義兩次。 – Makoto

+0

這只是輸出你創建的LinkedList,你到底想要輸出什麼? – Satya

回答

1

查看我的回答here瞭解基礎知識。您的列表中的元素必須實現Comparable,請閱讀更多here