对于栈的操作很像顺序表操作方法,同样使用了泛型类和接口来实现,代码如下:

 

  1using System;
  2using System.Collections.Generic;
  3using System.Text;
  4
  5namespace 栈和队列
  6{
  7    class Program
  8    {
  9        public interface IStack<T>
 10        
 11           //求栈的长度
 12             int GetLength();
 13            //判断是否为空
 14            bool IsEmpty(); 
 15            //判断是否已满
 16            bool IsFull();
 17            //清空操作
 18            void Clear();
 19            //入栈
 20            void Push(T item);
 21            //出栈
 22            T Pop();
 23            //取栈顶元素
 24            T GetTop();
 25
 26        }

 27        public class SeqStack<T> : IStack<T>
 28        
 29           //栈的容量
 30            private int maxsize;
 31           //存储栈中的数据
 32            private T[] data;
 33           //指定栈的栈顶
 34            private int top;
 35
 36            //索引器
 37            public T this[int index]
 38            {
 39                get
 40                
 41                   return data[index];
 42                }

 43                set
 44                {
 45                    data[index] = value;
 46                }

 47            }

 48
 49            //容量属性
 50            public int Maxsize
 51            {
 52                get
 53                {
 54                    return maxsize;
 55                }

 56                set
 57                {
 58                    maxsize = value;
 59                }

 60            }

 61
 62            //栈顶属性
 63            public int Top
 64            {
 65                get
 66                {
 67                    return top;
 68                }

 69            }

 70
 71            //构造器
 72            public SeqStack(int size)
 73            {
 74                data = new T[size];
 75                maxsize = size;
 76                top = -1;
 77            }

 78
 79            //求栈的长度
 80            public int GetLength()
 81            {
 82                return top + 1;
 83            }

 84
 85            //清空栈
 86            public void Clear()
 87            {
 88                top = -1;
 89            }

 90
 91            //判断是否为空
 92            public bool IsEmpty()
 93            {
 94                if (top == -1)
 95                {
 96                    return true;
 97                }

 98                else
 99                {
100                    return false;
101                }

102            }

103
104            //判断是否以满
105            public bool IsFull()
106            {
107                if (top == maxsize - 1)
108                {
109                    return true;
110                }

111                else
112                {
113                    return false;
114                }

115            }

116
117            //入栈
118            public void Push(T item)
119            {
120                if (IsFull())
121                {
122                    Console.WriteLine("栈满啦,要清空啦!");
123                    return;
124                }

125                data[++top] = item;
126            }

127            //出栈
128            public T Pop()
129            
130               T tmp=default(T);
131               if (IsEmpty())
132               {
133                   Console.WriteLine("栈已空!");
134                   return tmp;
135               }

136               tmp = data[top];
137               --top;
138               return tmp;
139            }

140            //获取栈顶数据元素
141            public T GetTop()
142            {
143                if (IsEmpty())
144                {
145                    Console.WriteLine("表已空!");
146                    return default(T);
147                }

148                return data[top];
149            }

150
151        }

152
153        static void Main(string[] args)
154        {
155            SeqStack<int> stack = new SeqStack<int>(10);
156            int[] str = new int[] {1,2,3,4,5,6,7,8,9};
157            for (int i = 0; i < str.Length; i++)
158            {
159                stack.Push(str[i]);
160                
161            }

162            //显示出栈值
163            Console.WriteLine(stack.Pop());
164            //显示栈顶值
165            Console.WriteLine(stack.GetTop());
166            
167
168        }

169    }

170}

171

 

 

 

posted on 2009-03-07 15:16  甲乙丙丁  阅读(261)  评论(0编辑  收藏  举报