PIbd-21_Valitov_D.F_Sailboa.../Sailboat/SetBoatsGeneric.cs
2023-04-14 21:28:29 +04:00

70 lines
1.6 KiB
C#

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