c#集合之五堆栈

奇怪,刚写好的blog,点发表就不见了,只好重新copy一下.

说道堆栈,其特点是先进后出。抽象出来理解这是这样子的,将最先进栈的元素放到栈底,第二个元素放在第一个元素上面,出栈的时候,先将上面的元素取出。因此堆栈中的方法有push,进栈,pop出栈,以及peek,获取栈中最上面的一个元素。

系统中栈的容量默认为10,当超过10时,系统会将栈的容量在原来的基础上增加10,这个与其他的集合类差不多。

下面是代码示例,包含了泛型和非泛型类的用法。

 1 using  System;
 2 using  System.Collections.Generic;
 3 using  System.Text;
 4
 5 using  System.Collections;
 6
 7 namespace  Practice
 8 {
 9      public   class  Cs_Stack
10      {
11          private  Stack myStack  =   null ;
12          public  Cs_Stack()
13          {
14             myStack  =   new  Stack();
15             myStack.Push( " a " );
16             myStack.Push( " b " );
17             myStack.Push( " c " );
18         }

19
20          public  Cs_Stack(ICollection collection)
21          {
22             myStack  =   new  Stack(collection);
23         }

24
25          public   void  PrintStack()
26          {
27             Console.Write( " the element of stack is :/n " );
28              foreach  ( object  obj  in  myStack)
29              {
30                 Console.Write( " {0}/n " ,obj);
31             }

32             Console.Write( " ------------/nthe element on the first of stack is:/n " );
33             Console.Write( " {0}/n " ,myStack.Peek());
34             Console.Write( " ------------/nthe element on the first pop of stack is:/n " );
35             Console.Write( " {0}/n " ,myStack.Pop());
36             Console.Write( " ------------/nthe element on the first of stack is:/n " );
37             Console.Write( " {0}/n " , myStack.Peek());
38             Console.ReadLine();
39         }

40
41          public   void  PrintStack < T > (Stack < T >  _myStack)
42          {
43             Console.Write( " the element of stack is :/n " );
44              foreach  ( object  obj  in  _myStack)
45              {
46                 Console.Write( " {0}/n " , obj);
47             }

48             Console.Write( " ------------/nthe element on the first of stack is:/n " );
49             Console.Write( " {0}/n " , _myStack.Peek());
50             Console.Write( " ------------/nthe element on the first pop of stack is:/n " );
51             Console.Write( " {0}/n " , _myStack.Pop());
52             Console.Write( " ------------/nthe element on the first of stack is:/n " );
53             Console.Write( " {0}/n " , _myStack.Peek());
54             Console.ReadLine();
55         }

56     }

57 }

58 下面的代码是调用改类以及其中的方法:
59          private   void  TestStack()
60          {
61             Cs_Stack myCs1  =   new  Cs_Stack();
62             myCs1.PrintStack();
63
64             ICollection collecton  =   new   object []  " a " " b " 1  } ;
65             Cs_Stack myCs2  =   new  Cs_Stack(collecton);
66             myCs2.PrintStack();
67
68             Stack < int >  myStack1  =   new  Stack < int > ();
69             myStack1.Push( 1 );
70             myStack1.Push( 2 );
71             myStack1.Push( 3 );
72
73             myCs1.PrintStack < int > (myStack1);
74
75             Stack < string >  myStack2  =   new  Stack < string > ();
76             myStack2.Push( " a " );
77             myStack2.Push( " b " );
78             myStack2.Push( " c " );
79
80             myCs1.PrintStack < string > (myStack2);
81         }

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值