PIbd-23_Vrazhkin_S_A_Electr.../lab1/Generics/SetGeneric.cs

52 lines
1.3 KiB
C#
Raw Permalink Normal View History

2023-10-17 13:49:31 +04:00
namespace ElectricLocomotive;
2023-10-31 11:39:52 +04:00
public class SetGeneric <T> where T : class
2023-10-17 13:49:31 +04:00
{
private readonly T?[] _places;
public int Count => _places.Length;
public SetGeneric(int count)
{
_places = new T?[count];
}
public int Insert(T electricLocomotiv)
{
if (_places[Count - 1] != null)
return -1;
return Insert(electricLocomotiv, 0);
}
public int Insert(T electricLocomotiv, int position)
{
if (!(position >= 0 && position < Count))
return -1;
if (_places[position] != null) {
int ind = position;
while (ind < Count && _places[ind] != null)
ind++;
if (ind == Count)
return -1;
for (int i = ind - 1; i >= position; i--)
_places[i + 1] = _places[i];
}
_places[position] = electricLocomotiv;
return position;
}
public bool Remove(int position)
{
if (!(position >= 0 && position < Count) || _places[position] == null)
return false;
_places[position] = null;
return true;
}
public T? Get(int position)
{
if (!(position >= 0 && position < Count))
return null;
return _places[position];
}
}