diff --git a/232. Implement Queue using Stacks/MyQueue.cs b/232. Implement Queue using Stacks/MyQueue.cs index c5fad50..7a0caef 100644 --- a/232. Implement Queue using Stacks/MyQueue.cs +++ b/232. Implement Queue using Stacks/MyQueue.cs @@ -2,18 +2,19 @@ public class MyQueue { - private Stack _sInput = new Stack(); - private Stack _sOutput = new Stack(); + private Stack _sInput = new Stack(256); + private Stack _sOutput = new Stack(256); public void Push(int x) { - while (_sOutput.TryPop(out int res)) - _sInput.Push(res); _sInput.Push(x); } public int Pop() { + if (_sOutput.Count > 0) + return _sOutput.Pop(); + while (_sInput.TryPop(out int res)) _sOutput.Push(res); return _sOutput.Pop(); @@ -21,6 +22,9 @@ public class MyQueue public int Peek() { + if (_sOutput.Count > 0) + return _sOutput.Peek(); + while (_sInput.TryPop(out int res)) _sOutput.Push(res); return _sOutput.Peek(); @@ -28,8 +32,6 @@ public class MyQueue public bool Empty() { - while (_sInput.TryPop(out int res)) - _sOutput.Push(res); - return _sOutput.Count == 0; + return _sOutput.Count == 0 && _sInput.Count == 0; } } diff --git a/232. Implement Queue using Stacks/Program.cs b/232. Implement Queue using Stacks/Program.cs index 538f626..c975fd1 100644 --- a/232. Implement Queue using Stacks/Program.cs +++ b/232. Implement Queue using Stacks/Program.cs @@ -4,6 +4,11 @@ internal class Program { static void Main(string[] args) { - Console.WriteLine("Hello, World!"); + int x = 5; + MyQueue obj = new MyQueue(); + obj.Push(x); + int param_2 = obj.Pop(); + int param_3 = obj.Peek(); + bool param_4 = obj.Empty(); } }