225. Implement Stack using Queues
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<RootNamespace>_225._Implement_Stack_using_Queues</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
50
225. Implement Stack using Queues/MyStack.cs
Normal file
50
225. Implement Stack using Queues/MyStack.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
namespace _225._Implement_Stack_using_Queues;
|
||||
|
||||
public class MyStack
|
||||
{
|
||||
private Queue<int> _queue = new Queue<int>(256);
|
||||
private int? _lastNum = null;
|
||||
|
||||
public MyStack()
|
||||
{
|
||||
}
|
||||
|
||||
public void Push(int x)
|
||||
{
|
||||
if (_lastNum == null)
|
||||
{
|
||||
_lastNum = x;
|
||||
return;
|
||||
}
|
||||
|
||||
int count = _queue.Count;
|
||||
_queue.Enqueue(_lastNum.Value);
|
||||
_lastNum = x;
|
||||
while (count-- > 0)
|
||||
_queue.Enqueue(_queue.Dequeue());
|
||||
}
|
||||
|
||||
public int Pop()
|
||||
{
|
||||
if (_lastNum != null)
|
||||
{
|
||||
int res = _lastNum.Value;
|
||||
_lastNum = null;
|
||||
return res;
|
||||
}
|
||||
|
||||
return _queue.Dequeue();
|
||||
}
|
||||
|
||||
public int Top()
|
||||
{
|
||||
if (_lastNum != null)
|
||||
return _lastNum.Value;
|
||||
return _queue.Peek();
|
||||
}
|
||||
|
||||
public bool Empty()
|
||||
{
|
||||
return _lastNum == null && _queue.Count == 0;
|
||||
}
|
||||
}
|
||||
3
225. Implement Stack using Queues/Program.cs
Normal file
3
225. Implement Stack using Queues/Program.cs
Normal file
@@ -0,0 +1,3 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
|
||||
Console.WriteLine("Hello, World!");
|
||||
@@ -163,6 +163,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "8. String to Integer (atoi)
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "15. 3Sum", "15. 3Sum\15. 3Sum.csproj", "{4FAFB0B1-A240-44B9-8305-3CA7FED8AE3C}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "225. Implement Stack using Queues", "225. Implement Stack using Queues\225. Implement Stack using Queues.csproj", "{4DF0467F-C3C9-4F92-9EE7-D21AD30F559D}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -489,6 +491,10 @@ Global
|
||||
{4FAFB0B1-A240-44B9-8305-3CA7FED8AE3C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{4FAFB0B1-A240-44B9-8305-3CA7FED8AE3C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{4FAFB0B1-A240-44B9-8305-3CA7FED8AE3C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{4DF0467F-C3C9-4F92-9EE7-D21AD30F559D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{4DF0467F-C3C9-4F92-9EE7-D21AD30F559D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{4DF0467F-C3C9-4F92-9EE7-D21AD30F559D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{4DF0467F-C3C9-4F92-9EE7-D21AD30F559D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
Reference in New Issue
Block a user