From c1754dc56dcf1a56fcf55d69e66a94336d39ac0c Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Sat, 3 Apr 2021 00:12:51 +0300 Subject: [PATCH 1/6] create a project --- hw4UniqueList/hw4UniqueList.sln | 25 +++++++++++++++++++ hw4UniqueList/hw4UniqueList/List.cs | 15 +++++++++++ hw4UniqueList/hw4UniqueList/Program.cs | 12 +++++++++ .../hw4UniqueList/hw4UniqueList.csproj | 8 ++++++ 4 files changed, 60 insertions(+) create mode 100644 hw4UniqueList/hw4UniqueList.sln create mode 100644 hw4UniqueList/hw4UniqueList/List.cs create mode 100644 hw4UniqueList/hw4UniqueList/Program.cs create mode 100644 hw4UniqueList/hw4UniqueList/hw4UniqueList.csproj diff --git a/hw4UniqueList/hw4UniqueList.sln b/hw4UniqueList/hw4UniqueList.sln new file mode 100644 index 0000000..49c26b9 --- /dev/null +++ b/hw4UniqueList/hw4UniqueList.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31005.135 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "hw4UniqueList", "hw4UniqueList\hw4UniqueList.csproj", "{D9F81E15-AB31-4DF2-9B17-80BD75E19694}" +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 + 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..3b27566 --- /dev/null +++ b/hw4UniqueList/hw4UniqueList/List.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace hw4UniqueList +{ + public class List + { + private class Node + { + + } + + } +} diff --git a/hw4UniqueList/hw4UniqueList/Program.cs b/hw4UniqueList/hw4UniqueList/Program.cs new file mode 100644 index 0000000..a0f7d79 --- /dev/null +++ b/hw4UniqueList/hw4UniqueList/Program.cs @@ -0,0 +1,12 @@ +using System; + +namespace hw4UniqueList +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + } + } +} 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 + + + From 4b754a1b8f55cecdd61a78d9a3d7a0178ce0b9e2 Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Sat, 3 Apr 2021 01:41:18 +0300 Subject: [PATCH 2/6] add Insert and Delete --- hw4UniqueList/hw4UniqueList/List.cs | 120 +++++++++++++++++++++++++++- 1 file changed, 119 insertions(+), 1 deletion(-) diff --git a/hw4UniqueList/hw4UniqueList/List.cs b/hw4UniqueList/hw4UniqueList/List.cs index 3b27566..eb02f66 100644 --- a/hw4UniqueList/hw4UniqueList/List.cs +++ b/hw4UniqueList/hw4UniqueList/List.cs @@ -6,9 +6,127 @@ namespace hw4UniqueList { public class List { + private int Size { get; set; } private class Node { - + public int Value { get; set; } + + public Node Next { get; set; } + + public Node(int value, Node node) + { + Value = value; + Next = node; + } + } + + private Node head; + + private Node runner; + + private Node GetNext(Node node) + => node.Next; + + private bool IsEmpty() + => head == null; + + public List() + { + Size = 0; + head = null; + } + + /// + /// + /// + /// + /// + public void Insert(int possition, int value) + { + if (possition > Size) + { + throw new Exception(); + } + if (IsEmpty() && possition == 0) + { + head = new Node(value, null); + } + else if (IsEmpty() && possition != 0) + { + throw new Exception(); + } + runner = head; + if (possition == 0) + { + var node = new Node(value, head); + head = node; + return; + } + int index = 1; + while (index < possition) + { + if (GetNext(runner) != null) + { + runner = runner.Next; + index++; + } + else + { + throw new Exception(); + } + } + var NewNode = new Node(value, runner.Next); + runner.Next = NewNode; + } + + /// + /// + /// + /// + /// + public int Delete(int possition) + { + if (possition > Size) + { + throw new Exception(); + } + if (IsEmpty()) + { + throw new Exception(); + } + if (possition == 1) + { + var node = head.Next; + var result = head.Value; + head = node; + return result; + } + int index = 1; + while (index < possition - 1) + { + if (GetNext(runner) != null) + { + runner = runner.Next; + index++; + } + else + { + throw new Exception(); + } + } + var returnResult = runner.Next.Value; + runner.Next = runner.Next.Next; + return returnResult; + } + + /// + /// + /// + /// + /// + public void ChangeByIndex(int possition, int value) + { + } } From 108bc0e0a478e317efadf81eef7a41163f7f2fca Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Sun, 4 Apr 2021 01:21:02 +0300 Subject: [PATCH 3/6] write Tests --- hw4UniqueList/hw4UniqueList.Test/ListTest.cs | 62 ++++++++++ .../hw4UniqueList.Test/UniqueListTest.cs | 75 +++++++++++++ .../hw4UniqueList.Test.csproj | 19 ++++ hw4UniqueList/hw4UniqueList.sln | 8 +- hw4UniqueList/hw4UniqueList/List.cs | 106 +++++++++++++++--- hw4UniqueList/hw4UniqueList/Program.cs | 37 +++++- hw4UniqueList/hw4UniqueList/UniqueList.cs | 49 ++++++++ .../ValueDoesNotExistException.cs | 18 +++ .../ValueIsAlreadyInListException.cs | 18 +++ 9 files changed, 375 insertions(+), 17 deletions(-) create mode 100644 hw4UniqueList/hw4UniqueList.Test/ListTest.cs create mode 100644 hw4UniqueList/hw4UniqueList.Test/UniqueListTest.cs create mode 100644 hw4UniqueList/hw4UniqueList.Test/hw4UniqueList.Test.csproj create mode 100644 hw4UniqueList/hw4UniqueList/UniqueList.cs create mode 100644 hw4UniqueList/hw4UniqueList/ValueDoesNotExistException.cs create mode 100644 hw4UniqueList/hw4UniqueList/ValueIsAlreadyInListException.cs diff --git a/hw4UniqueList/hw4UniqueList.Test/ListTest.cs b/hw4UniqueList/hw4UniqueList.Test/ListTest.cs new file mode 100644 index 0000000..437f64d --- /dev/null +++ b/hw4UniqueList/hw4UniqueList.Test/ListTest.cs @@ -0,0 +1,62 @@ +using NUnit.Framework; + +namespace hw4UniqueList.Test +{ + public class Tests + { + 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.IsConsist(2) || list.IsConsist(8) || list.IsConsist(1)); + } + + [TestCase] + public void TestDeleteByValue() + { + list.DeleteByValue(2); + list.DeleteByValue(8); + list.DeleteByValue(1); + Assert.IsFalse(list.IsConsist(2) || list.IsConsist(8) || list.IsConsist(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)); + } + } + } +} \ 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..6ee53b1 --- /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.IsConsist(12)); + } + + [TestCase] + public void TestDeleteByValueException() + { + Assert.Throws(() => list.DeleteByValue(12)); + } + + [TestCase] + public void TestDeleteByValue() + { + list.DeleteByValue(6); + Assert.IsFalse(list.IsConsist(6)); + } + + [TestCase] + public void TestDeleteByIndexException() + { + Assert.Throws(() => list.DeleteByIndex(12)); + } + + [TestCase] + public void TestDeleteByIndex() + { + list.DeleteByValue(3); + Assert.IsFalse(list.IsConsist(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 index 49c26b9..86a7073 100644 --- a/hw4UniqueList/hw4UniqueList.sln +++ b/hw4UniqueList/hw4UniqueList.sln @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.31005.135 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "hw4UniqueList", "hw4UniqueList\hw4UniqueList.csproj", "{D9F81E15-AB31-4DF2-9B17-80BD75E19694}" +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 @@ -15,6 +17,10 @@ Global {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 diff --git a/hw4UniqueList/hw4UniqueList/List.cs b/hw4UniqueList/hw4UniqueList/List.cs index eb02f66..af97ede 100644 --- a/hw4UniqueList/hw4UniqueList/List.cs +++ b/hw4UniqueList/hw4UniqueList/List.cs @@ -24,12 +24,23 @@ public Node(int value, Node node) private Node runner; + public int GetSize() + => Size; + private Node GetNext(Node node) => node.Next; private bool IsEmpty() => head == null; + private void CheckSize(int index) + { + if (index > Size || index < 0) + { + throw new IndexOutOfRangeException(); + } + } + public List() { Size = 0; @@ -41,25 +52,25 @@ public List() /// /// /// - public void Insert(int possition, int value) + public virtual void Insert(int possition, int value) { - if (possition > Size) - { - throw new Exception(); - } + CheckSize(possition); if (IsEmpty() && possition == 0) { head = new Node(value, null); + Size++; + return; } else if (IsEmpty() && possition != 0) { - throw new Exception(); + throw new IndexOutOfRangeException(); } runner = head; if (possition == 0) { var node = new Node(value, head); head = node; + Size++; return; } int index = 1; @@ -72,11 +83,12 @@ public void Insert(int possition, int value) } else { - throw new Exception(); + throw new IndexOutOfRangeException(); } } var NewNode = new Node(value, runner.Next); runner.Next = NewNode; + Size++; } /// @@ -84,23 +96,22 @@ public void Insert(int possition, int value) /// /// /// - public int Delete(int possition) + public virtual int DeleteByIndex(int possition) { - if (possition > Size) - { - throw new Exception(); - } + CheckSize(possition); if (IsEmpty()) { - throw new Exception(); + throw new IndexOutOfRangeException(); } if (possition == 1) { var node = head.Next; var result = head.Value; head = node; + Size--; return result; } + runner = head; int index = 1; while (index < possition - 1) { @@ -111,23 +122,88 @@ public int Delete(int possition) } else { - throw new Exception(); + 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; + } + runner = head; + while (GetNext(runner) != null) + { + if (GetNext(runner).Value == value) + { + runner.Next = GetNext(runner).Next; + Size--; + return; + } + runner = GetNext(runner); + } + } + /// /// /// /// /// - public void ChangeByIndex(int possition, int value) + public virtual void ChangeByIndex(int possition, int value) { + CheckSize(possition); + int index = 1; + runner = head; + while (index < possition && GetNext(runner) != null) + { + runner = GetNext(runner); + index++; + } + runner.Value = value; + } + /// + /// + /// + /// + /// + public int GetValueByIndex(int possition) + { + CheckSize(possition); + int index = 1; + runner = head; + while (index < possition && GetNext(runner) != null) + { + runner = GetNext(runner); + index++; + } + return runner.Value; } + public bool IsConsist(int value) + { + runner = head; + while (runner != null) + { + if (runner.Value == value) + { + return true; + } + runner = GetNext(runner); + } + return false; + } } } diff --git a/hw4UniqueList/hw4UniqueList/Program.cs b/hw4UniqueList/hw4UniqueList/Program.cs index a0f7d79..0d4568b 100644 --- a/hw4UniqueList/hw4UniqueList/Program.cs +++ b/hw4UniqueList/hw4UniqueList/Program.cs @@ -6,7 +6,42 @@ class Program { static void Main(string[] args) { - Console.WriteLine("Hello World!"); + 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); + if (list.IsConsist(1)) + { + Console.WriteLine("TRUE"); + } + else + { + Console.WriteLine("FALSE"); + } + if (list.IsConsist(2)) + { + Console.WriteLine("TRUE"); + } + else + { + Console.WriteLine("FALSE"); + } + if (list.IsConsist(7)) + { + Console.WriteLine("TRUE"); + } + else + { + Console.WriteLine("FALSE"); + } } } } diff --git a/hw4UniqueList/hw4UniqueList/UniqueList.cs b/hw4UniqueList/hw4UniqueList/UniqueList.cs new file mode 100644 index 0000000..c4da22a --- /dev/null +++ b/hw4UniqueList/hw4UniqueList/UniqueList.cs @@ -0,0 +1,49 @@ + using System; +using System.Collections.Generic; +using System.Text; + +namespace hw4UniqueList +{ + public class UniqueList : List + { + public override void Insert(int possition, int value) + { + if (IsConsist(value)) + { + throw new ValueIsAlreadyInListException("Такое значение уже есть в списке!"); + } + base.Insert(possition, value); + } + + public override int DeleteByIndex(int possition) + { + if (possition > GetSize() || possition < 1) + { + throw new IndexOutOfRangeException(); + } + return base.DeleteByIndex(possition); + } + + public override void DeleteByValue(int value) + { + if (!IsConsist(value)) + { + throw new ValueDoesNotExistException("Такого значения в списке не существует!"); + } + base.DeleteByValue(value); + } + + public override void ChangeByIndex(int possition, int value) + { + if (possition > GetSize() || possition < 1) + { + throw new IndexOutOfRangeException(); + } + else if (IsConsist(value)) + { + throw new ValueIsAlreadyInListException("Такое значение уже есть в списке!"); + } + base.ChangeByIndex(possition, value); + } + } +} diff --git a/hw4UniqueList/hw4UniqueList/ValueDoesNotExistException.cs b/hw4UniqueList/hw4UniqueList/ValueDoesNotExistException.cs new file mode 100644 index 0000000..645d5d0 --- /dev/null +++ b/hw4UniqueList/hw4UniqueList/ValueDoesNotExistException.cs @@ -0,0 +1,18 @@ +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..f5cc9b1 --- /dev/null +++ b/hw4UniqueList/hw4UniqueList/ValueIsAlreadyInListException.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace hw4UniqueList +{ + public class ValueIsAlreadyInListException : Exception + { + public ValueIsAlreadyInListException() + { + } + + public ValueIsAlreadyInListException(string message) + : base(message) + { + } + } +} From 054173b58ba94d7f72a1562ccaa480d53e4216c6 Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Mon, 5 Apr 2021 18:59:15 +0300 Subject: [PATCH 4/6] write test and comments --- hw4UniqueList/hw4UniqueList.Test/ListTest.cs | 38 ++++++++++++- hw4UniqueList/hw4UniqueList/List.cs | 59 +++++++++++++------- hw4UniqueList/hw4UniqueList/Program.cs | 24 -------- hw4UniqueList/hw4UniqueList/UniqueList.cs | 3 + 4 files changed, 78 insertions(+), 46 deletions(-) diff --git a/hw4UniqueList/hw4UniqueList.Test/ListTest.cs b/hw4UniqueList/hw4UniqueList.Test/ListTest.cs index 437f64d..ef289a1 100644 --- a/hw4UniqueList/hw4UniqueList.Test/ListTest.cs +++ b/hw4UniqueList/hw4UniqueList.Test/ListTest.cs @@ -1,4 +1,5 @@ using NUnit.Framework; +using System; namespace hw4UniqueList.Test { @@ -38,6 +39,18 @@ public void TestDeleteByIndex() Assert.IsFalse(list.IsConsist(2) || list.IsConsist(8) || list.IsConsist(1)); } + [TestCase] + public void TestDeleteByIndexException() + { + Assert.Throws(() => list.DeleteByIndex(12)); + } + + [TestCase] + public void TestDeleteByValueException() + { + Assert.Throws(() => list.DeleteByValue(12)); + } + [TestCase] public void TestDeleteByValue() { @@ -46,7 +59,14 @@ public void TestDeleteByValue() list.DeleteByValue(1); Assert.IsFalse(list.IsConsist(2) || list.IsConsist(8) || list.IsConsist(1)); } - [TestCase] + + [TestCase] + public void TestChangeByIndexIndexException() + { + Assert.Throws(() => list.ChangeByIndex(12, 1)); + } + + [TestCase] public void TestChangeByIndex() { for (int i = 1; i < 9; ++i) @@ -58,5 +78,21 @@ public void TestChangeByIndex() Assert.AreEqual(i + 10, list.GetValueByIndex(i)); } } + + [TestCase] + public void TestGetSize() + { + Assert.IsTrue(list.GetSize() == 8); + } + + [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/List.cs b/hw4UniqueList/hw4UniqueList/List.cs index af97ede..99641ae 100644 --- a/hw4UniqueList/hw4UniqueList/List.cs +++ b/hw4UniqueList/hw4UniqueList/List.cs @@ -4,9 +4,13 @@ namespace hw4UniqueList { + /// + /// Список + /// public class List { private int Size { get; set; } + private class Node { public int Value { get; set; } @@ -20,17 +24,30 @@ public Node(int value, Node node) } } + public List() + { + Size = 0; + head = null; + } + private Node head; private Node runner; + /// + /// Возвращает размер списка + /// public int GetSize() => Size; private Node GetNext(Node node) => node.Next; - private bool IsEmpty() + /// + /// Проверка на пустоту списка + /// + /// возвращает true, если список пуст + public bool IsEmpty() => head == null; private void CheckSize(int index) @@ -41,17 +58,11 @@ private void CheckSize(int index) } } - public List() - { - Size = 0; - head = null; - } - /// - /// + /// Функция вставки /// - /// - /// + /// позиция узла + /// значение в узле public virtual void Insert(int possition, int value) { CheckSize(possition); @@ -92,10 +103,10 @@ public virtual void Insert(int possition, int value) } /// - /// + /// Функция удаления по индексу /// - /// - /// + /// индекс узла + /// возвращает значение в узле public virtual int DeleteByIndex(int possition) { CheckSize(possition); @@ -132,9 +143,9 @@ public virtual int DeleteByIndex(int possition) } /// - /// + /// Функция удаление по значению /// - /// + /// значение, которое надо удалить public virtual void DeleteByValue(int value) { if (head.Value == value) @@ -154,13 +165,14 @@ public virtual void DeleteByValue(int value) } runner = GetNext(runner); } + throw new ValueDoesNotExistException(); } /// - /// + /// Смена значение в узле по индексу /// - /// - /// + /// индекс узла + /// новое значение public virtual void ChangeByIndex(int possition, int value) { CheckSize(possition); @@ -175,10 +187,10 @@ public virtual void ChangeByIndex(int possition, int value) } /// - /// + /// получить значение узла по его индексу /// - /// - /// + /// индекс узла + /// возвращает значение в узле public int GetValueByIndex(int possition) { CheckSize(possition); @@ -192,6 +204,11 @@ public int GetValueByIndex(int possition) return runner.Value; } + /// + /// проверка есть ли такое значение в списке + /// + /// значение, которое хотим проверить + /// возвращает true, если нашелся узел с таким значением public bool IsConsist(int value) { runner = head; diff --git a/hw4UniqueList/hw4UniqueList/Program.cs b/hw4UniqueList/hw4UniqueList/Program.cs index 0d4568b..d261647 100644 --- a/hw4UniqueList/hw4UniqueList/Program.cs +++ b/hw4UniqueList/hw4UniqueList/Program.cs @@ -18,30 +18,6 @@ static void Main(string[] args) list.DeleteByValue(2); list.DeleteByValue(7); list.DeleteByValue(1); - if (list.IsConsist(1)) - { - Console.WriteLine("TRUE"); - } - else - { - Console.WriteLine("FALSE"); - } - if (list.IsConsist(2)) - { - Console.WriteLine("TRUE"); - } - else - { - Console.WriteLine("FALSE"); - } - if (list.IsConsist(7)) - { - Console.WriteLine("TRUE"); - } - else - { - Console.WriteLine("FALSE"); - } } } } diff --git a/hw4UniqueList/hw4UniqueList/UniqueList.cs b/hw4UniqueList/hw4UniqueList/UniqueList.cs index c4da22a..011f2d4 100644 --- a/hw4UniqueList/hw4UniqueList/UniqueList.cs +++ b/hw4UniqueList/hw4UniqueList/UniqueList.cs @@ -4,6 +4,9 @@ namespace hw4UniqueList { + /// + /// Список без повторяющихся значений + /// public class UniqueList : List { public override void Insert(int possition, int value) From c6d1d2a99c9d72add2002fda480b02b3741203d5 Mon Sep 17 00:00:00 2001 From: MikePuzanov <89803250877@mail.ru> Date: Sun, 16 May 2021 16:14:12 +0300 Subject: [PATCH 5/6] fix mistakes --- hw4UniqueList/hw4UniqueList.Test/ListTest.cs | 8 +- .../hw4UniqueList.Test/UniqueListTest.cs | 6 +- hw4UniqueList/hw4UniqueList/List.cs | 104 ++++++++---------- hw4UniqueList/hw4UniqueList/UniqueList.cs | 16 +-- .../ValueDoesNotExistException.cs | 3 + .../ValueIsAlreadyInListException.cs | 3 + 6 files changed, 64 insertions(+), 76 deletions(-) diff --git a/hw4UniqueList/hw4UniqueList.Test/ListTest.cs b/hw4UniqueList/hw4UniqueList.Test/ListTest.cs index ef289a1..e9e2338 100644 --- a/hw4UniqueList/hw4UniqueList.Test/ListTest.cs +++ b/hw4UniqueList/hw4UniqueList.Test/ListTest.cs @@ -36,7 +36,9 @@ public void TestDeleteByIndex() list.DeleteByIndex(2); list.DeleteByIndex(7); list.DeleteByIndex(1); - Assert.IsFalse(list.IsConsist(2) || list.IsConsist(8) || list.IsConsist(1)); + Assert.IsFalse(list.Contains(2)); + Assert.IsFalse(list.Contains(8)); + Assert.IsFalse(list.Contains(1)); } [TestCase] @@ -57,7 +59,7 @@ public void TestDeleteByValue() list.DeleteByValue(2); list.DeleteByValue(8); list.DeleteByValue(1); - Assert.IsFalse(list.IsConsist(2) || list.IsConsist(8) || list.IsConsist(1)); + Assert.IsFalse(list.Contains(2) || list.Contains(8) || list.Contains(1)); } [TestCase] @@ -82,7 +84,7 @@ public void TestChangeByIndex() [TestCase] public void TestGetSize() { - Assert.IsTrue(list.GetSize() == 8); + Assert.AreEqual(list.GetSize(), 8); } [TestCase] diff --git a/hw4UniqueList/hw4UniqueList.Test/UniqueListTest.cs b/hw4UniqueList/hw4UniqueList.Test/UniqueListTest.cs index 6ee53b1..0e021cb 100644 --- a/hw4UniqueList/hw4UniqueList.Test/UniqueListTest.cs +++ b/hw4UniqueList/hw4UniqueList.Test/UniqueListTest.cs @@ -31,7 +31,7 @@ public void TestInsertValueAgain() public void TestInsert() { list.Insert(3, 12); - Assert.IsTrue(list.IsConsist(12)); + Assert.IsTrue(list.Contains(12)); } [TestCase] @@ -44,7 +44,7 @@ public void TestDeleteByValueException() public void TestDeleteByValue() { list.DeleteByValue(6); - Assert.IsFalse(list.IsConsist(6)); + Assert.IsFalse(list.Contains(6)); } [TestCase] @@ -57,7 +57,7 @@ public void TestDeleteByIndexException() public void TestDeleteByIndex() { list.DeleteByValue(3); - Assert.IsFalse(list.IsConsist(3)); + Assert.IsFalse(list.Contains(3)); } [TestCase] diff --git a/hw4UniqueList/hw4UniqueList/List.cs b/hw4UniqueList/hw4UniqueList/List.cs index 99641ae..c880e31 100644 --- a/hw4UniqueList/hw4UniqueList/List.cs +++ b/hw4UniqueList/hw4UniqueList/List.cs @@ -24,25 +24,14 @@ public Node(int value, Node node) } } - public List() - { - Size = 0; - head = null; - } - private Node head; - private Node runner; - /// /// Возвращает размер списка /// public int GetSize() => Size; - private Node GetNext(Node node) - => node.Next; - /// /// Проверка на пустоту списка /// @@ -61,23 +50,23 @@ private void CheckSize(int index) /// /// Функция вставки /// - /// позиция узла + /// позиция узла /// значение в узле - public virtual void Insert(int possition, int value) + public virtual void Insert(int position, int value) { - CheckSize(possition); - if (IsEmpty() && possition == 0) + CheckSize(position); + if (IsEmpty() && position == 0) { head = new Node(value, null); Size++; return; } - else if (IsEmpty() && possition != 0) + else if (IsEmpty() && position != 0) { throw new IndexOutOfRangeException(); } - runner = head; - if (possition == 0) + var runner = head; + if (position == 0) { var node = new Node(value, head); head = node; @@ -85,9 +74,9 @@ public virtual void Insert(int possition, int value) return; } int index = 1; - while (index < possition) + while (index < position) { - if (GetNext(runner) != null) + if (runner.Next != null) { runner = runner.Next; index++; @@ -97,24 +86,24 @@ public virtual void Insert(int possition, int value) throw new IndexOutOfRangeException(); } } - var NewNode = new Node(value, runner.Next); - runner.Next = NewNode; + var newNode = new Node(value, runner.Next); + runner.Next = newNode; Size++; } /// /// Функция удаления по индексу /// - /// индекс узла + /// индекс узла /// возвращает значение в узле - public virtual int DeleteByIndex(int possition) + public virtual int DeleteByIndex(int position) { - CheckSize(possition); + CheckSize(position); if (IsEmpty()) { throw new IndexOutOfRangeException(); } - if (possition == 1) + if (position == 1) { var node = head.Next; var result = head.Value; @@ -122,11 +111,11 @@ public virtual int DeleteByIndex(int possition) Size--; return result; } - runner = head; + var runner = head; int index = 1; - while (index < possition - 1) + while (index < position - 1) { - if (GetNext(runner) != null) + if (runner.Next != null) { runner = runner.Next; index++; @@ -154,71 +143,70 @@ public virtual void DeleteByValue(int value) Size--; return; } - runner = head; - while (GetNext(runner) != null) + var runner = head; + while (runner.Next != null) { - if (GetNext(runner).Value == value) + if (runner.Next.Value == value) { - runner.Next = GetNext(runner).Next; + runner.Next = runner.Next.Next; Size--; return; } - runner = GetNext(runner); + runner = runner.Next; } throw new ValueDoesNotExistException(); } - /// - /// Смена значение в узле по индексу - /// - /// индекс узла - /// новое значение - public virtual void ChangeByIndex(int possition, int value) + private Node GoToPosition(int position, Node node) { - CheckSize(possition); int index = 1; - runner = head; - while (index < possition && GetNext(runner) != null) + while (index < position) { - runner = GetNext(runner); + 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 possition) + public int GetValueByIndex(int position) { - CheckSize(possition); - int index = 1; - runner = head; - while (index < possition && GetNext(runner) != null) - { - runner = GetNext(runner); - index++; - } + CheckSize(position); + var runner = GoToPosition(position, head); return runner.Value; } /// - /// проверка есть ли такое значение в списке + /// проверка, есть ли такое значение в списке /// /// значение, которое хотим проверить /// возвращает true, если нашелся узел с таким значением - public bool IsConsist(int value) + public bool Contains(int value) { - runner = head; + var runner = head; while (runner != null) { if (runner.Value == value) { return true; } - runner = GetNext(runner); + runner = runner.Next; } return false; } diff --git a/hw4UniqueList/hw4UniqueList/UniqueList.cs b/hw4UniqueList/hw4UniqueList/UniqueList.cs index 011f2d4..934df92 100644 --- a/hw4UniqueList/hw4UniqueList/UniqueList.cs +++ b/hw4UniqueList/hw4UniqueList/UniqueList.cs @@ -11,7 +11,7 @@ public class UniqueList : List { public override void Insert(int possition, int value) { - if (IsConsist(value)) + if (Contains(value)) { throw new ValueIsAlreadyInListException("Такое значение уже есть в списке!"); } @@ -20,29 +20,21 @@ public override void Insert(int possition, int value) public override int DeleteByIndex(int possition) { - if (possition > GetSize() || possition < 1) - { - throw new IndexOutOfRangeException(); - } return base.DeleteByIndex(possition); } public override void DeleteByValue(int value) { - if (!IsConsist(value)) - { - throw new ValueDoesNotExistException("Такого значения в списке не существует!"); - } base.DeleteByValue(value); } public override void ChangeByIndex(int possition, int value) { - if (possition > GetSize() || possition < 1) + if (GetValueByIndex(possition) == value) { - throw new IndexOutOfRangeException(); + return; } - else if (IsConsist(value)) + if (Contains(value)) { throw new ValueIsAlreadyInListException("Такое значение уже есть в списке!"); } diff --git a/hw4UniqueList/hw4UniqueList/ValueDoesNotExistException.cs b/hw4UniqueList/hw4UniqueList/ValueDoesNotExistException.cs index 645d5d0..f591340 100644 --- a/hw4UniqueList/hw4UniqueList/ValueDoesNotExistException.cs +++ b/hw4UniqueList/hw4UniqueList/ValueDoesNotExistException.cs @@ -4,6 +4,9 @@ namespace hw4UniqueList { + /// + /// исключение для удаления элемента из списка, когда значение не содержиться в списке + /// public class ValueDoesNotExistException : Exception { public ValueDoesNotExistException() diff --git a/hw4UniqueList/hw4UniqueList/ValueIsAlreadyInListException.cs b/hw4UniqueList/hw4UniqueList/ValueIsAlreadyInListException.cs index f5cc9b1..c8c4a6d 100644 --- a/hw4UniqueList/hw4UniqueList/ValueIsAlreadyInListException.cs +++ b/hw4UniqueList/hw4UniqueList/ValueIsAlreadyInListException.cs @@ -4,6 +4,9 @@ namespace hw4UniqueList { + /// + /// исключение для unique list, когда значение уже содержиться в списке + /// public class ValueIsAlreadyInListException : Exception { public ValueIsAlreadyInListException() From e29315cf4bc89a31ce09d35f2d26e85b8482d6d7 Mon Sep 17 00:00:00 2001 From: MikePuzanov <89803250877@mail.ru> Date: Tue, 18 May 2021 03:14:29 +0300 Subject: [PATCH 6/6] fix style guide --- hw4UniqueList/hw4UniqueList.Test/ListTest.cs | 10 ++++---- .../hw4UniqueList.Test/UniqueListTest.cs | 2 +- hw4UniqueList/hw4UniqueList/List.cs | 24 +++++++++---------- hw4UniqueList/hw4UniqueList/Program.cs | 2 +- hw4UniqueList/hw4UniqueList/UniqueList.cs | 22 +++++------------ .../ValueDoesNotExistException.cs | 2 +- .../ValueIsAlreadyInListException.cs | 2 +- 7 files changed, 28 insertions(+), 36 deletions(-) diff --git a/hw4UniqueList/hw4UniqueList.Test/ListTest.cs b/hw4UniqueList/hw4UniqueList.Test/ListTest.cs index e9e2338..1eb6093 100644 --- a/hw4UniqueList/hw4UniqueList.Test/ListTest.cs +++ b/hw4UniqueList/hw4UniqueList.Test/ListTest.cs @@ -1,9 +1,9 @@ using NUnit.Framework; using System; -namespace hw4UniqueList.Test +namespace Hw4UniqueList.Test { - public class Tests + public class ListTests { private List list; @@ -59,7 +59,9 @@ public void TestDeleteByValue() list.DeleteByValue(2); list.DeleteByValue(8); list.DeleteByValue(1); - Assert.IsFalse(list.Contains(2) || list.Contains(8) || list.Contains(1)); + Assert.IsFalse(list.Contains(2)); + Assert.IsFalse(list.Contains(8)); + Assert.IsFalse(list.Contains(1)); } [TestCase] @@ -84,7 +86,7 @@ public void TestChangeByIndex() [TestCase] public void TestGetSize() { - Assert.AreEqual(list.GetSize(), 8); + Assert.AreEqual(8, list.GetSize()); } [TestCase] diff --git a/hw4UniqueList/hw4UniqueList.Test/UniqueListTest.cs b/hw4UniqueList/hw4UniqueList.Test/UniqueListTest.cs index 0e021cb..f2499b3 100644 --- a/hw4UniqueList/hw4UniqueList.Test/UniqueListTest.cs +++ b/hw4UniqueList/hw4UniqueList.Test/UniqueListTest.cs @@ -1,7 +1,7 @@ using NUnit.Framework; using System; -namespace hw4UniqueList.Test +namespace Hw4UniqueList.Test { class UniqueListTest { diff --git a/hw4UniqueList/hw4UniqueList/List.cs b/hw4UniqueList/hw4UniqueList/List.cs index c880e31..9f970d1 100644 --- a/hw4UniqueList/hw4UniqueList/List.cs +++ b/hw4UniqueList/hw4UniqueList/List.cs @@ -2,15 +2,13 @@ using System.Collections.Generic; using System.Text; -namespace hw4UniqueList +namespace Hw4UniqueList { /// /// Список /// public class List { - private int Size { get; set; } - private class Node { public int Value { get; set; } @@ -24,13 +22,15 @@ public Node(int value, Node node) } } + private int size; + private Node head; /// /// Возвращает размер списка /// public int GetSize() - => Size; + => size; /// /// Проверка на пустоту списка @@ -41,7 +41,7 @@ public bool IsEmpty() private void CheckSize(int index) { - if (index > Size || index < 0) + if (index > size || index < 0) { throw new IndexOutOfRangeException(); } @@ -58,7 +58,7 @@ public virtual void Insert(int position, int value) if (IsEmpty() && position == 0) { head = new Node(value, null); - Size++; + size++; return; } else if (IsEmpty() && position != 0) @@ -70,7 +70,7 @@ public virtual void Insert(int position, int value) { var node = new Node(value, head); head = node; - Size++; + size++; return; } int index = 1; @@ -88,7 +88,7 @@ public virtual void Insert(int position, int value) } var newNode = new Node(value, runner.Next); runner.Next = newNode; - Size++; + size++; } /// @@ -108,7 +108,7 @@ public virtual int DeleteByIndex(int position) var node = head.Next; var result = head.Value; head = node; - Size--; + size--; return result; } var runner = head; @@ -127,7 +127,7 @@ public virtual int DeleteByIndex(int position) } var returnResult = runner.Next.Value; runner.Next = runner.Next.Next; - Size--; + size--; return returnResult; } @@ -140,7 +140,7 @@ public virtual void DeleteByValue(int value) if (head.Value == value) { head = head.Next; - Size--; + size--; return; } var runner = head; @@ -149,7 +149,7 @@ public virtual void DeleteByValue(int value) if (runner.Next.Value == value) { runner.Next = runner.Next.Next; - Size--; + size--; return; } runner = runner.Next; diff --git a/hw4UniqueList/hw4UniqueList/Program.cs b/hw4UniqueList/hw4UniqueList/Program.cs index d261647..b71895f 100644 --- a/hw4UniqueList/hw4UniqueList/Program.cs +++ b/hw4UniqueList/hw4UniqueList/Program.cs @@ -1,6 +1,6 @@ using System; -namespace hw4UniqueList +namespace Hw4UniqueList { class Program { diff --git a/hw4UniqueList/hw4UniqueList/UniqueList.cs b/hw4UniqueList/hw4UniqueList/UniqueList.cs index 934df92..bcb7b5d 100644 --- a/hw4UniqueList/hw4UniqueList/UniqueList.cs +++ b/hw4UniqueList/hw4UniqueList/UniqueList.cs @@ -2,35 +2,25 @@ using System.Collections.Generic; using System.Text; -namespace hw4UniqueList +namespace Hw4UniqueList { /// /// Список без повторяющихся значений /// public class UniqueList : List { - public override void Insert(int possition, int value) + public override void Insert(int position, int value) { if (Contains(value)) { throw new ValueIsAlreadyInListException("Такое значение уже есть в списке!"); } - base.Insert(possition, value); + base.Insert(position, value); } - public override int DeleteByIndex(int possition) + public override void ChangeByIndex(int position, int value) { - return base.DeleteByIndex(possition); - } - - public override void DeleteByValue(int value) - { - base.DeleteByValue(value); - } - - public override void ChangeByIndex(int possition, int value) - { - if (GetValueByIndex(possition) == value) + if (GetValueByIndex(position) == value) { return; } @@ -38,7 +28,7 @@ public override void ChangeByIndex(int possition, int value) { throw new ValueIsAlreadyInListException("Такое значение уже есть в списке!"); } - base.ChangeByIndex(possition, value); + base.ChangeByIndex(position, value); } } } diff --git a/hw4UniqueList/hw4UniqueList/ValueDoesNotExistException.cs b/hw4UniqueList/hw4UniqueList/ValueDoesNotExistException.cs index f591340..5d8a860 100644 --- a/hw4UniqueList/hw4UniqueList/ValueDoesNotExistException.cs +++ b/hw4UniqueList/hw4UniqueList/ValueDoesNotExistException.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Text; -namespace hw4UniqueList +namespace Hw4UniqueList { /// /// исключение для удаления элемента из списка, когда значение не содержиться в списке diff --git a/hw4UniqueList/hw4UniqueList/ValueIsAlreadyInListException.cs b/hw4UniqueList/hw4UniqueList/ValueIsAlreadyInListException.cs index c8c4a6d..4940ed7 100644 --- a/hw4UniqueList/hw4UniqueList/ValueIsAlreadyInListException.cs +++ b/hw4UniqueList/hw4UniqueList/ValueIsAlreadyInListException.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Text; -namespace hw4UniqueList +namespace Hw4UniqueList { /// /// исключение для unique list, когда значение уже содержиться в списке