Я пытаюсь реализовать Token Ring в C#. Я думал о проблеме следующим образом: я добавляю числа p c в список (называемый PcSubList) и p c Ip в другом списке (IpSubList). Теперь, когда я ввожу источник P c 4, например, и назначение p c 2, программа работает нормально, но я ввожу источник p c 1, а затем я ввожу p c destination 5, он попадает в бесконечное время l oop.
Этот код пересекает список:
public void traverseList()
{
int pcSourceNumber = Int32.Parse(source);
int pcDestinationNumber = Int32.Parse(destination);
int i = pcSourceNumber;
int f = pcDestinationNumber - 1;
while (!String.IsNullOrEmpty(message))
{
for(int j = pcSourceNumber; j < pc.PcSubList.Count; j++) {
Console.WriteLine("PC number {0} ", pc.PcSubList[j]);
Console.WriteLine("Source IP: {0} ", pc.IpSubList[j]);
Console.WriteLine("Destination IP: {0} ", pc.IpSubList[pcDestinationNumber]);
}
if (destination.Equals(pc.PcSubList[i]))
{
//Console.WriteLine("PC number {0} ", pc.PcSubList[i]);
Console.WriteLine("The message is : {0} ", message);
}
if (i == pc.PcSubList.Count - 1)
{
i = 0; // if the pc index gets to the final element of the list, start reiterating
}
if (destination.Equals(pc.PcSubList[i]) && i != pcSourceNumber)
{
while (!String.IsNullOrEmpty(message))
{
if (i != pcSourceNumber)
{
Console.WriteLine("PC number {0} ", pc.PcSubList[i]);
Console.WriteLine("Source IP: {0} ", pc.IpSubList[i]);
Console.WriteLine("Destination IP: {0} ", pc.IpSubList[pcDestinationNumber]);
}
if (i == pcSourceNumber)
{
Console.WriteLine("------------------------");
Console.WriteLine("REACHED SOURCE AGAIN. PC : {0} ", pc.PcSubList[i]);
Console.WriteLine("Source PC IP : -");
Console.WriteLine("Destination PC IP : -");
Console.WriteLine("Message : -");
message = null;
}
i++;
}
i++;
}
}
}
}
Не могли бы вы мне помочь? Я просто не могу понять, что я делаю неправильно, или, может быть, мне нужен другой подход?
Спасибо!