zoukankan      html  css  js  c++  java
  • 泛型演示堆栈

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace ConsoleApplication1
     7 {
     8     class MyStack<T>
     9     {
    10         T[] StackArray;
    11         int StackPointer = 0;
    12 
    13         const int MaxStack = 3;
    14         bool IsStackFull
    15         {
    16             get 
    17             {
    18                 return StackPointer >= MaxStack;
    19             }
    20         }
    21         bool IsStackEmpty
    22         {
    23             get
    24             {
    25                 return StackPointer <= 0;
    26             }
    27         }
    28         public void Push(T x)
    29         {
    30             if (!IsStackFull)
    31                 StackArray[StackPointer++= x;
    32         }
    33         public T Pop()
    34         {
    35             return (!IsStackEmpty)
    36                 ? StackArray[--StackPointer]
    37                 : StackArray[0];
    38         }
    39 
    40         public MyStack()
    41         {
    42             StackArray = new T[MaxStack]; // 创建栈
    43         }
    44         public void Print()
    45         {
    46             for (int i = StackPointer - 1; i >= 0; i--)
    47             {
    48                 Console.WriteLine("Value:{0}", StackArray[i]);
    49             }
    50         }
    51 
    52     }
    53     class Program
    54     {
    55         static void Main(string[] args)
    56         {
    57             var stackInt = new MyStack<int>();
    58             var stackString=new MyStack<string>();
    59 
    60             stackInt.Push(3);
    61             stackInt.Push(5);
    62             stackInt.Push(7);
    63             stackInt.Push(8);
    64             stackInt.Print();
    65 
    66             stackString.Push("Great!");
    67             stackString.Push("Hi there");
    68 
    69             stackString.Print();
    70             Console.ReadKey();
    71         }
    72     }
    73 }
    74 
  • 相关阅读:
    springboot中多端口启动(这里也适用于https既443端口)
    Gson的fromJson()方法(从Json相关对象到Java实体或转换成List集合)
    [C++] STL源码中学到的 Traits 编程技法的应用
    【解决问题】UNIAPP、VUE 中DATA 数组更新后页面不同步动态渲染更新
    selenium操作chrome时的一些配置
    在react项目中使用fetch 和 JWT进行权限验证(转)
    滚动条的设置(样式包括宽度,颜色等)zhuan
    css3 box-shadow阴影(外阴影与外发光)图示讲解(zhuan)
    js 截断字符串 超过几个字加...
    create-react-app 打包部署
  • 原文地址:https://www.cnblogs.com/leamiko/p/1731366.html
Copyright © 2011-2022 走看看