Добавлены операции со списком

This commit is contained in:
tyxz0 2024-04-25 21:11:30 +04:00
parent 59d11e7b84
commit 85d39d43aa

View File

@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DoubleDeckerBus.CollectionGenericObjects;
public class ListGenericObjects<T> : ICollectionGenericObjects<T>
where T : class
{
private readonly List<T?> _collection;
private int _maxCount;
public int Count => _collection.Count;
public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } }
public ListGenericObjects()
{
_collection = new();
}
public T? Get(int position)
{
if (position < 0 || position >= Count)
{
return null;
}
return _collection[position];
}
public int Insert(T obj)
{
if (Count >= _maxCount)
{
return -1;
}
_collection.Add(obj);
return _collection.IndexOf(obj);
}
public int Insert(T obj, int position)
{
if (Count >= _maxCount || position < 0 || position > _maxCount)
{
return -1;
}
if (position > Count && position < _maxCount)
{
return Insert(obj);
}
int copy_of_position = position - 1;
while (position < Count)
{
if (_collection[position] == null)
{
_collection.Insert(position, obj);
return position;
}
position++;
}
while (copy_of_position > 0)
{
if (_collection[copy_of_position] == null)
{
_collection.Insert(copy_of_position, obj);
return copy_of_position;
}
copy_of_position--;
}
return -1;
}
public T? Remove(int position)
{
if (position < 0 || position >= Count)
{
return null;
}
T? removed_object = Get(position);
_collection.RemoveAt(position);
return removed_object;
}
}