Начало Lab3. Создание SetGeneric

This commit is contained in:
malimova 2023-12-10 13:50:55 +04:00
parent c3bb9c66cc
commit 15200db2d1

View File

@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirBomber
{
internal class SetGeneric<T>
where T : class
{
private readonly T?[] _places;
public int Count => _places.Length;
public SetGeneric(int count)
{
_places = new T?[count];
}
public int Insert(T plane)
{
return Insert(plane, 0);
}
public int Insert(T plane, int position)
{
int NoEmpty = 0, temp = 0;
for (int i = position; i < Count; i++)
{
if (_places[i] != null) NoEmpty++;
}
if (NoEmpty == Count - position) return -1;
if (position < Count && position >= 0)
{
for (int j = position; j < Count; j++)
{
if (_places[j] == null)
{
temp = j;
break;
}
}
for (int i = temp; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = plane;
return position;
}
return -1;
}
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];
}
}
}