From 3e33fb1b7353553b86288f85d13be81a724854ea Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Mon, 28 Feb 2022 19:03:08 +0300 Subject: [PATCH] Writing basic stack functions, writing tests --- Stack/Stack.sln | 31 ++++++++++ Stack/Stack/Solution.cs | 9 +++ Stack/Stack/Stack.csproj | 10 ++++ Stack/Stack/StackOnArray.cs | 59 ++++++++++++++++++ Stack/Stack/StackOnLists.cs | 70 ++++++++++++++++++++++ Stack/TestStackOnArray/TestStack.csproj | 21 +++++++ Stack/TestStackOnArray/TestStackOnArray.cs | 55 +++++++++++++++++ Stack/TestStackOnArray/TestStackOnLists.cs | 55 +++++++++++++++++ 8 files changed, 310 insertions(+) create mode 100644 Stack/Stack.sln create mode 100644 Stack/Stack/Solution.cs create mode 100644 Stack/Stack/Stack.csproj create mode 100644 Stack/Stack/StackOnArray.cs create mode 100644 Stack/Stack/StackOnLists.cs create mode 100644 Stack/TestStackOnArray/TestStack.csproj create mode 100644 Stack/TestStackOnArray/TestStackOnArray.cs create mode 100644 Stack/TestStackOnArray/TestStackOnLists.cs diff --git a/Stack/Stack.sln b/Stack/Stack.sln new file mode 100644 index 0000000..69b9eb5 --- /dev/null +++ b/Stack/Stack.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.1.32210.238 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Stack", "Stack\Stack.csproj", "{70DCF9E2-593E-4AB9-A056-F0C172DA3419}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestStack", "TestStackOnArray\TestStack.csproj", "{60E6252B-6D54-47D9-B466-95927AFE6E5D}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {70DCF9E2-593E-4AB9-A056-F0C172DA3419}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {70DCF9E2-593E-4AB9-A056-F0C172DA3419}.Debug|Any CPU.Build.0 = Debug|Any CPU + {70DCF9E2-593E-4AB9-A056-F0C172DA3419}.Release|Any CPU.ActiveCfg = Release|Any CPU + {70DCF9E2-593E-4AB9-A056-F0C172DA3419}.Release|Any CPU.Build.0 = Release|Any CPU + {60E6252B-6D54-47D9-B466-95927AFE6E5D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {60E6252B-6D54-47D9-B466-95927AFE6E5D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {60E6252B-6D54-47D9-B466-95927AFE6E5D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {60E6252B-6D54-47D9-B466-95927AFE6E5D}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {F2B1E14B-C1F7-471E-987B-DCCA99BA603E} + EndGlobalSection +EndGlobal diff --git a/Stack/Stack/Solution.cs b/Stack/Stack/Solution.cs new file mode 100644 index 0000000..01b00fc --- /dev/null +++ b/Stack/Stack/Solution.cs @@ -0,0 +1,9 @@ +namespace Stack; + +public class Solution +{ + static void Main() + { + return; + } +} diff --git a/Stack/Stack/Stack.csproj b/Stack/Stack/Stack.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/Stack/Stack/Stack.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/Stack/Stack/StackOnArray.cs b/Stack/Stack/StackOnArray.cs new file mode 100644 index 0000000..7153ef2 --- /dev/null +++ b/Stack/Stack/StackOnArray.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; + +namespace Stack; + +public class StackOnArray +{ + private T[] values; + private int numberOfElements; + + public StackOnArray() + { + values = new T[20]; + } + + public bool IsEmpty() + { + return numberOfElements == 0; + } + + public void Push(T value) + { + if(numberOfElements == values.Length) + { + Array.Resize(ref values, values.Length + 20); + } + numberOfElements++; + values[numberOfElements - 1] = value; + } + + public T Pop() + { + if(numberOfElements == 0) + { + throw new InvalidOperationException("Stack is empty"); + } + T topOfSTack = values[numberOfElements - 1]; + numberOfElements--; + return topOfSTack; + } + + public T ReturnTopOfTheStack() + { + return values[numberOfElements - 1]; + } + + public int ReturnNumberOfElements() + { + return numberOfElements; + } + + public void PrintStack() + { + for (int i = 0; i < numberOfElements; i++) + { + Console.Write($"{values[i]} "); + } + } +} \ No newline at end of file diff --git a/Stack/Stack/StackOnLists.cs b/Stack/Stack/StackOnLists.cs new file mode 100644 index 0000000..5e505f5 --- /dev/null +++ b/Stack/Stack/StackOnLists.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; + +namespace Stack; + +public class StackOnLists +{ + private StackOnLists? head; + private StackOnLists? next; + private T? value; + private int numberOfElements; + + public bool IsEmpty() + { + return numberOfElements == 0; + } + + public void Push(T value) + { + numberOfElements++; + StackOnLists newHead = new StackOnLists(); + newHead.next = head; + head = newHead; + head.value = value; + } + + public T Pop() + { + if (head == null || head.value == null) + { + throw new InvalidOperationException("Stack is empty"); + } + var topOfSTack = head; + head = head.next; + numberOfElements--; + return topOfSTack.value; + } + + public int ReturnNumberOfElements() + { + return numberOfElements; + } + + public T ReturnTopOfTheStack() + { + if (head == null || head.value == null) + { + throw new InvalidOperationException("Stack is empty"); + } + return head.value; + } + + public void PrintStack() + { + if (head == null) + { + throw new InvalidOperationException("Stack is empty"); + } + StackOnLists copyHead = head; + while (copyHead != null && copyHead.next != null) + { + Console.Write($"{copyHead.value} "); + copyHead = copyHead.next; + } + if (copyHead != null && copyHead.value != null) + { + Console.Write($"{copyHead.value} "); + } + } +} diff --git a/Stack/TestStackOnArray/TestStack.csproj b/Stack/TestStackOnArray/TestStack.csproj new file mode 100644 index 0000000..48ef2ac --- /dev/null +++ b/Stack/TestStackOnArray/TestStack.csproj @@ -0,0 +1,21 @@ + + + + net6.0 + enable + + false + + + + + + + + + + + + + + diff --git a/Stack/TestStackOnArray/TestStackOnArray.cs b/Stack/TestStackOnArray/TestStackOnArray.cs new file mode 100644 index 0000000..73e9391 --- /dev/null +++ b/Stack/TestStackOnArray/TestStackOnArray.cs @@ -0,0 +1,55 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Stack; + +namespace TestStack; + +[TestClass] +public class TestStackOnArray +{ + [TestMethod] + public void TestPush() + { + StackOnArray stackOnArray = new StackOnArray(); + stackOnArray.Push(1); + Assert.AreEqual(stackOnArray.ReturnTopOfTheStack(), 1); + stackOnArray.Push(2); + Assert.AreEqual(stackOnArray.ReturnTopOfTheStack(), 2); + stackOnArray.Push(4); + Assert.AreEqual(stackOnArray.ReturnTopOfTheStack(), 4); + stackOnArray.Push(5); + Assert.AreEqual(stackOnArray.ReturnTopOfTheStack(), 5); + } + + [TestMethod] + public void TestPop() + { + StackOnArray stackOnArray = new StackOnArray(); + stackOnArray.Push("first"); + stackOnArray.Push("second"); + stackOnArray.Push("hello"); + stackOnArray.Push("kek"); + stackOnArray.Pop(); + Assert.AreEqual(stackOnArray.ReturnTopOfTheStack(), "hello"); + stackOnArray.Pop(); + Assert.AreEqual(stackOnArray.ReturnTopOfTheStack(), "second"); + stackOnArray.Pop(); + Assert.AreEqual(stackOnArray.ReturnTopOfTheStack(), "first"); + } + + [TestMethod] + public void TestReturnNumberOfElements() + { + StackOnArray stackOnArray = new StackOnArray(); + stackOnArray.Push("first"); + stackOnArray.Push("second"); + stackOnArray.Push("hello"); + stackOnArray.Push("kek"); + Assert.AreEqual(stackOnArray.ReturnNumberOfElements(), 4); + stackOnArray.Pop(); + Assert.AreEqual(stackOnArray.ReturnNumberOfElements(), 3); + stackOnArray.Pop(); + Assert.AreEqual(stackOnArray.ReturnNumberOfElements(), 2); + stackOnArray.Pop(); + Assert.AreEqual(stackOnArray.ReturnNumberOfElements(), 1); + } +} diff --git a/Stack/TestStackOnArray/TestStackOnLists.cs b/Stack/TestStackOnArray/TestStackOnLists.cs new file mode 100644 index 0000000..86f1b9f --- /dev/null +++ b/Stack/TestStackOnArray/TestStackOnLists.cs @@ -0,0 +1,55 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Stack; + +namespace TestStack; + +[TestClass] +public class TestStackOnLists +{ + [TestMethod] + public void TestPush() + { + StackOnLists stackOnLists = new StackOnLists(); + stackOnLists.Push(1); + Assert.AreEqual(stackOnLists.ReturnTopOfTheStack(), 1); + stackOnLists.Push(2); + Assert.AreEqual(stackOnLists.ReturnTopOfTheStack(), 2); + stackOnLists.Push(4); + Assert.AreEqual(stackOnLists.ReturnTopOfTheStack(), 4); + stackOnLists.Push(5); + Assert.AreEqual(stackOnLists.ReturnTopOfTheStack(), 5); + } + + [TestMethod] + public void TestPop() + { + StackOnLists stackOnLists = new StackOnLists(); + stackOnLists.Push("first"); + stackOnLists.Push("second"); + stackOnLists.Push("hello"); + stackOnLists.Push("kek"); + stackOnLists.Pop(); + Assert.AreEqual(stackOnLists.ReturnTopOfTheStack(), "hello"); + stackOnLists.Pop(); + Assert.AreEqual(stackOnLists.ReturnTopOfTheStack(), "second"); + stackOnLists.Pop(); + Assert.AreEqual(stackOnLists.ReturnTopOfTheStack(), "first"); + } + + [TestMethod] + public void TestReturnNumberOfElements() + { + StackOnLists stackOnLists = new StackOnLists(); + stackOnLists.Push("first"); + stackOnLists.Push("second"); + stackOnLists.Push("hello"); + stackOnLists.Push("kek"); + Assert.AreEqual(stackOnLists.ReturnNumberOfElements(), 4); + stackOnLists.Pop(); + Assert.AreEqual(stackOnLists.ReturnNumberOfElements(), 3); + stackOnLists.Pop(); + Assert.AreEqual(stackOnLists.ReturnNumberOfElements(), 2); + stackOnLists.Pop(); + Assert.AreEqual(stackOnLists.ReturnNumberOfElements(), 1); + } +}