From 15200db2d15db18b3cca25f9fe4355a661dc30af Mon Sep 17 00:00:00 2001 From: malimova Date: Sun, 10 Dec 2023 13:50:55 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D1=87=D0=B0=D0=BB=D0=BE=20Lab3.=20?= =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5=20SetGeneric?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AirBomber/AirBomber/SetGeneric.cs | 69 +++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 AirBomber/AirBomber/SetGeneric.cs diff --git a/AirBomber/AirBomber/SetGeneric.cs b/AirBomber/AirBomber/SetGeneric.cs new file mode 100644 index 0000000..b182f0c --- /dev/null +++ b/AirBomber/AirBomber/SetGeneric.cs @@ -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 + 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]; + } + } +}