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 TwoStacksQueue
     7 {
     8     public class Program
     9     {
    10         public void Main(string[] args)
    11         {
    12             TwoStaksQueues twoStaksQueues = new TwoStaksQueues();
    13 
    14             twoStaksQueues.Add(1);
    15             twoStaksQueues.Add(2);
    16             twoStaksQueues.Add(3);
    17 
    18             Console.WriteLine(twoStaksQueues.Peek());
    19 
    20             twoStaksQueues.Poll();
    21 
    22             Console.WriteLine(twoStaksQueues.Peek());
    23 
    24             Console.ReadLine();
    25         }
    26         public class TwoStaksQueues
    27         {
    28             public Stack<int> stacksPush;
    29             public Stack<int> stackPop;
    30             public TwoStaksQueues()
    31             {
    32                 stackPop = new Stack<int>();
    33                 stacksPush = new Stack<int>();
    34             }
    35             public void Add(int newNumber)
    36             {
    37                 stacksPush.Push(newNumber);
    38             }
    39 
    40             public int Poll()
    41             {
    42                 if (stacksPush.Count() == 0 && stackPop.Count() == 0)
    43                 {
    44                      
    45                     throw new ArgumentOutOfRangeException("DataStacks is Empty");
    46                 }
    47                 if (stackPop.Count() == 0)
    48                 {
    49                     while (stacksPush.Count() != 0)
    50                     {
    51                         stackPop.Push(stacksPush.Pop());
    52                     }
    53                 }
    54                 return stackPop.Pop();
    55             }
    56             public int Peek()
    57             {
    58                 if (stacksPush.Count() == 0 && stackPop.Count() == 0)
    59                 {
    60                     throw new IndexOutOfRangeException("DataStacks is Empty");
    61                 }
    62                 if (stackPop.Count() == 0)
    63                 {
    64                     while (stacksPush.Count() != 0)
    65                     {
    66                         stackPop.Push(stacksPush.Pop());
    67                     }
    68                 }
    69                 return stackPop.Peek();
    70 
    71             }
    72         }
    73     }
    74 }
    View Code
  • 相关阅读:
    Nginx proxy开启cache缓存
    Nginx Server 配置
    nginx 全局配置
    Nginx 配置文件解析
    Python-Scrapy框架
    Python-shutil模块
    os模块3
    os模块
    python os模块atime ,ctime,mtime意义
    Python-正则表达式
  • 原文地址:https://www.cnblogs.com/shidengyun/p/5037478.html
Copyright © 2011-2022 走看看