From 572df7efe3aaf98609524358327f8435f94664d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=20=D0=91=D0=BE=D0=BD=D0=B4=D0=B0?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Sun, 23 Oct 2022 18:37:20 +0400 Subject: [PATCH] 101 --- WarmlyShip/WarmlyShip/SetShipGeneric.cs | 26 ++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/WarmlyShip/WarmlyShip/SetShipGeneric.cs b/WarmlyShip/WarmlyShip/SetShipGeneric.cs index 86ad35e..8c4e42f 100644 --- a/WarmlyShip/WarmlyShip/SetShipGeneric.cs +++ b/WarmlyShip/WarmlyShip/SetShipGeneric.cs @@ -17,16 +17,28 @@ namespace WarmlyShip _places = new T[count]; } - public bool Insert(T ship) - { - _places[0] = ship; - return true; - } - private bool CanInsert(int position) { for (int i = position; i < _places.Length; ++i) - if (_places[i] != null) return true; + if (_places[i] == null) return true; + return false; + } + + public bool Insert(T ship) + { + if (CanInsert(0)) + { + for (int i = _places.Length - 1; i > 0; --i) + { + if (_places[i] == null) + { + _places[i] = _places[i - 1]; + _places[i - 1] = null; + } + } + _places[0] = ship; + return true; + } return false; }