Java 在链接列表中查找多个链接
当前的find方法运行良好,但仅适用于一个节点。例如,如果我尝试查找(“London”)和更多满足条件的节点,它只返回第一个节点。我必须修改什么才能返回满足条件的所有节点Java 在链接列表中查找多个链接,java,linked-list,Java,Linked List,当前的find方法运行良好,但仅适用于一个节点。例如,如果我尝试查找(“London”)和更多满足条件的节点,它只返回第一个节点。我必须修改什么才能返回满足条件的所有节点 find方法中的while循环在找到匹配项后停止。做你所要求的事情的正确方法是: 创建一个将存储结果的对象 每次发现结果时,不要像现在这样立即返回,而是将其放在临时存储中 到达列表末尾后,返回存储对象。如果是空的,则没有结果 我我对对象/方法/列表有点陌生。这是哪种语言?用你正在使用的正确语言标记你的文章。至于你的问题,首先你
find方法中的while循环在找到匹配项后停止。做你所要求的事情的正确方法是:
我我对对象/方法/列表有点陌生。这是哪种语言?用你正在使用的正确语言标记你的文章。至于你的问题,首先你必须决定你希望结果以什么形式出来。假设你有3个城市,你打算如何将它们返回到find()调用中?假设我的链接列表中有10架飞机(航班号、目的地、航空公司、起飞时间)。如果我在搜索目的地时有多个航班具有相同的目的地,我希望打印出具有该目的地的所有节点的所有信息。我从main添加了一段代码,显示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");
}