代码拉取完成,页面将自动刷新
同步操作将从 编程语言算法集/C-Sharp 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
using System;
using System.Collections.Generic;
namespace DataStructures.Stack
{
/// <summary>
/// Implementation of a list based stack. FILO style.
/// </summary>
/// <typeparam name="T">Generic Type.</typeparam>
public class ListBasedStack<T>
{
/// <summary>
/// <see cref="List{T}" /> based stack.
/// </summary>
private readonly LinkedList<T> stack;
/// <summary>
/// Initializes a new instance of the <see cref="ListBasedStack{T}" /> class.
/// </summary>
public ListBasedStack() => stack = new LinkedList<T>();
/// <summary>
/// Initializes a new instance of the <see cref="ListBasedStack{T}" /> class.
/// </summary>
/// <param name="item">Item to push onto the <see cref="ListBasedStack{T}" />.</param>
public ListBasedStack(T item)
: this() => Push(item);
/// <summary>
/// Initializes a new instance of the <see cref="ListBasedStack{T}" /> class.
/// </summary>
/// <param name="items">Items to push onto the <see cref="ListBasedStack{T}" />.</param>
public ListBasedStack(IEnumerable<T> items)
: this()
{
foreach (var item in items)
{
Push(item);
}
}
/// <summary>
/// Gets the number of elements on the <see cref="ListBasedStack{T}" />.
/// </summary>
public int Count => stack.Count;
/// <summary>
/// Removes all items from the <see cref="ListBasedStack{T}" />.
/// </summary>
public void Clear() => stack.Clear();
/// <summary>
/// Determines whether an element is in the <see cref="ListBasedStack{T}" />.
/// </summary>
/// <param name="item">The item to locate in the <see cref="ListBasedStack{T}" />.</param>
/// <returns>True, if the item is in the stack.</returns>
public bool Contains(T item) => stack.Contains(item);
/// <summary>
/// Returns the item at the top of the <see cref="ListBasedStack{T}" /> without removing it.
/// </summary>
/// <returns>The item at the top of the <see cref="ListBasedStack{T}" />.</returns>
public T Peek()
{
if (stack.First is null)
{
throw new InvalidOperationException("Stack is empty");
}
return stack.First.Value;
}
/// <summary>
/// Removes and returns the item at the top of the <see cref="ListBasedStack{T}" />.
/// </summary>
/// <returns>The item removed from the top of the <see cref="ListBasedStack{T}" />.</returns>
public T Pop()
{
if (stack.First is null)
{
throw new InvalidOperationException("Stack is empty");
}
var item = stack.First.Value;
stack.RemoveFirst();
return item;
}
/// <summary>
/// Inserts an item at the top of the <see cref="ListBasedStack{T}" />.
/// </summary>
/// <param name="item">The item to push onto the <see cref="ListBasedStack{T}" />.</param>
public void Push(T item) => stack.AddFirst(item);
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。