ISEbd-22 Alimova M.S. Lab Work 03 Hard #3
69
AirBomber/AirBomber/SetGeneric.cs
Normal file
69
AirBomber/AirBomber/SetGeneric.cs
Normal 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];
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user