18 Star 133 Fork 63

编程语言算法集 / C-Sharp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
StackBasedQueue.cs 2.44 KB
一键复制 编辑 原始数据 按行查看 历史
Gerson Jr 提交于 2024-01-08 14:18 . Switch to file-scoped namespaces (#434)
using System;
using System.Collections.Generic;
namespace DataStructures.Queue;
/// <summary>
/// Implementation of a stack based queue. FIFO style.
/// </summary>
/// <remarks>
/// Enqueue is O(1) and Dequeue is amortized O(1).
/// </remarks>
/// <typeparam name="T">Generic Type.</typeparam>
public class StackBasedQueue<T>
{
private readonly Stack<T> input;
private readonly Stack<T> output;
/// <summary>
/// Initializes a new instance of the <see cref="StackBasedQueue{T}" /> class.
/// </summary>
public StackBasedQueue()
{
input = new Stack<T>();
output = new Stack<T>();
}
/// <summary>
/// Clears the queue.
/// </summary>
public void Clear()
{
input.Clear();
output.Clear();
}
/// <summary>
/// Returns the first item in the queue and removes it from the queue.
/// </summary>
/// <exception cref="InvalidOperationException">Thrown if the queue is empty.</exception>
public T Dequeue()
{
if (input.Count == 0 && output.Count == 0)
{
throw new InvalidOperationException("The queue contains no items.");
}
if (output.Count == 0)
{
while (input.Count > 0)
{
var item = input.Pop();
output.Push(item);
}
}
return output.Pop();
}
/// <summary>
/// Returns a boolean indicating whether the queue is empty.
/// </summary>
public bool IsEmpty() => input.Count == 0 && output.Count == 0;
/// <summary>
/// Returns a boolean indicating whether the queue is full.
/// </summary>
public bool IsFull() => false;
/// <summary>
/// Returns the first item in the queue and keeps it in the queue.
/// </summary>
/// <exception cref="InvalidOperationException">Thrown if the queue is empty.</exception>
public T Peek()
{
if (input.Count == 0 && output.Count == 0)
{
throw new InvalidOperationException("The queue contains no items.");
}
if (output.Count == 0)
{
while (input.Count > 0)
{
var item = input.Pop();
output.Push(item);
}
}
return output.Peek();
}
/// <summary>
/// Adds an item at the last position in the queue.
/// </summary>
public void Enqueue(T item) => input.Push(item);
}
C#
1
https://gitee.com/TheAlgorithms/C-Sharp.git
git@gitee.com:TheAlgorithms/C-Sharp.git
TheAlgorithms
C-Sharp
C-Sharp
master

搜索帮助