2013-11-03 56 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