diff --git a/algorithms/CSharp/README.md b/algorithms/CSharp/README.md index 040bd275..081b254e 100644 --- a/algorithms/CSharp/README.md +++ b/algorithms/CSharp/README.md @@ -17,8 +17,11 @@ 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) ## Graph -- [Depth First Search](src/Graph/depth-first-search.cs) \ No newline at end of file +- [Depth First Search](src/Graph/depth-first-search.cs) diff --git a/algorithms/CSharp/src/Queues/queue-implementation-using-two-stacks.cs b/algorithms/CSharp/src/Queues/queue-implementation-using-two-stacks.cs new file mode 100644 index 00000000..ae90c266 --- /dev/null +++ b/algorithms/CSharp/src/Queues/queue-implementation-using-two-stacks.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections.Generic; + +namespace Algorithms.Queues +{ + public class QueueImplementationUsingTwoStacks + { + public class MyQueue + { + private Stack _stack1, _stack2; + + public MyQueue() + { + _stack1 = new Stack(); + _stack2 = new Stack(); + } + + 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 myQueue = new MyQueue(); + + myQueue.Push(3); + myQueue.Push(4); + Console.Write($"{myQueue.Peek()} "); + myQueue.Pop(); + Console.Write($"{myQueue.Peek()} "); + myQueue.Push(7); + Console.Write($"{myQueue.Peek()} "); + } + } +} diff --git a/algorithms/CSharp/test/Queues/queue-implementation-using-two-stacks.cs b/algorithms/CSharp/test/Queues/queue-implementation-using-two-stacks.cs new file mode 100644 index 00000000..b212d847 --- /dev/null +++ b/algorithms/CSharp/test/Queues/queue-implementation-using-two-stacks.cs @@ -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 myQueue; + myQueue = new Algorithms.Queues.QueueImplementationUsingTwoStacks.MyQueue(); + + List result = new List(); + + 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)); + } + } +}