chore(CSharp): add implementation of queue using two stacks (#448)
Co-authored-by: Ming Tsai <37890026+ming-tsai@users.noreply.github.com>pull/450/head
parent
cbe6df6e70
commit
25fb424140
|
@ -17,6 +17,9 @@ For running the `.cs` file please using [.Net Finddle](https://dotnetfiddle.net/
|
|||
## Maths
|
||||
- [Abundant Number](src/Maths/abundant-number.cs)
|
||||
|
||||
## Queues
|
||||
- [Queue Implementation Using Two Stacks](src/Queues/queue-implementation-using-two-stacks.cs)
|
||||
|
||||
## Recusrsion
|
||||
- [Factorial](src/Recursion/factorial.cs)
|
||||
|
||||
|
|
|
@ -0,0 +1,87 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Algorithms.Queues
|
||||
{
|
||||
public class QueueImplementationUsingTwoStacks
|
||||
{
|
||||
public class MyQueue<T>
|
||||
{
|
||||
private Stack<T> _stack1, _stack2;
|
||||
|
||||
public MyQueue()
|
||||
{
|
||||
_stack1 = new Stack<T>();
|
||||
_stack2 = new Stack<T>();
|
||||
}
|
||||
|
||||
public void Push(T x)
|
||||
{
|
||||
_stack1.Push(x);
|
||||
}
|
||||
|
||||
public T Pop()
|
||||
{
|
||||
if (Empty())
|
||||
{
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
|
||||
if(_stack2.Count > 0)
|
||||
{
|
||||
return _stack2.Pop();
|
||||
}
|
||||
|
||||
while(_stack1.Count > 0)
|
||||
{
|
||||
_stack2.Push(_stack1.Pop());
|
||||
}
|
||||
|
||||
return _stack2.Pop();
|
||||
}
|
||||
|
||||
public T Peek()
|
||||
{
|
||||
if (Empty())
|
||||
{
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
|
||||
if (_stack2.Count > 0)
|
||||
{
|
||||
return _stack2.Peek();
|
||||
}
|
||||
|
||||
while (_stack1.Count > 0)
|
||||
{
|
||||
_stack2.Push(_stack1.Pop());
|
||||
}
|
||||
|
||||
return _stack2.Peek();
|
||||
}
|
||||
|
||||
public bool Empty()
|
||||
{
|
||||
return _stack1.Count == 0 && _stack2.Count == 0;
|
||||
}
|
||||
|
||||
public int Count()
|
||||
{
|
||||
return _stack1.Count + _stack2.Count;
|
||||
}
|
||||
}
|
||||
|
||||
public static void Main()
|
||||
{
|
||||
MyQueue<int> myQueue = new MyQueue<int>();
|
||||
|
||||
myQueue.Push(3);
|
||||
myQueue.Push(4);
|
||||
Console.Write($"{myQueue.Peek()} ");
|
||||
myQueue.Pop();
|
||||
Console.Write($"{myQueue.Peek()} ");
|
||||
myQueue.Push(7);
|
||||
Console.Write($"{myQueue.Peek()} ");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
using NUnit.Framework;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Algorithms.Tests.Queues
|
||||
{
|
||||
[TestFixture]
|
||||
class QueueImplementationUsingTwoStacks
|
||||
{
|
||||
[Test]
|
||||
public void TestQueueUsingTwoStacks1_ShouldGetExpectedResult()
|
||||
{
|
||||
Algorithms.Queues.QueueImplementationUsingTwoStacks.MyQueue<int> myQueue;
|
||||
myQueue = new Algorithms.Queues.QueueImplementationUsingTwoStacks.MyQueue<int>();
|
||||
|
||||
List<int> result = new List<int>();
|
||||
|
||||
myQueue.Push(7);
|
||||
result.Add(myQueue.Peek());
|
||||
result.Add(myQueue.Count());
|
||||
|
||||
myQueue.Push(118);
|
||||
result.Add(myQueue.Count());
|
||||
result.Add(myQueue.Peek());
|
||||
|
||||
myQueue.Push(107);
|
||||
result.Add(myQueue.Count());
|
||||
result.Add(myQueue.Peek());
|
||||
|
||||
myQueue.Pop();
|
||||
result.Add(myQueue.Peek());
|
||||
result.Add(myQueue.Count());
|
||||
|
||||
myQueue.Pop();
|
||||
result.Add(myQueue.Peek());
|
||||
result.Add(myQueue.Count());
|
||||
|
||||
myQueue.Pop();
|
||||
result.Add(myQueue.Count());
|
||||
|
||||
Assert.AreEqual("7 1 2 7 3 7 118 2 107 1 0", string.Join(" ", result));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue