2009-12-07 57 views
0

我必須爲航空公司服務實施航班圖的鄰接列表,並且我很難理解如何實現這一點。我已經有了readFlightMap方法。這在數據讀取信息,然後在我的第二個以下方法環路處理鄰接表文件調用:爲航班地圖實施鄰接列表

// Inserts information into the flight map to record the fact that the 
// adjCity is adjacent to aCity. 
// Precondition: aCity and adjCity are valid cities that are served 
// by the airline. 
// Postcondition: The map will have changed to record the given information 

public void insertAdjacent(City aCity, City adjCity) 
{ 

} 

我的教練告訴我要實現鏈表的陣列來解決這個問題,但這是圖中他向我們展示類:

LinkedLists數組:

在| |,原始城市位置,然後是他們的Adj列表。

| Q | -> X<br/> 
| X | -> No Adj<br/> 
| R | -> X<br/> 
| P | ->R ->W<br/> 
| W | ->S<br/> 
| S | ->T<br/> 
| T | ->W<br/> 
| Y | ->R ->Z<br/> 
| Z | ->No Adj<br/> 

我在谷歌搜索了關於如何實現這個,但我沒有發現任何有用的東西。他給了我們一個基於ADTList的類,但它只有簡單的東西,如添加,刪除,大小等,並沒有處理他建議給我們的實現。

我迷路了。

回答

0

所有你需要的是添加。

每個城市的一個列表。當調用insertAdjacent時,將adjCity推入aCity的列表中,反之亦然。

+0

我沒有可用於City類的推送方法。它在堆棧類中。我認爲我只會在實際從原點走到目的地時使用它。不是爲了存儲鄰接關係的目的。會是這樣的嗎? private ListArrayBased cityAdj = new ListArrayBased(); public void insertAdjacent(City aCity,City adjcity){cityAdj.add(0,aCity); } 但是,如何添加到最後一個incase還有另一個鄰接關係? – javaRancher2009 2009-12-07 03:01:54

+0

每個城市都需要有一個鄰接者列表。要添加到該列表,請調用其「add」方法。 – 2009-12-07 03:04:55