【发布时间】:2014-04-10 20:52:03
【问题描述】:
我被分配了以下任务:
我想构建一个链接列表的实现。具体来说,我希望它是一个双向链表。
我的任务: 您的程序应使用链表对使用链表的火车路线进行建模。 首先,用户将输入他们希望火车有多少站,以及每个站的名称。 然后程序应打印路线图。 完成后,他们然后输入他们想要开始的站点的名称。 从那里他们可以输入命令将火车向前移动到下一站或向后移动到上一站。
有人告诉我我没有正确完成这项任务,但我真的不明白怎么做,如果有人能解释我没有做的事情,我将不胜感激。
我的 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;
}
}
}
我也不允许使用链接列表类,但我要使用链接列表(我不是 100% 确定这意味着什么)。
我们将不胜感激提供的任何和所有建议/帮助!
【问题讨论】:
-
您的任务是使用链表还是实现链表?
-
听起来你的老师(假设这是家庭作业)希望你实现一个链表类,而不是使用提供的 .NET 类。
-
@LasseV.Karlsen 我刚问过,得到了一个非常令人困惑的答案,但幸运的是,最后他说学习如何使用某些东西的最佳方法是构建它以便实现。好吧,这立即使我所有的代码都没用了:(
-
@user3245390:不一定,但毫无疑问,它需要进行一些重大修改。
-
同意其他评论者的观点,但实际上,您应该与您的老师澄清这一点(以获得最高分)。澄清需求是软件工程的重要组成部分,但通常做得不好。因此,将其视为实践...作为记录,我同意上面的马特,您需要使用适当的 API 实现一个链表
MyLinkedList来完成要求。您的要求是能够记录火车路线,然后根据要求返回路线。在考虑实现时,用节点和指针绘制图片并计算出它们是如何移动的。也可以重复使用它们。