PIbd-21_Krasnikov_Lab1.base/WinFormsApp1/SetTraktorGeneric.cs
2022-11-14 21:25:33 +04:00

63 lines
1.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WinFormsApp1
{
internal class SetTraktorGeneric<T>
where T : class
{
private readonly T[] _places;
public int Count => _places.Length;
private int TractorPlaces = 0;
public SetTraktorGeneric(int count)
{
_places = new T[count];
}
public int Insert(T tractor)
{
return Insert(tractor, 0);
}
public int Insert(T tractor, int position)
{
if (position < 0 || position >= _places.Length || TractorPlaces == _places.Length)
{
return -1;
}
TractorPlaces++;
while (_places[position] != null)
{
for (int i = _places.Length - 1; i > 0; --i)
{
if (_places[i] == null && _places[i - 1] != null)
{
_places[i] = _places[i - 1];
_places[i - 1] = null;
}
}
}
_places[position] = tractor;
return position;
}
public T Remove(int position)
{
if (position < 0 || position >= _places.Length) return null;
T savedTractor = _places[position];
_places[position] = null;
return savedTractor;
}
public T Get(int position)
{
if (position < 0 || position >= _places.Length) return null;
return _places[position];
}
}
}