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 public class MyQueue
{ {
private Stack<int> _sInput = new Stack<int>(); private Stack<int> _sInput = new Stack<int>(256);
private Stack<int> _sOutput = new Stack<int>(); private Stack<int> _sOutput = new Stack<int>(256);
public void Push(int x) public void Push(int x)
{ {
while (_sOutput.TryPop(out int res))
_sInput.Push(res);
_sInput.Push(x); _sInput.Push(x);
} }
public int Pop() public int Pop()
{ {
if (_sOutput.Count > 0)
return _sOutput.Pop();
while (_sInput.TryPop(out int res)) while (_sInput.TryPop(out int res))
_sOutput.Push(res); _sOutput.Push(res);
return _sOutput.Pop(); return _sOutput.Pop();
@@ -21,6 +22,9 @@ public class MyQueue
public int Peek() public int Peek()
{ {
if (_sOutput.Count > 0)
return _sOutput.Peek();
while (_sInput.TryPop(out int res)) while (_sInput.TryPop(out int res))
_sOutput.Push(res); _sOutput.Push(res);
return _sOutput.Peek(); return _sOutput.Peek();
@@ -28,8 +32,6 @@ public class MyQueue
public bool Empty() public bool Empty()
{ {
while (_sInput.TryPop(out int res)) return _sOutput.Count == 0 && _sInput.Count == 0;
_sOutput.Push(res);
return _sOutput.Count == 0;
} }
} }

View File

@@ -4,6 +4,11 @@ internal class Program
{ {
static void Main(string[] args) 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();
} }
} }