在链表中查找多个链接

时间:2013-11-03 11:02:20

标签: java linked-list

  

当前的find方法效果很好但仅适用于一个节点。如果我尝试查找(“伦敦”)和更多满足条件的节点,它只返回第一个。我必须修改它以返回满足条件的所有节点吗?

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");
  }

1 个答案:

答案 0 :(得分:2)

find方法中的while循环一旦找到匹配就会停止。做你要问的正确方法是:

  1. 创建一个存储结果的对象
  2. 每次找到结果时,不要立即返回,就像你现在一样,把它放在临时存储上
  3. 到达列表末尾后,返回存储对象。如果它是空的,则没有结果