泛型演示堆栈

 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 

 

转载于:https://www.cnblogs.com/leamiko/archive/2010/05/09/1731366.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值