我給了一個任務如下:鏈接列表建議
我想建立一個鏈接列表的實現。具體而言,我希望它是一個雙向鏈接列表。
我的任務: 你的程序應該使用鏈表使用鏈表來模擬火車路線。 首先,用戶將輸入儘可能多的站點,因爲他們希望火車有,並且每個站點的名稱。然後程序應該打印路線圖。 一旦完成,他們然後輸入他們想要開始的停止的名稱。 從那裏他們可以輸入命令,將列車前進到下一站或後退到前一站。
我已經告訴我沒有做這個任務的權利,但我真的不明白怎麼沒有,我會很感激,如果有人可以解釋我沒有做我應該做的。
我Route類(還沒有完成,但它會一直接近完成,如果它是正確完成):
namespace TrainRoute
{
class Route
{
Stops root;
public LinkedList<Stops> linkedList = new LinkedList<Stops>();
public Stops MakeNewStop(string stopName)
{
Stops stopWithStopName = new Stops(stopName);
return stopWithStopName;
}
public void AddStops(Stops stopIWantToAdd)
{
if (linkedList.Count == 0)
{
linkedList.AddFirst(stopIWantToAdd);
}
else
{
//stopIWantToAdd.prevStop = linkedList.Last();
linkedList.AddLast(stopIWantToAdd);
}
}
public void StopRelationships()
{
for (int i = 0; i < linkedList.Count; i++)
{
if (linkedList.ElementAt<Stops>(i).nextStop == null && linkedList.ElementAt<Stops>((i + 1)) != null)
{
linkedList.ElementAt<Stops>(i).nextStop = linkedList.ElementAt<Stops>((i + 1));
}
if (linkedList.ElementAt<Stops>((i - 1)) != null)
{
linkedList.ElementAt<Stops>(i).prevStop = linkedList.ElementAt<Stops>(i - 1);
}
}
}
public void Print()
{
if (linkedList != null)
{
foreach (var item in linkedList)
{
Console.WriteLine("Stop name: " + item.stopName);
}
}
}
public int StopPosition(string usersInput)
{
int position = 0;
for (int i = 0; i < linkedList.Count; i++)
{
if (linkedList.ElementAt<Stops>(i).stopName == usersInput)
{
position = i;
break;
}
}
return position;
}
public int MoveForward(int indexPosition)
{
Console.WriteLine("The train is now at " +linkedList.ElementAt<Stops>(indexPosition).nextStop.stopName);
return (indexPosition + 1);
}
public int MoveBackwords(int indexPosition)
{
Console.WriteLine("The train is now at " + linkedList.ElementAt<Stops>(indexPosition).prevStop.stopName);
return (indexPosition - 1);
}
public bool VerifyRoute(int indexPosition, string prevOrForward)
{
if (prevOrForward.Contains("forward"))
{
if (linkedList.ElementAt<Stops>((indexPosition+1)) != null)
{
return true;
}
}
else
{
if (linkedList.ElementAt<Stops>((indexPosition-1)) != null)
{
return true;
}
}
return false;
}
}
}
我也不允許使用鏈表類,但我m使用鏈表(我不是100%確定這意味着什麼)。
任何和所有的意見/幫助提供將不勝感激!
您的任務是**使用**鏈接列表或**實現**鏈接列表? –
聽起來像你的老師(假設這是家庭作業)期望你實現一個鏈表類,而不是使用提供的.NET類。 –
@ LasseV.Karlsen我剛問過,得到了一個非常令人困惑的答案,但幸運的是,他說最好的學習方法是建立它。那麼,即刻使我的所有代碼無用:( – Zain