Skip to content

Commit 3e33fb1

Browse files
committed
Writing basic stack functions, writing tests
1 parent 3f3d4c2 commit 3e33fb1

8 files changed

Lines changed: 310 additions & 0 deletions

File tree

Stack/Stack.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.1.32210.238
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Stack", "Stack\Stack.csproj", "{70DCF9E2-593E-4AB9-A056-F0C172DA3419}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestStack", "TestStackOnArray\TestStack.csproj", "{60E6252B-6D54-47D9-B466-95927AFE6E5D}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{70DCF9E2-593E-4AB9-A056-F0C172DA3419}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{70DCF9E2-593E-4AB9-A056-F0C172DA3419}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{70DCF9E2-593E-4AB9-A056-F0C172DA3419}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{70DCF9E2-593E-4AB9-A056-F0C172DA3419}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{60E6252B-6D54-47D9-B466-95927AFE6E5D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{60E6252B-6D54-47D9-B466-95927AFE6E5D}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{60E6252B-6D54-47D9-B466-95927AFE6E5D}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{60E6252B-6D54-47D9-B466-95927AFE6E5D}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {F2B1E14B-C1F7-471E-987B-DCCA99BA603E}
30+
EndGlobalSection
31+
EndGlobal

Stack/Stack/Solution.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Stack;
2+
3+
public class Solution
4+
{
5+
static void Main()
6+
{
7+
return;
8+
}
9+
}

Stack/Stack/Stack.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>

Stack/Stack/StackOnArray.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace Stack;
5+
6+
public class StackOnArray<T>
7+
{
8+
private T[] values;
9+
private int numberOfElements;
10+
11+
public StackOnArray()
12+
{
13+
values = new T[20];
14+
}
15+
16+
public bool IsEmpty()
17+
{
18+
return numberOfElements == 0;
19+
}
20+
21+
public void Push(T value)
22+
{
23+
if(numberOfElements == values.Length)
24+
{
25+
Array.Resize(ref values, values.Length + 20);
26+
}
27+
numberOfElements++;
28+
values[numberOfElements - 1] = value;
29+
}
30+
31+
public T Pop()
32+
{
33+
if(numberOfElements == 0)
34+
{
35+
throw new InvalidOperationException("Stack is empty");
36+
}
37+
T topOfSTack = values[numberOfElements - 1];
38+
numberOfElements--;
39+
return topOfSTack;
40+
}
41+
42+
public T ReturnTopOfTheStack()
43+
{
44+
return values[numberOfElements - 1];
45+
}
46+
47+
public int ReturnNumberOfElements()
48+
{
49+
return numberOfElements;
50+
}
51+
52+
public void PrintStack()
53+
{
54+
for (int i = 0; i < numberOfElements; i++)
55+
{
56+
Console.Write($"{values[i]} ");
57+
}
58+
}
59+
}

Stack/Stack/StackOnLists.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace Stack;
5+
6+
public class StackOnLists<T>
7+
{
8+
private StackOnLists<T>? head;
9+
private StackOnLists<T>? next;
10+
private T? value;
11+
private int numberOfElements;
12+
13+
public bool IsEmpty()
14+
{
15+
return numberOfElements == 0;
16+
}
17+
18+
public void Push(T value)
19+
{
20+
numberOfElements++;
21+
StackOnLists<T> newHead = new StackOnLists<T>();
22+
newHead.next = head;
23+
head = newHead;
24+
head.value = value;
25+
}
26+
27+
public T Pop()
28+
{
29+
if (head == null || head.value == null)
30+
{
31+
throw new InvalidOperationException("Stack is empty");
32+
}
33+
var topOfSTack = head;
34+
head = head.next;
35+
numberOfElements--;
36+
return topOfSTack.value;
37+
}
38+
39+
public int ReturnNumberOfElements()
40+
{
41+
return numberOfElements;
42+
}
43+
44+
public T ReturnTopOfTheStack()
45+
{
46+
if (head == null || head.value == null)
47+
{
48+
throw new InvalidOperationException("Stack is empty");
49+
}
50+
return head.value;
51+
}
52+
53+
public void PrintStack()
54+
{
55+
if (head == null)
56+
{
57+
throw new InvalidOperationException("Stack is empty");
58+
}
59+
StackOnLists<T> copyHead = head;
60+
while (copyHead != null && copyHead.next != null)
61+
{
62+
Console.Write($"{copyHead.value} ");
63+
copyHead = copyHead.next;
64+
}
65+
if (copyHead != null && copyHead.value != null)
66+
{
67+
Console.Write($"{copyHead.value} ");
68+
}
69+
}
70+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
7+
<IsPackable>false</IsPackable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
12+
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
13+
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
14+
<PackageReference Include="coverlet.collector" Version="3.1.0" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<ProjectReference Include="..\Stack\Stack.csproj" />
19+
</ItemGroup>
20+
21+
</Project>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using Stack;
3+
4+
namespace TestStack;
5+
6+
[TestClass]
7+
public class TestStackOnArray
8+
{
9+
[TestMethod]
10+
public void TestPush()
11+
{
12+
StackOnArray<int> stackOnArray = new StackOnArray<int>();
13+
stackOnArray.Push(1);
14+
Assert.AreEqual(stackOnArray.ReturnTopOfTheStack(), 1);
15+
stackOnArray.Push(2);
16+
Assert.AreEqual(stackOnArray.ReturnTopOfTheStack(), 2);
17+
stackOnArray.Push(4);
18+
Assert.AreEqual(stackOnArray.ReturnTopOfTheStack(), 4);
19+
stackOnArray.Push(5);
20+
Assert.AreEqual(stackOnArray.ReturnTopOfTheStack(), 5);
21+
}
22+
23+
[TestMethod]
24+
public void TestPop()
25+
{
26+
StackOnArray<string> stackOnArray = new StackOnArray<string>();
27+
stackOnArray.Push("first");
28+
stackOnArray.Push("second");
29+
stackOnArray.Push("hello");
30+
stackOnArray.Push("kek");
31+
stackOnArray.Pop();
32+
Assert.AreEqual(stackOnArray.ReturnTopOfTheStack(), "hello");
33+
stackOnArray.Pop();
34+
Assert.AreEqual(stackOnArray.ReturnTopOfTheStack(), "second");
35+
stackOnArray.Pop();
36+
Assert.AreEqual(stackOnArray.ReturnTopOfTheStack(), "first");
37+
}
38+
39+
[TestMethod]
40+
public void TestReturnNumberOfElements()
41+
{
42+
StackOnArray<string> stackOnArray = new StackOnArray<string>();
43+
stackOnArray.Push("first");
44+
stackOnArray.Push("second");
45+
stackOnArray.Push("hello");
46+
stackOnArray.Push("kek");
47+
Assert.AreEqual(stackOnArray.ReturnNumberOfElements(), 4);
48+
stackOnArray.Pop();
49+
Assert.AreEqual(stackOnArray.ReturnNumberOfElements(), 3);
50+
stackOnArray.Pop();
51+
Assert.AreEqual(stackOnArray.ReturnNumberOfElements(), 2);
52+
stackOnArray.Pop();
53+
Assert.AreEqual(stackOnArray.ReturnNumberOfElements(), 1);
54+
}
55+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using Stack;
3+
4+
namespace TestStack;
5+
6+
[TestClass]
7+
public class TestStackOnLists
8+
{
9+
[TestMethod]
10+
public void TestPush()
11+
{
12+
StackOnLists<int> stackOnLists = new StackOnLists<int>();
13+
stackOnLists.Push(1);
14+
Assert.AreEqual(stackOnLists.ReturnTopOfTheStack(), 1);
15+
stackOnLists.Push(2);
16+
Assert.AreEqual(stackOnLists.ReturnTopOfTheStack(), 2);
17+
stackOnLists.Push(4);
18+
Assert.AreEqual(stackOnLists.ReturnTopOfTheStack(), 4);
19+
stackOnLists.Push(5);
20+
Assert.AreEqual(stackOnLists.ReturnTopOfTheStack(), 5);
21+
}
22+
23+
[TestMethod]
24+
public void TestPop()
25+
{
26+
StackOnLists<string> stackOnLists = new StackOnLists<string>();
27+
stackOnLists.Push("first");
28+
stackOnLists.Push("second");
29+
stackOnLists.Push("hello");
30+
stackOnLists.Push("kek");
31+
stackOnLists.Pop();
32+
Assert.AreEqual(stackOnLists.ReturnTopOfTheStack(), "hello");
33+
stackOnLists.Pop();
34+
Assert.AreEqual(stackOnLists.ReturnTopOfTheStack(), "second");
35+
stackOnLists.Pop();
36+
Assert.AreEqual(stackOnLists.ReturnTopOfTheStack(), "first");
37+
}
38+
39+
[TestMethod]
40+
public void TestReturnNumberOfElements()
41+
{
42+
StackOnLists<string> stackOnLists = new StackOnLists<string>();
43+
stackOnLists.Push("first");
44+
stackOnLists.Push("second");
45+
stackOnLists.Push("hello");
46+
stackOnLists.Push("kek");
47+
Assert.AreEqual(stackOnLists.ReturnNumberOfElements(), 4);
48+
stackOnLists.Pop();
49+
Assert.AreEqual(stackOnLists.ReturnNumberOfElements(), 3);
50+
stackOnLists.Pop();
51+
Assert.AreEqual(stackOnLists.ReturnNumberOfElements(), 2);
52+
stackOnLists.Pop();
53+
Assert.AreEqual(stackOnLists.ReturnNumberOfElements(), 1);
54+
}
55+
}

0 commit comments

Comments
 (0)