首页 > 代码库 > 05 1 栈与队列 栈的实现与Stack类

05 1 栈与队列 栈的实现与Stack类

技术分享

 技术分享

技术分享
 1 /// <summary>
 2 /// 自定义栈
 3 /// </summary>
 4 class CStack {
 5 
 6     /// <summary>
 7     /// 存储数据的数组
 8     /// </summary>
 9     private ArrayList m_arrayList;
10     /// <summary>
11     /// 栈顶索引
12     /// </summary>
13     private int m_top;
14 
15     public CStack() {
16         m_arrayList = new ArrayList();
17         m_top = -1;
18     }
19 
20     /// <summary>
21     /// 栈的元素数量
22     /// </summary>
23     public int Count {
24         get {
25             return m_arrayList.Count;
26         }
27     }
28 
29     /// <summary>
30     /// 入栈
31     /// </summary>
32     public void Push(object item) {
33         m_arrayList.Add(item);
34         m_top++;
35     }
36 
37     /// <summary>
38     /// 出栈
39     /// </summary>
40     public object Pop() {
41         object obj = m_arrayList[m_top];
42         m_arrayList.RemoveAt(m_top);
43         m_top--;
44         return obj;
45     }
46 
47     /// <summary>
48     /// 取栈顶
49     /// </summary>
50     public object Peek() {
51         return m_arrayList[m_top];
52     }
53 
54     /// <summary>
55     /// 清空
56     /// </summary>
57     public void Clear() {
58         m_arrayList.Clear();
59         m_top = -1;
60     }
61 }
CStack

 

05 1 栈与队列 栈的实现与Stack类