This commit is contained in:
Пытков Роман
2024-01-30 23:19:36 +03:00
parent 75d2db5b75
commit 8b40248caf
2 changed files with 15 additions and 8 deletions

View File

@@ -2,18 +2,19 @@
public class MyQueue
{
private Stack<int> _sInput = new Stack<int>();
private Stack<int> _sOutput = new Stack<int>();
private Stack<int> _sInput = new Stack<int>(256);
private Stack<int> _sOutput = new Stack<int>(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;
}
}