Nie wiem jak mogę wstawiać obiekty klasy połączenie do listy. Chce żeby były wkładane według CzasuPoczatku. Wydaje mi się że muszę przeciążyć metodę Compare, ale nie wiem w jaki sposób to zrobić. W klasie list narzuciłem warunek na parametr T class List<T> where T : IComparable
using System;
using System.IO;
using System.Collections.Generic;
class List<T> where T : IComparable
{
#region Class Node
private class Node
{
private T _Item;
public T Item
{
get
{
return _Item;
}
set
{
_Item = value;
}
}
public Node(T t)
{
Item = t;
}
}
#endregion
#region Members
private int BufforSize;
private int ReSizeBuffor;
private int Numbers;
Node[] NewList;
#endregion
#region Constructors;
public List(int newBufforSize, int newReSizeBuffor)
{
if (newBufforSize <= 0 || newReSizeBuffor < 0) throw new ArgumentException();
BufforSize = newBufforSize;
ReSizeBuffor = newReSizeBuffor;
NewList = new Node[BufforSize];
Numbers = 0;
for (int i = 0; i < BufforSize; i++)
{
NewList[i] = null;
}
}
#endregion
#region Metods
public void Print()
{
for (int i = 0; i < Numbers; i++)
{
Console.WriteLine(NewList[i].Item);
}
}
public void AddNewItem(T t)
{
if (Numbers == BufforSize && ReSizeBuffor == 0) throw new OutOfMemoryException();
if (Numbers == BufforSize)
{
BufforSize += ReSizeBuffor;
System.Array.Resize<Node>(ref NewList, BufforSize);
}
if (Numbers >= 1 && Comparer<T>.Default.Compare(t, NewList[Numbers - 1].Item) < 0)
{
int i = Numbers - 1;
do
{
Node tempNode = NewList[i];
NewList[i] = new Node(t);
NewList[i + 1] = tempNode;
if (i == 0)
break;
i--;
}
while (Comparer<T>.Default.Compare(t, NewList[i].Item) < 0);
Numbers++;
}
else
{
Node tempNode = new Node(t);
NewList[Numbers] = tempNode;
Numbers++;
}
}
public void DeleteItem(int index)
{
index -= 1;
if (index > Numbers || index < 0)
throw new IndexOutOfRangeException();
for (int i = index; i < Numbers - 1; i++)
{
NewList[i] = NewList[i + 1];
}
NewList[Numbers - 1] = null;
Numbers--;
}
public T ShowHead()
{
return NewList[0].Item;
}
public T ShowTail()
{
return NewList[Numbers - 1].Item;
}
public int ShowNumbers()
{
return Numbers;
}
#endregion
}
#region Class TestList
class TestList
{
public static void Main()
{
try
{
List<int> lista = new List<int>(2, 0);
lista.AddNewItem(6);
lista.AddNewItem(3);
Console.WriteLine(lista.ShowHead());
Console.WriteLine(lista.ShowTail());
Console.WriteLine(lista.ShowNumbers());
lista.Print();
}
catch (OutOfMemoryException)
{
Console.WriteLine("Brak miejsca na liście");
}
catch (ArgumentException)
{
Console.WriteLine("Niedozwolone parametry konstruktora");
}
catch (IndexOutOfRangeException)
{
Console.WriteLine("Nie ma elementu na liście o tym indeksie");
}
}
}
#endregion
Oto klasa połączenie
using System;
class Polaczenie
{
private int Poczatek;
private int Koniec;
private double CzasPoczatku;
private double CzasKonca;
Polaczenie(int nowyPoczatek, int nowyKoniec, double nowyCzasPoczatku, double nowyCzasKonca)
{
Poczatek = nowyPoczatek;
Koniec = nowyKoniec;
CzasPoczatku = nowyCzasPoczatku;
CzasKonca = nowyCzasKonca;
}
public class TestPolaczenie
{
public static void main()
{
Polaczenie testowe = new Polaczenie(5, 5, 4.56, 7.87);
//List<Polaczenie> lista = new List<Polaczenie>(2, 0);
}
}
}