Day8-9
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RootNamespace>_232._Implement_Queue_using_Stacks</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
35
232. Implement Queue using Stacks/MyQueue.cs
Normal file
35
232. Implement Queue using Stacks/MyQueue.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
namespace _232._Implement_Queue_using_Stacks;
|
||||
|
||||
public class MyQueue
|
||||
{
|
||||
private Stack<int> _sInput = new Stack<int>();
|
||||
private Stack<int> _sOutput = new Stack<int>();
|
||||
|
||||
public void Push(int x)
|
||||
{
|
||||
while(_sOutput.TryPop(out int res))
|
||||
_sInput.Push(res);
|
||||
_sInput.Push(x);
|
||||
}
|
||||
|
||||
public int Pop()
|
||||
{
|
||||
while(_sInput.TryPop(out int res))
|
||||
_sOutput.Push(res);
|
||||
return _sOutput.Pop();
|
||||
}
|
||||
|
||||
public int Peek()
|
||||
{
|
||||
while (_sInput.TryPop(out int res))
|
||||
_sOutput.Push(res);
|
||||
return _sOutput.Peek();
|
||||
}
|
||||
|
||||
public bool Empty()
|
||||
{
|
||||
while (_sInput.TryPop(out int res))
|
||||
_sOutput.Push(res);
|
||||
return _sOutput.Count == 0;
|
||||
}
|
||||
}
|
||||
9
232. Implement Queue using Stacks/Program.cs
Normal file
9
232. Implement Queue using Stacks/Program.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace _232._Implement_Queue_using_Stacks;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello, World!");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user