-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Writing basic stack functions, writing tests
- Loading branch information
Showing
8 changed files
with
310 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Stack; | ||
|
||
public class Solution | ||
{ | ||
static void Main() | ||
{ | ||
return; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Stack; | ||
|
||
public class StackOnArray<T> | ||
{ | ||
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]} "); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Stack; | ||
|
||
public class StackOnLists<T> | ||
{ | ||
private StackOnLists<T>? head; | ||
private StackOnLists<T>? next; | ||
private T? value; | ||
private int numberOfElements; | ||
|
||
public bool IsEmpty() | ||
{ | ||
return numberOfElements == 0; | ||
} | ||
|
||
public void Push(T value) | ||
{ | ||
numberOfElements++; | ||
StackOnLists<T> newHead = new StackOnLists<T>(); | ||
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<T> 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} "); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" /> | ||
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" /> | ||
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" /> | ||
<PackageReference Include="coverlet.collector" Version="3.1.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Stack\Stack.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Stack; | ||
|
||
namespace TestStack; | ||
|
||
[TestClass] | ||
public class TestStackOnArray | ||
{ | ||
[TestMethod] | ||
public void TestPush() | ||
{ | ||
StackOnArray<int> stackOnArray = new StackOnArray<int>(); | ||
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<string> stackOnArray = new StackOnArray<string>(); | ||
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<string> stackOnArray = new StackOnArray<string>(); | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Stack; | ||
|
||
namespace TestStack; | ||
|
||
[TestClass] | ||
public class TestStackOnLists | ||
{ | ||
[TestMethod] | ||
public void TestPush() | ||
{ | ||
StackOnLists<int> stackOnLists = new StackOnLists<int>(); | ||
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<string> stackOnLists = new StackOnLists<string>(); | ||
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<string> stackOnLists = new StackOnLists<string>(); | ||
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); | ||
} | ||
} |