Skip to content

Commit 30e750e

Browse files
authored
Add Queue-based Stack (TheAlgorithms#412)
1 parent b6d7385 commit 30e750e

File tree

3 files changed

+204
-0
lines changed

3 files changed

+204
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
using DataStructures.Stack;
2+
using NUnit.Framework;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace DataStructures.Tests.Stack
10+
{
11+
public static class QueueBasedStackTests
12+
{
13+
[Test]
14+
public static void PopWorksCorrectly()
15+
{
16+
//Arrange
17+
QueueBasedStack<char> s = new QueueBasedStack<char>();
18+
s.Push('A');
19+
s.Push('B');
20+
s.Push('C');
21+
var result = new StringBuilder();
22+
23+
//Act
24+
for (int i = 0; i < 3; i++)
25+
{
26+
result.Append(s.Pop());
27+
}
28+
29+
30+
//Assert
31+
Assert.That("CBA", Is.EqualTo(result.ToString()));
32+
Assert.IsTrue(s.IsEmpty(), "Stack is Empty");
33+
}
34+
[Test]
35+
public static void PeekWorksCorrectly()
36+
{
37+
//Arrange
38+
QueueBasedStack<int> s = new QueueBasedStack<int>();
39+
s.Push(1);
40+
s.Push(2);
41+
s.Push(3);
42+
var peeked = 0;
43+
44+
//Act
45+
for (int i = 0; i < 3; i++)
46+
{
47+
peeked = s.Peek();
48+
}
49+
50+
51+
//Assert
52+
Assert.That(3, Is.EqualTo(peeked));
53+
Assert.IsFalse(s.IsEmpty(), "Stack is Empty");
54+
}
55+
[Test]
56+
public static void PopEmptyStackThrowsInvalidOperationException()
57+
{
58+
//Arrange
59+
var s = new QueueBasedStack<int>();
60+
Exception? exception = null;
61+
62+
//Act
63+
try
64+
{
65+
s.Pop();
66+
}
67+
catch (Exception ex)
68+
{
69+
exception = ex;
70+
}
71+
72+
//Assert
73+
Assert.That(exception?.GetType(), Is.EqualTo(typeof(InvalidOperationException)));
74+
}
75+
[Test]
76+
public static void PeekEmptyStackThrowsInvalidOperationException()
77+
{
78+
//Arrange
79+
var s = new QueueBasedStack<int>();
80+
Exception? exception = null;
81+
82+
//Act
83+
try
84+
{
85+
s.Peek();
86+
}
87+
catch (Exception ex)
88+
{
89+
exception = ex;
90+
}
91+
92+
//Assert
93+
Assert.That(exception?.GetType(), Is.EqualTo(typeof(InvalidOperationException)));
94+
}
95+
[Test]
96+
public static void ClearWorksCorrectly()
97+
{
98+
// Arrange
99+
var s = new QueueBasedStack<int>();
100+
s.Push(1);
101+
s.Push(2);
102+
103+
// Act
104+
s.Clear();
105+
106+
// Assert
107+
Assert.IsTrue(s.IsEmpty(), "Queue is empty");
108+
109+
}
110+
[Test]
111+
public static void LengthWorksCorrectly()
112+
{
113+
// Arrange
114+
var s = new QueueBasedStack<int>();
115+
s.Push(1);
116+
s.Push(2);
117+
var length = 0;
118+
119+
// Act
120+
length = s.Length();
121+
122+
// Assert
123+
Assert.That(2, Is.EqualTo(length));
124+
125+
}
126+
}
127+
}
+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace DataStructures.Stack
8+
{
9+
public class QueueBasedStack<T>
10+
{
11+
private readonly Queue<T> queue;
12+
13+
public QueueBasedStack() => queue = new Queue<T>();
14+
15+
/// <summary>
16+
/// Clears the stack.
17+
/// </summary>
18+
public void Clear() => queue.Clear();
19+
20+
public bool IsEmpty() => queue.Count == 0;
21+
22+
/// <summary>
23+
/// Adds an item on top of the stack.
24+
/// </summary>
25+
/// <param name="item">Item to be added on top of stack.</param>
26+
public void Push(T item) => queue.Enqueue(item);
27+
28+
/// <summary>
29+
/// Removes an item from top of the stack and returns it.
30+
/// </summary>
31+
/// <returns>item on top of stack.</returns>
32+
/// <exception cref="InvalidOperationException">Throw if stack is empty.</exception>
33+
public T Pop()
34+
{
35+
if (IsEmpty())
36+
{
37+
throw new InvalidOperationException("The stack contains no items.");
38+
}
39+
40+
for (int i = 0; i < queue.Count - 1; i++)
41+
{
42+
queue.Enqueue(queue.Dequeue());
43+
}
44+
45+
return queue.Dequeue();
46+
}
47+
48+
/// <summary>
49+
/// return an item from the top of the stack without removing it.
50+
/// </summary>
51+
/// <returns>item on top of the stack.</returns>
52+
/// <exception cref="InvalidOperationException">Throw if stack is empty.</exception>
53+
public T Peek()
54+
{
55+
if (IsEmpty())
56+
{
57+
throw new InvalidOperationException("The stack contains no items.");
58+
}
59+
60+
for (int i = 0; i < queue.Count - 1; i++)
61+
{
62+
queue.Enqueue(queue.Dequeue());
63+
}
64+
65+
var item = queue.Peek();
66+
queue.Enqueue(queue.Dequeue());
67+
return item;
68+
}
69+
70+
/// <summary>
71+
/// returns the count of items on the stack.
72+
/// </summary>
73+
/// <returns>number of items on the stack.</returns>
74+
public int Length() => queue.Count;
75+
}
76+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ find more than one implementation for the same objective but using different alg
228228
* [Stack](./DataStructures/Stack)
229229
* [Array-based Stack](./DataStructures/Stack/ArrayBasedStack.cs)
230230
* [List-based Stack](./DataStructures/Stack/ListBasedStack.cs)
231+
* [Queue-based Stack](./DataStructures/Stack/QueueBasedStack.cs)
231232
* [Heap](./DataStructures/Heap)
232233
* [Min-Max Heap](./DataStructures/Heap/MinMaxHeap.cs)
233234
* [Binary Heap](./DataStructures/Heap/BinaryHeap.cs)

0 commit comments

Comments
 (0)