2013-11-03 83 views
0

當前查找方法運行良好,但僅適用於一個節點。如果我嘗試查找(「倫敦」)以及更多滿足條件的節點,它只返回第一個。我必須修改它以返回滿足條件的所有節點嗎?在鏈接列表中查找多個鏈接

public class LinkList { 

public Link first; // ref to first link on list 

public LinkList() // constructor 
{ 
    first = null; // no links on list yet 
} 

public void insert(Object Airplanefnumber,Object Airplanedest, Object Airplaneline, Object Airplaneplane, Object Airplanetime, Object Terminal, Object Parkingslot) 
{ // make new link 
    Link newLink = new Link(Airplanefnumber, Airplanedest, Airplaneline, Airplaneplane, Airplanetime, Terminal, Parkingslot); 
    newLink.next = first; // it points to old first link 
    first = newLink; // now first points to this 

} 
public void insertqueue(Object Airplanefnumber,Object Airplanedest, Object Airplaneline, Object Airplaneplane, Object Airplanetime, Object Terminal, Object Parkingslot, Object Runway) 
{ // make new link 
    LinkQueue newLink = new LinkQueue(Airplanefnumber, Airplanedest, Airplaneline, Airplaneplane, Airplanetime, Terminal, Parkingslot, Runway); 
    // newLink.next = first; // it points to old first link 
    // first = newLink; // now first points to this 
} 


public Link find(String key) // find link with given key 
{ // (assumes non-empty list) 
    Link current = first; // start at FIRST 

    while(!current.Airplanedest.equals(key)) // while no match, 
    { 

    if(current.next == null) // if end of list, 
      return null; // did not find it 
     else // not end of list, 
      current = current.next; // go to next link 

} 
return current; 
} 





public Link findnumber(int key) // find link with given key 
{ // (assumes non-empty list) 
    Link current = first; // start at FIRST 
    while(!(current.Airplanefnumber.equals(key))) // while no match, 
    { 
     if(current.next == null) // if end of list, 
      return null; // did not find it 
     else // not end of list, 
      current = current.next; // go to next link 
    } 
    return current; // found it 



} 




public Link delete(int key) // delete link with given key 
{ // (assumes non-empty list) 
    Link current = first; // search for link 
    Link previous = first; 

    while(!(current.Airplanefnumber).equals(key)) 
    { 
     if(current.next == null) 
      return null; // did not find it 
     else 
     { 
      previous = current; // go to next link 

     } 
     current = current.next; 
    } // found it 

    if(current == first) // if first link, 
     first = first.next; // change first 
    else // otherwise, 
     previous.next = current.next; // bypass it 
    return current; 
} 


public void displayList() // display the list 
{ 

    System.out.println(); 
    Link current = first; // start at beginning of list 
    while(current != null) // until end of list, 
    { 
     current.displayLink(); // print data 

     current = current.next; // move to next link 
    } 
    // System.out.println("Flight Number is: "+ flightnumber +"\n"+"Flight destination is:"+ destination +"\n"+ "Airline: "+ airline +"\n"+"Airplane: "+ airplane +"\n"+"Schedule time: "+ time); 

    System.out.println(""); 
} 

public void peekFirst() 
{ 
    System.out.println(first.Airplanefnumber + "\n" +first.Airplanedest + "\n" + first.Airplaneline + "\n" + first.Airplaneplane + "\n" + first.Airplanetime + "\n" + first.Terminal + "\n" + first.Parkingslot); 
} 


public boolean isEmpty() 
{ 
    return(first==null); 
} 
} //end of LinkList class 


    //code from main 

    Link f = null; 
    System.out.println("Enter flight destination"); 
    String dest = input.nextLine(); 
    System.out.println("You entered destination "+ dest); 
    f = Planes.find(dest); 
    if(f != null){ 
    System.out.println("Flight number: "+f.Airplanefnumber +"\n"+"Flight destination is:"+f.Airplanedest+"\n"+"Airline: "+f.Airplaneline+"\n"+"Airplane type: "+f.Airplaneplane+"\n"+"Scheduled time: "+f.Airplanetime + "\n" + "Terminal nr. " + f.Terminal +"\n" + "Parking slot: " + f.Parkingslot);} 
     else{ 
     System.out.println("Cannot find flight"); 
    } 

回答

2

在您的查找方法while循環停止,一旦它找到一個匹配。做你所要求的正確方法是:

  1. 創建一個對象,將存儲您的結果
  2. 每次你找到一個結果,而不是返回立即,像你現在,把它放在臨時存儲
  3. 一旦到達列表的末尾,請返回您的存儲對象。如果它是空的,沒有結果
+0

我是一種新的對象/方法/列表。 – user2946804

+0

這是哪一種語言?用您使用的正確語言標記您的帖子。 至於你的問題,首先你必須決定你想要什麼樣的結果出來。說如果你有3個城市,你打算如何將它們返回給發現的任何電話()? – Ermir

+0

假設我在我的鏈接列表中有10架飛機(航班號,目的地,航空公司,出發時間)。如果我在搜索目的地時有相同目的地的多個航班,我想打印出所有具有該目的地的所有節點的所有信息。我在main中添加了一段代碼,顯示發現的返回信息。 – user2946804