zoukankan      html  css  js  c++  java
  • C#=>递归反转栈

    原理,递归反转栈
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Threading.Tasks;
     5 
     6 namespace StacksReverse
     7 {
     8     public class Program
     9     {
    10         public void Main(string[] args)
    11         {
    12             StacksReverses stacksReverses = new StacksReverses();
    13             Stack<int> stack = new Stack<int>();
    14 
    15             stack.Push(1);
    16             stack.Push(2);
    17             stack.Push(3);
    18             stack.Push(4);
    19             stack.Push(5);
    20             stack.Push(6);
    21             stack.Push(7);
    22 
    23             stacksReverses.reverse(stack);
    24 
    25             Console.ReadLine();
    26         }
    27 
    28         public class StacksReverses
    29         {
    30             public int GetAndRemoveLastElement(Stack<int> stack)
    31             {
    32                 int result = stack.Pop();
    33                 if (stack.Count() == 0) return result;
    34                 int last = GetAndRemoveLastElement(stack);
    35                 stack.Push(result);
    36                 return last;
    37             }
    38             public void reverse(Stack<int> stack)
    39             {
    40                 if (stack.Count() == 0) return;
    41                 int i = GetAndRemoveLastElement(stack);
    42                 reverse(stack);
    43                 stack.Push(i);
    44             }
    45         }
    46     }
    47 }
    View Code
  • 相关阅读:
    linux 硬件信息
    docker note
    Shell cmd set note
    mysql management note
    scp noneed passwd
    update kernel
    数据包处理过程
    tcp/ip分片
    sockopt note
    Python note
  • 原文地址:https://www.cnblogs.com/shidengyun/p/5037621.html
Copyright © 2011-2022 走看看