代码拉取完成,页面将自动刷新
using System;
using System.Collections.Generic;
namespace Algorithms.Stack
{
/// <summary>
/// Reverses the elements in a stack using recursion.
/// @author Mohit Singh. <a href="https://github.com/mohit-gogitter">mohit-gogitter</a>
/// </summary>
public class ReverseStack
{
/// <summary>
/// Recursively reverses the elements of the specified stack.
/// </summary>
/// <typeparam name="T">The type of elements in the stack.</typeparam>
/// <param name="stack">The stack to be reversed. This parameter cannot be null.</param>
/// <exception cref="ArgumentNullException">Thrown when the stack parameter is null.</exception>
public void Reverse<T>(Stack<T> stack)
{
if (stack.Count == 0)
{
return;
}
T temp = stack.Pop();
Reverse(stack);
InsertAtBottom(stack, temp);
}
private void InsertAtBottom<T>(Stack<T> stack, T value)
{
if (stack.Count == 0)
{
stack.Push(value);
}
else
{
T temp = stack.Pop();
InsertAtBottom(stack, value);
stack.Push(temp);
}
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。