From 1276141f2be01c9951163d94d5a74d59e475c772 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sun, 27 Mar 2022 16:42:09 +0300 Subject: [PATCH 01/10] Added functions for the list, added tests --- List/List/List/List.sln | 31 +++++ List/List/List/List/IUniqueList.cs | 57 ++++++++ List/List/List/List/List.cs | 164 ++++++++++++++++++++++++ List/List/List/List/List.csproj | 10 ++ List/List/List/List/ListExceptions.cs | 17 +++ List/List/List/List/UniqueList.cs | 50 ++++++++ List/List/List/ListTest/ListTest.cs | 87 +++++++++++++ List/List/List/ListTest/ListTest.csproj | 21 +++ 8 files changed, 437 insertions(+) create mode 100644 List/List/List/List.sln create mode 100644 List/List/List/List/IUniqueList.cs create mode 100644 List/List/List/List/List.cs create mode 100644 List/List/List/List/List.csproj create mode 100644 List/List/List/List/ListExceptions.cs create mode 100644 List/List/List/List/UniqueList.cs create mode 100644 List/List/List/ListTest/ListTest.cs create mode 100644 List/List/List/ListTest/ListTest.csproj diff --git a/List/List/List/List.sln b/List/List/List/List.sln new file mode 100644 index 0000000..ba5b5d4 --- /dev/null +++ b/List/List/List/List.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.1.32228.430 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "List", "List\List.csproj", "{E452EFCC-D3CF-4F34-86B6-FFF9119434ED}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ListTest", "ListTest\ListTest.csproj", "{D91A340C-44C7-462D-8304-FF4C9E6170FE}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E452EFCC-D3CF-4F34-86B6-FFF9119434ED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E452EFCC-D3CF-4F34-86B6-FFF9119434ED}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E452EFCC-D3CF-4F34-86B6-FFF9119434ED}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E452EFCC-D3CF-4F34-86B6-FFF9119434ED}.Release|Any CPU.Build.0 = Release|Any CPU + {D91A340C-44C7-462D-8304-FF4C9E6170FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D91A340C-44C7-462D-8304-FF4C9E6170FE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D91A340C-44C7-462D-8304-FF4C9E6170FE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D91A340C-44C7-462D-8304-FF4C9E6170FE}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {8A75F5F0-0627-4DE9-BE2B-FBB251E5CA4F} + EndGlobalSection +EndGlobal diff --git a/List/List/List/List/IUniqueList.cs b/List/List/List/List/IUniqueList.cs new file mode 100644 index 0000000..427f584 --- /dev/null +++ b/List/List/List/List/IUniqueList.cs @@ -0,0 +1,57 @@ +namespace List; + +/// +/// Class interface for a unique list +/// +/// Еype of items in the list +public interface IUniqueList +{ + /// + /// Function for changing the value of an element by index + /// + /// Item position in the list + /// New value + public bool ChangeElement(int index, T value); + + /// + /// Function for printing a list + /// + public void PrintList(); + + /// + /// Аunction to search for an item in the list + /// + /// Search value + /// Is there an item in the list + public bool Contains(T value); + + /// + /// Function for deleting a list + /// + public void DeleteList(); + + /// + /// Function for adding an item to a list + /// + /// Type of item value in the list + public void Add(T value); + + /// + /// Function to remove an item from the unique list + /// + /// Item position in the list + public bool Remove(int index); + + /// + /// Function for return number of element on list + /// + /// Number of element on list + public int ReturnSize(); +} + +public class Solution +{ + public static void Main(string[] args) + { + } +} \ No newline at end of file diff --git a/List/List/List/List/List.cs b/List/List/List/List/List.cs new file mode 100644 index 0000000..9c85b40 --- /dev/null +++ b/List/List/List/List/List.cs @@ -0,0 +1,164 @@ +using System; +using System.Collections.Generic; + +namespace List; + +/// +/// Сlass representing a list +/// +/// type of item values in the list +abstract public class SinglyLinkedList : IUniqueList +{ + /// + /// Class for storing list items + /// + private class ListElement + { + private ListElement? next { get; set; } + private T? value; + public T? Value { get => value; set { this.value = value; } } + public ListElement? Next { get => next; set { next = value; } } + } + + private ListElement? head; + private ListElement? tail; + private int size; + + /// + /// Function for adding an item to a list + /// + /// T ype of item value in the list + public virtual void Add(T value) + { + if (value == null) + { + return; + } + size++; + if (head == null || tail == null) + { + head = new ListElement(); + tail = head; + head.Value = value; + return; + } + ListElement newHead = new ListElement(); + newHead.Value = value; + tail.Next = newHead; + tail = newHead; + } + + /// + /// Function to remove an item from the list + /// + /// Item position in the list + /// was the item in the list + public virtual bool Remove(int index) + { + if (index > size || index <= 0) + { + return false; + } + size--; + if (index == 1) + { + head = head?.Next; + return true; + } + ListElement? element = new ListElement(); + ListElement? copyElement = new ListElement(); + element = head; + for ( int i = 1; i < index; i++) + { + if (i == index - 1) + { + copyElement = element; + } + element = element?.Next; + } + if (element != null && copyElement != null) + { + copyElement.Next = element.Next; + element.Next = null; + } + return true; + } + + /// + /// Function for changing the value of an element by index + /// + /// Item position in the list + /// New value + public bool ChangeElement(int index, T value) + { + if (index > size || index <= 0) + { + return false; + } + ListElement? element = new ListElement(); + element = head; + for (int i = 1; i < index; i++) + { + element = element?.Next; + } + if (element != null) + { + element.Value = value; + } + return true; + } + + /// + /// Function for printing a list + /// + public void PrintList() + { + ListElement? element = new ListElement(); + element = head; + while (element != null) + { + Console.Write($"{element.Value} "); + element = element.Next; + } + Console.WriteLine(); + } + + /// + /// Аunction to search for an item in the list + /// + /// Search value + /// Is there an item in the list + public bool Contains(T value) + { + ListElement? element = new ListElement(); + element = head; + while (element != null) + { + if (value != null && value.Equals(element.Value)) + { + return true; + } + element = element.Next; + } + return false; + } + + /// + /// Function for deleting a list + /// + public void DeleteList() + { + while (head != null) + { + ListElement? element = head; + head = head.Next; + element = null; + } + } + + /// + /// Function for return number of element on list + /// + /// Number of element on list + public int ReturnSize() => size; +} \ No newline at end of file diff --git a/List/List/List/List/List.csproj b/List/List/List/List/List.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/List/List/List/List/List.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/List/List/List/List/ListExceptions.cs b/List/List/List/List/ListExceptions.cs new file mode 100644 index 0000000..9e7c32c --- /dev/null +++ b/List/List/List/List/ListExceptions.cs @@ -0,0 +1,17 @@ +namespace List; + +/// +/// A class for creating custom exceptions +/// +public class RepeatValueException : Exception +{ + public RepeatValueException(string? message) : base(message) { } +} + +/// +/// A class for creating custom exceptions +/// +public class RemoveNonExistenElementException : Exception +{ + public RemoveNonExistenElementException(string? message) : base(message) { } +} \ No newline at end of file diff --git a/List/List/List/List/UniqueList.cs b/List/List/List/List/UniqueList.cs new file mode 100644 index 0000000..1d7224f --- /dev/null +++ b/List/List/List/List/UniqueList.cs @@ -0,0 +1,50 @@ +namespace List; + +/// +/// Class representing a list of unique values +/// +/// +public class UniqueList : SinglyLinkedList +{ + /// + /// Function for adding an item to a list + /// + /// Type of item value in the list + public override void Add(T value) + { + try + { + if (Contains(value)) + { + throw new RepeatValueException("this value already exist in list"); + } + } + catch (RepeatValueException exception) + { + Console.WriteLine($"Ошибка: {exception.Message}"); + throw; + } + base.Add(value); + } + + /// + /// Function to remove an item from the unique list + /// + /// Item position in the list + public override bool Remove(int index) + { + try + { + if (!base.Remove(index)) + { + throw new RemoveNonExistenElementException("this item does not exist in the list"); + } + } + catch (RemoveNonExistenElementException exception) + { + Console.WriteLine($"Ошибка: {exception.Message}"); + throw; + } + return true; + } +} diff --git a/List/List/List/ListTest/ListTest.cs b/List/List/List/ListTest/ListTest.cs new file mode 100644 index 0000000..318784b --- /dev/null +++ b/List/List/List/ListTest/ListTest.cs @@ -0,0 +1,87 @@ +using NUnit.Framework; +using List; + +namespace ListTest; + +public class Tests +{ + IUniqueList? list; + + [SetUp] + public void Setup() + { + list = new UniqueList(); + } + + [Test] + public void NumberOfElementsInEmptyListEqual0() + { + Assert.AreEqual(0, list?.ReturnSize()); + } + + [Test] + public void ListShouldNotBeEmptyAfterPush() + { + list?.Add(1); + Assert.AreEqual(1, list?.ReturnSize()); + } + + [Test] + public void TryRemoveItemFromEmptyList() + { + Assert.Throws(() => list?.Remove(0)); + } + + [Test] + public void RemoveNonExistenElementException() + { + list?.Add(1); + list?.Add(2); + list?.Add(0); + list?.Add(-12); + Assert.Throws(() => list?.Remove(5)); + } + + [Test] + public void AddRepeatElement() + { + list?.Add(1); + Assert.Throws(() => list?.Add(1)); + } + + [Test] + public void AddedElementMustContain() + { + list?.Add(12); + Assert.AreEqual(true, list?.Contains(12)); + } + + [Test] + public void NonExistenElementNotMustContain() + { + Assert.AreEqual(false, list?.Contains(10)); + } + + [Test] + public void RemoveElementNotMustContain() + { + list?.Add(2); + list?.Remove(1); + Assert.AreEqual(false, list?.Contains(2)); + } + + [Test] + public void ChangeNonExistenElement() + { + Assert.AreEqual(false, list?.ChangeElement(2, 12)); + } + + [Test] + public void ContainsChangeElement() + { + list?.Add(125); + list?.ChangeElement(1, 12); + Assert.AreEqual(true, list?.Contains(12)); + Assert.AreEqual(false, list?.Contains(125)); + } +} \ No newline at end of file diff --git a/List/List/List/ListTest/ListTest.csproj b/List/List/List/ListTest/ListTest.csproj new file mode 100644 index 0000000..4cc12ed --- /dev/null +++ b/List/List/List/ListTest/ListTest.csproj @@ -0,0 +1,21 @@ + + + + net6.0 + enable + + false + + + + + + + + + + + + + + From 9ddbf6412f9023137a0f640bcb5e9cf3c8d36c67 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Tue, 29 Mar 2022 02:26:16 +0300 Subject: [PATCH 02/10] Fixed a bug : indexing of list items started with 1 --- List/List/List/List/List.cs | 10 +++++----- List/List/List/ListTest/ListTest.cs | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/List/List/List/List/List.cs b/List/List/List/List/List.cs index 9c85b40..16a2ab4 100644 --- a/List/List/List/List/List.cs +++ b/List/List/List/List/List.cs @@ -55,12 +55,12 @@ public virtual void Add(T value) /// was the item in the list public virtual bool Remove(int index) { - if (index > size || index <= 0) + if (index >= size || index < 0) { return false; } size--; - if (index == 1) + if (index == 0) { head = head?.Next; return true; @@ -68,7 +68,7 @@ public virtual bool Remove(int index) ListElement? element = new ListElement(); ListElement? copyElement = new ListElement(); element = head; - for ( int i = 1; i < index; i++) + for (int i = 0; i < index - 1; i++) { if (i == index - 1) { @@ -91,13 +91,13 @@ public virtual bool Remove(int index) /// New value public bool ChangeElement(int index, T value) { - if (index > size || index <= 0) + if (index >= size || index < 0) { return false; } ListElement? element = new ListElement(); element = head; - for (int i = 1; i < index; i++) + for (int i = 0; i < index - 1; i++) { element = element?.Next; } diff --git a/List/List/List/ListTest/ListTest.cs b/List/List/List/ListTest/ListTest.cs index 318784b..6ededca 100644 --- a/List/List/List/ListTest/ListTest.cs +++ b/List/List/List/ListTest/ListTest.cs @@ -39,7 +39,7 @@ public void RemoveNonExistenElementException() list?.Add(2); list?.Add(0); list?.Add(-12); - Assert.Throws(() => list?.Remove(5)); + Assert.Throws(() => list?.Remove(4)); } [Test] @@ -66,7 +66,7 @@ public void NonExistenElementNotMustContain() public void RemoveElementNotMustContain() { list?.Add(2); - list?.Remove(1); + list?.Remove(0); Assert.AreEqual(false, list?.Contains(2)); } @@ -80,7 +80,7 @@ public void ChangeNonExistenElement() public void ContainsChangeElement() { list?.Add(125); - list?.ChangeElement(1, 12); + list?.ChangeElement(0, 12); Assert.AreEqual(true, list?.Contains(12)); Assert.AreEqual(false, list?.Contains(125)); } From ab28fb7c3d73f3494caadef48f377fec2c9a1d13 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Thu, 21 Apr 2022 02:07:17 +0300 Subject: [PATCH 03/10] Added a function to delete an element by value and changed the tests --- List/List/List/List/IUniqueList.cs | 18 ++--- List/List/List/List/List.cs | 70 ++++++++++-------- List/List/List/List/List.csproj | 2 +- List/List/List/List/ListExceptions.cs | 17 ----- .../List/RemoveNonExistingElementException.cs | 9 +++ List/List/List/List/RepeatValueException.cs | 9 +++ List/List/List/List/UniqueList.cs | 40 +++++------ List/List/List/ListTest/ListTest.cs | 71 ++++++++++--------- 8 files changed, 123 insertions(+), 113 deletions(-) delete mode 100644 List/List/List/List/ListExceptions.cs create mode 100644 List/List/List/List/RemoveNonExistingElementException.cs create mode 100644 List/List/List/List/RepeatValueException.cs diff --git a/List/List/List/List/IUniqueList.cs b/List/List/List/List/IUniqueList.cs index 427f584..b7eba0a 100644 --- a/List/List/List/List/IUniqueList.cs +++ b/List/List/List/List/IUniqueList.cs @@ -28,7 +28,7 @@ public interface IUniqueList /// /// Function for deleting a list /// - public void DeleteList(); + public void ClearList(); /// /// Function for adding an item to a list @@ -42,16 +42,16 @@ public interface IUniqueList /// Item position in the list public bool Remove(int index); + /// + /// Function to remove an item from the list + /// + /// Value to be deleted + /// was the value in the list + public bool RemoveAt(T value); + /// /// Function for return number of element on list /// /// Number of element on list - public int ReturnSize(); -} - -public class Solution -{ - public static void Main(string[] args) - { - } + public int Size(); } \ No newline at end of file diff --git a/List/List/List/List/List.cs b/List/List/List/List/List.cs index 16a2ab4..9240a32 100644 --- a/List/List/List/List/List.cs +++ b/List/List/List/List/List.cs @@ -1,8 +1,7 @@ -using System; +namespace List; +using System; using System.Collections.Generic; -namespace List; - /// /// Сlass representing a list /// @@ -14,10 +13,8 @@ abstract public class SinglyLinkedList : IUniqueList /// private class ListElement { - private ListElement? next { get; set; } - private T? value; - public T? Value { get => value; set { this.value = value; } } - public ListElement? Next { get => next; set { next = value; } } + public T? Value { get ; set; } + public ListElement? Next { get; set; } } private ListElement? head; @@ -42,10 +39,10 @@ public virtual void Add(T value) head.Value = value; return; } - ListElement newHead = new ListElement(); - newHead.Value = value; - tail.Next = newHead; - tail = newHead; + var newTail = new ListElement(){}; + newTail.Value = value; + tail.Next = newTail; + tail = newTail; } /// @@ -65,9 +62,8 @@ public virtual bool Remove(int index) head = head?.Next; return true; } - ListElement? element = new ListElement(); - ListElement? copyElement = new ListElement(); - element = head; + var element = head; + var copyElement = new ListElement(); for (int i = 0; i < index - 1; i++) { if (i == index - 1) @@ -84,6 +80,29 @@ public virtual bool Remove(int index) return true; } + /// + /// Function to remove an item from the list + /// + /// Value to be deleted + /// was the value in the list + public virtual bool RemoveAt(T value) + { + var copyHead = head; + for (int i = 0; i < size; i++) + { + while (copyHead!= null) + { + if (copyHead != null && value != null && value.Equals(copyHead.Value)) + { + Remove(i); + return true; + } + copyHead = copyHead?.Next; + } + } + return false; + } + /// /// Function for changing the value of an element by index /// @@ -95,8 +114,7 @@ public bool ChangeElement(int index, T value) { return false; } - ListElement? element = new ListElement(); - element = head; + var element = head; for (int i = 0; i < index - 1; i++) { element = element?.Next; @@ -113,8 +131,7 @@ public bool ChangeElement(int index, T value) /// public void PrintList() { - ListElement? element = new ListElement(); - element = head; + var element = head; while (element != null) { Console.Write($"{element.Value} "); @@ -130,8 +147,7 @@ public void PrintList() /// Is there an item in the list public bool Contains(T value) { - ListElement? element = new ListElement(); - element = head; + var element = head; while (element != null) { if (value != null && value.Equals(element.Value)) @@ -144,21 +160,13 @@ public bool Contains(T value) } /// - /// Function for deleting a list + /// Function for clear list /// - public void DeleteList() - { - while (head != null) - { - ListElement? element = head; - head = head.Next; - element = null; - } - } + public void ClearList() => head = null; /// /// Function for return number of element on list /// /// Number of element on list - public int ReturnSize() => size; + public int Size() => size; } \ No newline at end of file diff --git a/List/List/List/List/List.csproj b/List/List/List/List/List.csproj index 74abf5c..16e62dd 100644 --- a/List/List/List/List/List.csproj +++ b/List/List/List/List/List.csproj @@ -1,7 +1,7 @@ - Exe + Library net6.0 enable enable diff --git a/List/List/List/List/ListExceptions.cs b/List/List/List/List/ListExceptions.cs deleted file mode 100644 index 9e7c32c..0000000 --- a/List/List/List/List/ListExceptions.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace List; - -/// -/// A class for creating custom exceptions -/// -public class RepeatValueException : Exception -{ - public RepeatValueException(string? message) : base(message) { } -} - -/// -/// A class for creating custom exceptions -/// -public class RemoveNonExistenElementException : Exception -{ - public RemoveNonExistenElementException(string? message) : base(message) { } -} \ No newline at end of file diff --git a/List/List/List/List/RemoveNonExistingElementException.cs b/List/List/List/List/RemoveNonExistingElementException.cs new file mode 100644 index 0000000..b1fc145 --- /dev/null +++ b/List/List/List/List/RemoveNonExistingElementException.cs @@ -0,0 +1,9 @@ +namespace List; + +/// +/// A class for creating custom exceptions +/// +public class RemoveNonExistingElementException : Exception +{ + public RemoveNonExistingElementException() : base() { } +} \ No newline at end of file diff --git a/List/List/List/List/RepeatValueException.cs b/List/List/List/List/RepeatValueException.cs new file mode 100644 index 0000000..a328802 --- /dev/null +++ b/List/List/List/List/RepeatValueException.cs @@ -0,0 +1,9 @@ +namespace List; + +/// +/// A class for creating custom exceptions +/// +public class RepeatValueException : Exception +{ + public RepeatValueException() : base() { } +} diff --git a/List/List/List/List/UniqueList.cs b/List/List/List/List/UniqueList.cs index 1d7224f..325b4cc 100644 --- a/List/List/List/List/UniqueList.cs +++ b/List/List/List/List/UniqueList.cs @@ -3,7 +3,6 @@ /// /// Class representing a list of unique values /// -/// public class UniqueList : SinglyLinkedList { /// @@ -12,18 +11,11 @@ public class UniqueList : SinglyLinkedList /// Type of item value in the list public override void Add(T value) { - try + if (Contains(value)) { - if (Contains(value)) - { - throw new RepeatValueException("this value already exist in list"); - } - } - catch (RepeatValueException exception) - { - Console.WriteLine($"Ошибка: {exception.Message}"); - throw; + throw new RepeatValueException(); } + base.Add(value); } @@ -33,18 +25,26 @@ public override void Add(T value) /// Item position in the list public override bool Remove(int index) { - try + if (!base.Remove(index)) { - if (!base.Remove(index)) - { - throw new RemoveNonExistenElementException("this item does not exist in the list"); - } + throw new RemoveNonExistingElementException(); } - catch (RemoveNonExistenElementException exception) + + return true; + } + + /// + /// Function to remove an item from the list + /// + /// Value to be deleted + /// was the value in the list + public override bool RemoveAt(T value) + { + if (!base.RemoveAt(value)) { - Console.WriteLine($"Ошибка: {exception.Message}"); - throw; + throw new RemoveNonExistingElementException(); } - return true; + + return false; } } diff --git a/List/List/List/ListTest/ListTest.cs b/List/List/List/ListTest/ListTest.cs index 6ededca..9f7afa0 100644 --- a/List/List/List/ListTest/ListTest.cs +++ b/List/List/List/ListTest/ListTest.cs @@ -1,11 +1,11 @@ +namespace ListTest; + using NUnit.Framework; using List; -namespace ListTest; - public class Tests { - IUniqueList? list; + IUniqueList list = new UniqueList(); [SetUp] public void Setup() @@ -14,74 +14,75 @@ public void Setup() } [Test] - public void NumberOfElementsInEmptyListEqual0() + public void ShouldExpectedZeroWhenListSizeEqualZero() { - Assert.AreEqual(0, list?.ReturnSize()); + Assert.AreEqual(0, list.Size()); } [Test] - public void ListShouldNotBeEmptyAfterPush() + public void ShouldExpectedOneWhenSizeAfterAddToEmptyList() { - list?.Add(1); - Assert.AreEqual(1, list?.ReturnSize()); + list.Add(1); + Assert.AreEqual(1, list.Size()); } [Test] - public void TryRemoveItemFromEmptyList() + public void ShouldThrowsRemoveNonExistingElementExceptionWhenRemoveFromEmptyList() { - Assert.Throws(() => list?.Remove(0)); + Assert.Throws(() => list.Remove(0)); } [Test] - public void RemoveNonExistenElementException() + public void ShouldThrowsRemoveNonExistingElementExceptionWhenRemoveNonExistenElement() { - list?.Add(1); - list?.Add(2); - list?.Add(0); - list?.Add(-12); - Assert.Throws(() => list?.Remove(4)); + list.Add(1); + list.Add(2); + list.Add(0); + list.Add(-12); + Assert.Throws(() => list.Remove(4)); } [Test] - public void AddRepeatElement() + public void ShouldThrowsRepeatValueExceptionWhenAddExistingElement() { - list?.Add(1); - Assert.Throws(() => list?.Add(1)); + list.Add(1); + Assert.Throws(() => list.Add(1)); } [Test] - public void AddedElementMustContain() + public void ShouldExpectedTrueWhenContainsForExistingElement() { - list?.Add(12); - Assert.AreEqual(true, list?.Contains(12)); + list.Add(12); + Assert.AreEqual(true, list.Contains(12)); } [Test] - public void NonExistenElementNotMustContain() + public void ShouldExpectedFalseWhenContainsForExistingElement() { - Assert.AreEqual(false, list?.Contains(10)); + list.Add(2); + Assert.AreEqual(false, list.Contains(10)); } [Test] - public void RemoveElementNotMustContain() + public void ShouldExpectedFalseWhenContainsForRemovedElement() { - list?.Add(2); - list?.Remove(0); - Assert.AreEqual(false, list?.Contains(2)); + list.Add(2); + list.RemoveAt(2); + Assert.AreEqual(false, list.Contains(2)); } [Test] - public void ChangeNonExistenElement() + public void ShouldExpectedFalseWhenChangeNonExistenElement() { - Assert.AreEqual(false, list?.ChangeElement(2, 12)); + Assert.AreEqual(false, list.ChangeElement(2, 12)); } [Test] - public void ContainsChangeElement() + public void ShouldExpectedFalseWhenContainsChangeReplacedElement() { - list?.Add(125); - list?.ChangeElement(0, 12); - Assert.AreEqual(true, list?.Contains(12)); - Assert.AreEqual(false, list?.Contains(125)); + list.Add(125); + list.ChangeElement(0, 12); + Assert.AreEqual(true, list.Contains(12)); + Assert.AreEqual(false, list.Contains(125)); } } \ No newline at end of file From 76814e663b9311e88177b1796f89968b7612dbfe Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Thu, 21 Apr 2022 02:37:58 +0300 Subject: [PATCH 04/10] Changing the code by style guide --- List/List/List/List/List.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/List/List/List/List/List.cs b/List/List/List/List/List.cs index 9240a32..d8e5375 100644 --- a/List/List/List/List/List.cs +++ b/List/List/List/List/List.cs @@ -1,4 +1,5 @@ namespace List; + using System; using System.Collections.Generic; @@ -31,6 +32,7 @@ public virtual void Add(T value) { return; } + size++; if (head == null || tail == null) { @@ -39,6 +41,7 @@ public virtual void Add(T value) head.Value = value; return; } + var newTail = new ListElement(){}; newTail.Value = value; tail.Next = newTail; @@ -56,12 +59,14 @@ public virtual bool Remove(int index) { return false; } + size--; if (index == 0) { head = head?.Next; return true; } + var element = head; var copyElement = new ListElement(); for (int i = 0; i < index - 1; i++) @@ -70,13 +75,16 @@ public virtual bool Remove(int index) { copyElement = element; } + element = element?.Next; } + if (element != null && copyElement != null) { copyElement.Next = element.Next; element.Next = null; } + return true; } @@ -97,9 +105,11 @@ public virtual bool RemoveAt(T value) Remove(i); return true; } + copyHead = copyHead?.Next; } } + return false; } @@ -114,15 +124,18 @@ public bool ChangeElement(int index, T value) { return false; } + var element = head; for (int i = 0; i < index - 1; i++) { element = element?.Next; } + if (element != null) { element.Value = value; } + return true; } @@ -137,6 +150,7 @@ public void PrintList() Console.Write($"{element.Value} "); element = element.Next; } + Console.WriteLine(); } @@ -154,8 +168,10 @@ public bool Contains(T value) { return true; } + element = element.Next; } + return false; } From b441c5f489aab6d8c3a891b78c7df8b214c7568f Mon Sep 17 00:00:00 2001 From: Roman Date: Mon, 16 May 2022 01:44:51 +0300 Subject: [PATCH 05/10] Added the Size property --- List/List/List/List/IUniqueList.cs | 8 ++++---- List/List/List/List/List.cs | 23 +++++++++-------------- List/List/List/List/UniqueList.cs | 8 ++++---- List/List/List/ListTest/ListTest.cs | 8 ++++---- 4 files changed, 21 insertions(+), 26 deletions(-) diff --git a/List/List/List/List/IUniqueList.cs b/List/List/List/List/IUniqueList.cs index b7eba0a..5a9d876 100644 --- a/List/List/List/List/IUniqueList.cs +++ b/List/List/List/List/IUniqueList.cs @@ -40,18 +40,18 @@ public interface IUniqueList /// Function to remove an item from the unique list /// /// Item position in the list - public bool Remove(int index); + public bool RemoveAt(int index); /// /// Function to remove an item from the list /// /// Value to be deleted /// was the value in the list - public bool RemoveAt(T value); + public bool Remove(T value); /// - /// Function for return number of element on list + /// Size property /// /// Number of element on list - public int Size(); + public int Size { get;} } \ No newline at end of file diff --git a/List/List/List/List/List.cs b/List/List/List/List/List.cs index d8e5375..a19b10a 100644 --- a/List/List/List/List/List.cs +++ b/List/List/List/List/List.cs @@ -20,7 +20,7 @@ private class ListElement private ListElement? head; private ListElement? tail; - private int size; + public int Size { get; private set; } /// /// Function for adding an item to a list @@ -33,7 +33,7 @@ public virtual void Add(T value) return; } - size++; + Size++; if (head == null || tail == null) { head = new ListElement(); @@ -53,14 +53,14 @@ public virtual void Add(T value) /// /// Item position in the list /// was the item in the list - public virtual bool Remove(int index) + public virtual bool RemoveAt(int index) { - if (index >= size || index < 0) + if (index >= Size || index < 0) { return false; } - size--; + Size--; if (index == 0) { head = head?.Next; @@ -93,16 +93,16 @@ public virtual bool Remove(int index) /// /// Value to be deleted /// was the value in the list - public virtual bool RemoveAt(T value) + public virtual bool Remove(T value) { var copyHead = head; - for (int i = 0; i < size; i++) + for (int i = 0; i < Size; i++) { while (copyHead!= null) { if (copyHead != null && value != null && value.Equals(copyHead.Value)) { - Remove(i); + RemoveAt(i); return true; } @@ -120,7 +120,7 @@ public virtual bool RemoveAt(T value) /// New value public bool ChangeElement(int index, T value) { - if (index >= size || index < 0) + if (index >= Size || index < 0) { return false; } @@ -180,9 +180,4 @@ public bool Contains(T value) /// public void ClearList() => head = null; - /// - /// Function for return number of element on list - /// - /// Number of element on list - public int Size() => size; } \ No newline at end of file diff --git a/List/List/List/List/UniqueList.cs b/List/List/List/List/UniqueList.cs index 325b4cc..cde0376 100644 --- a/List/List/List/List/UniqueList.cs +++ b/List/List/List/List/UniqueList.cs @@ -23,9 +23,9 @@ public override void Add(T value) /// Function to remove an item from the unique list /// /// Item position in the list - public override bool Remove(int index) + public override bool RemoveAt(int index) { - if (!base.Remove(index)) + if (!base.RemoveAt(index)) { throw new RemoveNonExistingElementException(); } @@ -38,9 +38,9 @@ public override bool Remove(int index) /// /// Value to be deleted /// was the value in the list - public override bool RemoveAt(T value) + public override bool Remove(T value) { - if (!base.RemoveAt(value)) + if (!base.Remove(value)) { throw new RemoveNonExistingElementException(); } diff --git a/List/List/List/ListTest/ListTest.cs b/List/List/List/ListTest/ListTest.cs index 9f7afa0..47fc47e 100644 --- a/List/List/List/ListTest/ListTest.cs +++ b/List/List/List/ListTest/ListTest.cs @@ -16,14 +16,14 @@ public void Setup() [Test] public void ShouldExpectedZeroWhenListSizeEqualZero() { - Assert.AreEqual(0, list.Size()); + Assert.AreEqual(0, list.Size); } [Test] - public void ShouldExpectedOneWhenSizeAfterAddToEmptyList() + public void ShouldSizeIncrementedWhenSizeAfterAddNonExisintgElement() { list.Add(1); - Assert.AreEqual(1, list.Size()); + Assert.AreEqual(1, list.Size); } [Test] @@ -67,7 +67,7 @@ public void ShouldExpectedFalseWhenContainsForExistingElement() public void ShouldExpectedFalseWhenContainsForRemovedElement() { list.Add(2); - list.RemoveAt(2); + list.Remove(2); Assert.AreEqual(false, list.Contains(2)); } From fb1242ccca3d8c8c6f5fc8bf0b0e3d575decbd2a Mon Sep 17 00:00:00 2001 From: Roman Date: Wed, 1 Jun 2022 13:39:09 +0300 Subject: [PATCH 06/10] The uniqueness invariant in the list is no longer violated --- List/List/List/List/IUniqueList.cs | 57 ------------------- List/List/List/List/List.cs | 33 +++++++++-- .../List/RemoveNonExistingElementException.cs | 7 +-- List/List/List/List/RepeatValueException.cs | 7 +-- List/List/List/List/UniqueList.cs | 28 ++++++++- List/List/List/ListTest/ListTest.cs | 11 +++- 6 files changed, 68 insertions(+), 75 deletions(-) delete mode 100644 List/List/List/List/IUniqueList.cs diff --git a/List/List/List/List/IUniqueList.cs b/List/List/List/List/IUniqueList.cs deleted file mode 100644 index 5a9d876..0000000 --- a/List/List/List/List/IUniqueList.cs +++ /dev/null @@ -1,57 +0,0 @@ -namespace List; - -/// -/// Class interface for a unique list -/// -/// Еype of items in the list -public interface IUniqueList -{ - /// - /// Function for changing the value of an element by index - /// - /// Item position in the list - /// New value - public bool ChangeElement(int index, T value); - - /// - /// Function for printing a list - /// - public void PrintList(); - - /// - /// Аunction to search for an item in the list - /// - /// Search value - /// Is there an item in the list - public bool Contains(T value); - - /// - /// Function for deleting a list - /// - public void ClearList(); - - /// - /// Function for adding an item to a list - /// - /// Type of item value in the list - public void Add(T value); - - /// - /// Function to remove an item from the unique list - /// - /// Item position in the list - public bool RemoveAt(int index); - - /// - /// Function to remove an item from the list - /// - /// Value to be deleted - /// was the value in the list - public bool Remove(T value); - - /// - /// Size property - /// - /// Number of element on list - public int Size { get;} -} \ No newline at end of file diff --git a/List/List/List/List/List.cs b/List/List/List/List/List.cs index a19b10a..9480100 100644 --- a/List/List/List/List/List.cs +++ b/List/List/List/List/List.cs @@ -7,7 +7,7 @@ /// Сlass representing a list /// /// type of item values in the list -abstract public class SinglyLinkedList : IUniqueList +public class SinglyLinkedList where T : IComparable { /// /// Class for storing list items @@ -20,6 +20,10 @@ private class ListElement private ListElement? head; private ListElement? tail; + + /// + /// List size + /// public int Size { get; private set; } /// @@ -42,7 +46,7 @@ public virtual void Add(T value) return; } - var newTail = new ListElement(){}; + var newTail = new ListElement(); newTail.Value = value; tail.Next = newTail; tail = newTail; @@ -68,7 +72,8 @@ public virtual bool RemoveAt(int index) } var element = head; - var copyElement = new ListElement(); + ListElement? copyElement = null; + for (int i = 0; i < index - 1; i++) { if (i == index - 1) @@ -118,7 +123,7 @@ public virtual bool Remove(T value) /// /// Item position in the list /// New value - public bool ChangeElement(int index, T value) + public virtual bool ChangeElement(int index, T value) { if (index >= Size || index < 0) { @@ -155,7 +160,7 @@ public void PrintList() } /// - /// Аunction to search for an item in the list + /// Function to search for an item in the list /// /// Search value /// Is there an item in the list @@ -164,7 +169,7 @@ public bool Contains(T value) var element = head; while (element != null) { - if (value != null && value.Equals(element.Value)) + if (value != null && value.CompareTo(element.Value) == 0) { return true; } @@ -180,4 +185,20 @@ public bool Contains(T value) /// public void ClearList() => head = null; + public T GetItemByIndex(int index) + { + if (index >= Size || index < 0) + { + throw new ArgumentOutOfRangeException(); + } + + ListElement? element = head; + for (int i = 0; i < index; i++) + { + element = element?.Next; + } + + return element!.Value!; + } + } \ No newline at end of file diff --git a/List/List/List/List/RemoveNonExistingElementException.cs b/List/List/List/List/RemoveNonExistingElementException.cs index b1fc145..b32a7a1 100644 --- a/List/List/List/List/RemoveNonExistingElementException.cs +++ b/List/List/List/List/RemoveNonExistingElementException.cs @@ -1,9 +1,6 @@ namespace List; /// -/// A class for creating custom exceptions +/// Exception for the case of deleting a non-existent element /// -public class RemoveNonExistingElementException : Exception -{ - public RemoveNonExistingElementException() : base() { } -} \ No newline at end of file +public class RemoveNonExistingElementException : InvalidOperationException{ } \ No newline at end of file diff --git a/List/List/List/List/RepeatValueException.cs b/List/List/List/List/RepeatValueException.cs index a328802..d3d4e35 100644 --- a/List/List/List/List/RepeatValueException.cs +++ b/List/List/List/List/RepeatValueException.cs @@ -1,9 +1,6 @@ namespace List; /// -/// A class for creating custom exceptions +/// Exception for the case of adding an existing element /// -public class RepeatValueException : Exception -{ - public RepeatValueException() : base() { } -} +public class RepeatValueException : InvalidOperationException{ } diff --git a/List/List/List/List/UniqueList.cs b/List/List/List/List/UniqueList.cs index cde0376..b591913 100644 --- a/List/List/List/List/UniqueList.cs +++ b/List/List/List/List/UniqueList.cs @@ -3,7 +3,7 @@ /// /// Class representing a list of unique values /// -public class UniqueList : SinglyLinkedList +public class UniqueList : SinglyLinkedList where T : IComparable { /// /// Function for adding an item to a list @@ -47,4 +47,30 @@ public override bool Remove(T value) return false; } + + /// + /// Function for changing the value of an element by index + /// + /// Item position in the list + /// New value + public override bool ChangeElement(int index, T value) + { + if (index >= Size || index < 0) + { + return false; + } + + if (GetItemByIndex(index).CompareTo(value) == 0) + { + return true; + } + + if (Contains(value)) + { + throw new InvalidOperationException(); + } + + ChangeElement(index, value); + return true; + } } diff --git a/List/List/List/ListTest/ListTest.cs b/List/List/List/ListTest/ListTest.cs index 47fc47e..5ef74f1 100644 --- a/List/List/List/ListTest/ListTest.cs +++ b/List/List/List/ListTest/ListTest.cs @@ -2,10 +2,11 @@ namespace ListTest; using NUnit.Framework; using List; +using System; public class Tests { - IUniqueList list = new UniqueList(); + private UniqueList list = new UniqueList(); [SetUp] public void Setup() @@ -49,6 +50,14 @@ public void ShouldThrowsRepeatValueExceptionWhenAddExistingElement() Assert.Throws(() => list.Add(1)); } + [Test] + public void ShouldThrowsInvalidOperationExceptionWhenChandeElementWithExistingOne() + { + list.Add(1); + list.Add(2); + Assert.Throws(() => list.ChangeElement(0, 2)); + } + [Test] public void ShouldExpectedTrueWhenContainsForExistingElement() { From 6dfba1d19cf27fffdba21e08651098e1cef191bc Mon Sep 17 00:00:00 2001 From: Roman Date: Wed, 1 Jun 2022 13:59:06 +0300 Subject: [PATCH 07/10] Fixed a stack overflow bug --- List/List/List/List/UniqueList.cs | 4 ++-- List/List/List/ListTest/ListTest.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/List/List/List/List/UniqueList.cs b/List/List/List/List/UniqueList.cs index b591913..0413598 100644 --- a/List/List/List/List/UniqueList.cs +++ b/List/List/List/List/UniqueList.cs @@ -60,7 +60,7 @@ public override bool ChangeElement(int index, T value) return false; } - if (GetItemByIndex(index).CompareTo(value) == 0) + if (base.GetItemByIndex(index).CompareTo(value) == 0) { return true; } @@ -70,7 +70,7 @@ public override bool ChangeElement(int index, T value) throw new InvalidOperationException(); } - ChangeElement(index, value); + base.ChangeElement(index, value); return true; } } diff --git a/List/List/List/ListTest/ListTest.cs b/List/List/List/ListTest/ListTest.cs index 5ef74f1..88ef85f 100644 --- a/List/List/List/ListTest/ListTest.cs +++ b/List/List/List/ListTest/ListTest.cs @@ -6,7 +6,7 @@ namespace ListTest; public class Tests { - private UniqueList list = new UniqueList(); + private UniqueList list = new(); [SetUp] public void Setup() From 591295442e0aeb74f99bb37dfe34f10f76e682bb Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 2 Jun 2022 00:15:17 +0300 Subject: [PATCH 08/10] rename yml --- .github/workflows/{dotnet.yml => List.yml} | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) rename .github/workflows/{dotnet.yml => List.yml} (56%) diff --git a/.github/workflows/dotnet.yml b/.github/workflows/List.yml similarity index 56% rename from .github/workflows/dotnet.yml rename to .github/workflows/List.yml index a6a59da..340cd7a 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/List.yml @@ -1,9 +1,9 @@ name: Build -on: [push, pull_request] +on: [push] jobs: - build: + build-Windows: runs-on: windows-latest @@ -20,4 +20,21 @@ jobs: - name: Test run: $slnList = Get-ChildItem $foo.FullName -Recurse -Filter '*.sln'; foreach ($file in $slnList) {dotnet test $file.FullName} + build-Ubuntu: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Build + uses: actions/setup-dotnet@v1 + with: + dotnet-version: '6.x' + - name: Restore + run: for f in $(find . -name "*.sln"); do dotnet restore $f; done + - name: Build + run: for f in $(find . -name "*.sln"); do dotnet build $f; done + - name: Test + run: for f in $(find . -name "*.sln"); do dotnet test $f; done + From 606580ea25b84d639a3b06d9948400fa418af54d Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 2 Jun 2022 01:17:46 +0300 Subject: [PATCH 09/10] rename yml --- .github/workflows/{List.yml => dotnet.yml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/workflows/{List.yml => dotnet.yml} (100%) diff --git a/.github/workflows/List.yml b/.github/workflows/dotnet.yml similarity index 100% rename from .github/workflows/List.yml rename to .github/workflows/dotnet.yml From fe398718b49ddb6ee1e00cfd5e4c95eb5f94ded0 Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 2 Jun 2022 03:03:49 +0300 Subject: [PATCH 10/10] formatted --- List/List/List/List/RemoveNonExistingElementException.cs | 2 +- List/List/List/List/RepeatValueException.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/List/List/List/List/RemoveNonExistingElementException.cs b/List/List/List/List/RemoveNonExistingElementException.cs index b32a7a1..1144954 100644 --- a/List/List/List/List/RemoveNonExistingElementException.cs +++ b/List/List/List/List/RemoveNonExistingElementException.cs @@ -3,4 +3,4 @@ /// /// Exception for the case of deleting a non-existent element /// -public class RemoveNonExistingElementException : InvalidOperationException{ } \ No newline at end of file +public class RemoveNonExistingElementException : InvalidOperationException { } \ No newline at end of file diff --git a/List/List/List/List/RepeatValueException.cs b/List/List/List/List/RepeatValueException.cs index d3d4e35..4ade6f3 100644 --- a/List/List/List/List/RepeatValueException.cs +++ b/List/List/List/List/RepeatValueException.cs @@ -3,4 +3,4 @@ /// /// Exception for the case of adding an existing element /// -public class RepeatValueException : InvalidOperationException{ } +public class RepeatValueException : InvalidOperationException { }