diff --git a/hw4UniqueList/hw4UniqueList.Test/ListTest.cs b/hw4UniqueList/hw4UniqueList.Test/ListTest.cs new file mode 100644 index 0000000..1eb6093 --- /dev/null +++ b/hw4UniqueList/hw4UniqueList.Test/ListTest.cs @@ -0,0 +1,102 @@ +using NUnit.Framework; +using System; + +namespace Hw4UniqueList.Test +{ + public class ListTests + { + private List list; + + [SetUp] + public void Setup() + { + list = new List(); + list.Insert(0, 2); + list.Insert(1, 4); + list.Insert(2, 7); + list.Insert(3, 8); + list.Insert(2, 6); + list.Insert(1, 3); + list.Insert(0, 1); + list.Insert(4, 5); + } + + [TestCase] + public void TestInsert() + { + for (int i = 1; i < 9; ++i) + { + Assert.AreEqual(i, list.GetValueByIndex(i)); + } + } + + [TestCase] + public void TestDeleteByIndex() + { + list.DeleteByIndex(2); + list.DeleteByIndex(7); + list.DeleteByIndex(1); + Assert.IsFalse(list.Contains(2)); + Assert.IsFalse(list.Contains(8)); + Assert.IsFalse(list.Contains(1)); + } + + [TestCase] + public void TestDeleteByIndexException() + { + Assert.Throws(() => list.DeleteByIndex(12)); + } + + [TestCase] + public void TestDeleteByValueException() + { + Assert.Throws(() => list.DeleteByValue(12)); + } + + [TestCase] + public void TestDeleteByValue() + { + list.DeleteByValue(2); + list.DeleteByValue(8); + list.DeleteByValue(1); + Assert.IsFalse(list.Contains(2)); + Assert.IsFalse(list.Contains(8)); + Assert.IsFalse(list.Contains(1)); + } + + [TestCase] + public void TestChangeByIndexIndexException() + { + Assert.Throws(() => list.ChangeByIndex(12, 1)); + } + + [TestCase] + public void TestChangeByIndex() + { + for (int i = 1; i < 9; ++i) + { + list.ChangeByIndex(i, i + 10); + } + for (int i = 1; i < 9; ++i) + { + Assert.AreEqual(i + 10, list.GetValueByIndex(i)); + } + } + + [TestCase] + public void TestGetSize() + { + Assert.AreEqual(8, list.GetSize()); + } + + [TestCase] + public void TestIsEmpty() + { + for (int i = 1; i < 9; ++i) + { + list.DeleteByValue(i); + } + Assert.IsTrue(list.IsEmpty()); + } + } +} \ No newline at end of file diff --git a/hw4UniqueList/hw4UniqueList.Test/UniqueListTest.cs b/hw4UniqueList/hw4UniqueList.Test/UniqueListTest.cs new file mode 100644 index 0000000..f2499b3 --- /dev/null +++ b/hw4UniqueList/hw4UniqueList.Test/UniqueListTest.cs @@ -0,0 +1,75 @@ +using NUnit.Framework; +using System; + +namespace Hw4UniqueList.Test +{ + class UniqueListTest + { + private UniqueList list; + + [SetUp] + public void Setup() + { + list = new UniqueList(); + list.Insert(0, 2); + list.Insert(1, 4); + list.Insert(2, 7); + list.Insert(3, 8); + list.Insert(2, 6); + list.Insert(1, 3); + list.Insert(0, 1); + list.Insert(4, 5); + } + + [TestCase] + public void TestInsertValueAgain() + { + Assert.Throws(() => list.Insert(2, 3)); + } + + [TestCase] + public void TestInsert() + { + list.Insert(3, 12); + Assert.IsTrue(list.Contains(12)); + } + + [TestCase] + public void TestDeleteByValueException() + { + Assert.Throws(() => list.DeleteByValue(12)); + } + + [TestCase] + public void TestDeleteByValue() + { + list.DeleteByValue(6); + Assert.IsFalse(list.Contains(6)); + } + + [TestCase] + public void TestDeleteByIndexException() + { + Assert.Throws(() => list.DeleteByIndex(12)); + } + + [TestCase] + public void TestDeleteByIndex() + { + list.DeleteByValue(3); + Assert.IsFalse(list.Contains(3)); + } + + [TestCase] + public void TestChangeByIndexIndexException() + { + Assert.Throws(() => list.ChangeByIndex(12, 1)); + } + + [TestCase] + public void TestChangeByIndexValueException() + { + Assert.Throws(() => list.ChangeByIndex(3, 5)); + } + } +} diff --git a/hw4UniqueList/hw4UniqueList.Test/hw4UniqueList.Test.csproj b/hw4UniqueList/hw4UniqueList.Test/hw4UniqueList.Test.csproj new file mode 100644 index 0000000..14a16ae --- /dev/null +++ b/hw4UniqueList/hw4UniqueList.Test/hw4UniqueList.Test.csproj @@ -0,0 +1,19 @@ + + + + netcoreapp3.1 + + false + + + + + + + + + + + + + diff --git a/hw4UniqueList/hw4UniqueList.sln b/hw4UniqueList/hw4UniqueList.sln new file mode 100644 index 0000000..86a7073 --- /dev/null +++ b/hw4UniqueList/hw4UniqueList.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31005.135 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "hw4UniqueList", "hw4UniqueList\hw4UniqueList.csproj", "{D9F81E15-AB31-4DF2-9B17-80BD75E19694}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "hw4UniqueList.Test", "hw4UniqueList.Test\hw4UniqueList.Test.csproj", "{581F2A1F-653B-433F-B7EC-E495133D0488}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {D9F81E15-AB31-4DF2-9B17-80BD75E19694}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D9F81E15-AB31-4DF2-9B17-80BD75E19694}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D9F81E15-AB31-4DF2-9B17-80BD75E19694}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D9F81E15-AB31-4DF2-9B17-80BD75E19694}.Release|Any CPU.Build.0 = Release|Any CPU + {581F2A1F-653B-433F-B7EC-E495133D0488}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {581F2A1F-653B-433F-B7EC-E495133D0488}.Debug|Any CPU.Build.0 = Debug|Any CPU + {581F2A1F-653B-433F-B7EC-E495133D0488}.Release|Any CPU.ActiveCfg = Release|Any CPU + {581F2A1F-653B-433F-B7EC-E495133D0488}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {55347B38-1DD7-4168-8FF4-D3C507E77DE3} + EndGlobalSection +EndGlobal diff --git a/hw4UniqueList/hw4UniqueList/List.cs b/hw4UniqueList/hw4UniqueList/List.cs new file mode 100644 index 0000000..9f970d1 --- /dev/null +++ b/hw4UniqueList/hw4UniqueList/List.cs @@ -0,0 +1,214 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Hw4UniqueList +{ + /// + /// Список + /// + public class List + { + private class Node + { + public int Value { get; set; } + + public Node Next { get; set; } + + public Node(int value, Node node) + { + Value = value; + Next = node; + } + } + + private int size; + + private Node head; + + /// + /// Возвращает размер списка + /// + public int GetSize() + => size; + + /// + /// Проверка на пустоту списка + /// + /// возвращает true, если список пуст + public bool IsEmpty() + => head == null; + + private void CheckSize(int index) + { + if (index > size || index < 0) + { + throw new IndexOutOfRangeException(); + } + } + + /// + /// Функция вставки + /// + /// позиция узла + /// значение в узле + public virtual void Insert(int position, int value) + { + CheckSize(position); + if (IsEmpty() && position == 0) + { + head = new Node(value, null); + size++; + return; + } + else if (IsEmpty() && position != 0) + { + throw new IndexOutOfRangeException(); + } + var runner = head; + if (position == 0) + { + var node = new Node(value, head); + head = node; + size++; + return; + } + int index = 1; + while (index < position) + { + if (runner.Next != null) + { + runner = runner.Next; + index++; + } + else + { + throw new IndexOutOfRangeException(); + } + } + var newNode = new Node(value, runner.Next); + runner.Next = newNode; + size++; + } + + /// + /// Функция удаления по индексу + /// + /// индекс узла + /// возвращает значение в узле + public virtual int DeleteByIndex(int position) + { + CheckSize(position); + if (IsEmpty()) + { + throw new IndexOutOfRangeException(); + } + if (position == 1) + { + var node = head.Next; + var result = head.Value; + head = node; + size--; + return result; + } + var runner = head; + int index = 1; + while (index < position - 1) + { + if (runner.Next != null) + { + runner = runner.Next; + index++; + } + else + { + throw new IndexOutOfRangeException(); + } + } + var returnResult = runner.Next.Value; + runner.Next = runner.Next.Next; + size--; + return returnResult; + } + + /// + /// Функция удаление по значению + /// + /// значение, которое надо удалить + public virtual void DeleteByValue(int value) + { + if (head.Value == value) + { + head = head.Next; + size--; + return; + } + var runner = head; + while (runner.Next != null) + { + if (runner.Next.Value == value) + { + runner.Next = runner.Next.Next; + size--; + return; + } + runner = runner.Next; + } + throw new ValueDoesNotExistException(); + } + + private Node GoToPosition(int position, Node node) + { + int index = 1; + while (index < position) + { + node = node.Next; + index++; + } + return node; + } + + /// + /// Смена значения в узле по индексу + /// + /// индекс узла + /// новое значение + public virtual void ChangeByIndex(int position, int value) + { + CheckSize(position); + var runner = GoToPosition(position, head); + runner.Value = value; + } + + /// + /// получить значение узла по его индексу + /// + /// индекс узла + /// возвращает значение в узле + public int GetValueByIndex(int position) + { + CheckSize(position); + var runner = GoToPosition(position, head); + return runner.Value; + } + + /// + /// проверка, есть ли такое значение в списке + /// + /// значение, которое хотим проверить + /// возвращает true, если нашелся узел с таким значением + public bool Contains(int value) + { + var runner = head; + while (runner != null) + { + if (runner.Value == value) + { + return true; + } + runner = runner.Next; + } + return false; + } + } +} diff --git a/hw4UniqueList/hw4UniqueList/Program.cs b/hw4UniqueList/hw4UniqueList/Program.cs new file mode 100644 index 0000000..b71895f --- /dev/null +++ b/hw4UniqueList/hw4UniqueList/Program.cs @@ -0,0 +1,23 @@ +using System; + +namespace Hw4UniqueList +{ + class Program + { + static void Main(string[] args) + { + var list = new List(); + list.Insert(0, 2); + list.Insert(1, 4); + list.Insert(2, 7); + list.Insert(3, 8); + list.Insert(2, 6); + list.Insert(1, 3); + list.Insert(0, 1); + list.Insert(4, 5); + list.DeleteByValue(2); + list.DeleteByValue(7); + list.DeleteByValue(1); + } + } +} diff --git a/hw4UniqueList/hw4UniqueList/UniqueList.cs b/hw4UniqueList/hw4UniqueList/UniqueList.cs new file mode 100644 index 0000000..bcb7b5d --- /dev/null +++ b/hw4UniqueList/hw4UniqueList/UniqueList.cs @@ -0,0 +1,34 @@ + using System; +using System.Collections.Generic; +using System.Text; + +namespace Hw4UniqueList +{ + /// + /// Список без повторяющихся значений + /// + public class UniqueList : List + { + public override void Insert(int position, int value) + { + if (Contains(value)) + { + throw new ValueIsAlreadyInListException("Такое значение уже есть в списке!"); + } + base.Insert(position, value); + } + + public override void ChangeByIndex(int position, int value) + { + if (GetValueByIndex(position) == value) + { + return; + } + if (Contains(value)) + { + throw new ValueIsAlreadyInListException("Такое значение уже есть в списке!"); + } + base.ChangeByIndex(position, value); + } + } +} diff --git a/hw4UniqueList/hw4UniqueList/ValueDoesNotExistException.cs b/hw4UniqueList/hw4UniqueList/ValueDoesNotExistException.cs new file mode 100644 index 0000000..5d8a860 --- /dev/null +++ b/hw4UniqueList/hw4UniqueList/ValueDoesNotExistException.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Hw4UniqueList +{ + /// + /// исключение для удаления элемента из списка, когда значение не содержиться в списке + /// + public class ValueDoesNotExistException : Exception + { + public ValueDoesNotExistException() + { + } + + public ValueDoesNotExistException(string message) + : base(message) + { + } + } +} diff --git a/hw4UniqueList/hw4UniqueList/ValueIsAlreadyInListException.cs b/hw4UniqueList/hw4UniqueList/ValueIsAlreadyInListException.cs new file mode 100644 index 0000000..4940ed7 --- /dev/null +++ b/hw4UniqueList/hw4UniqueList/ValueIsAlreadyInListException.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Hw4UniqueList +{ + /// + /// исключение для unique list, когда значение уже содержиться в списке + /// + public class ValueIsAlreadyInListException : Exception + { + public ValueIsAlreadyInListException() + { + } + + public ValueIsAlreadyInListException(string message) + : base(message) + { + } + } +} diff --git a/hw4UniqueList/hw4UniqueList/hw4UniqueList.csproj b/hw4UniqueList/hw4UniqueList/hw4UniqueList.csproj new file mode 100644 index 0000000..c73e0d1 --- /dev/null +++ b/hw4UniqueList/hw4UniqueList/hw4UniqueList.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + +