1 Star 0 Fork 0

jobily/TheAlgorithms-C-Sharp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ReverseStack.cs 1.28 KB
一键复制 编辑 原始数据 按行查看 历史
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);
}
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hubo/the-algorithms-c-sharp.git
git@gitee.com:hubo/the-algorithms-c-sharp.git
hubo
the-algorithms-c-sharp
TheAlgorithms-C-Sharp
master

搜索帮助