加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

C#实现栈(数组,线程安全型)

(2010-11-20 00:29:36)
标签:

csharp

数组

线程安全

分类: 技术总结
    /// <summary>
    /// 泛型的栈(加上线程安全)
    /// </summary>
    /// <typeparam name="T">使用的时候才确定的类型</typeparam>
    public class Stack<T>
    {
        /// <summary>
        /// 通知的状态机
        /// </summary>
        AutoResetEvent notice=new AutoResetEvent(true);
        //初始栈大小为100
        T[] ary = new T[100];
        //顶端位置为0
        int top = 0;
        /// <summary>
        /// 向栈中压入对象
        /// </summary>
        /// <param name="x">要压入的对象</param>
        public void Push(T x)
        {
            //锁定
            Lock();
            //如果栈顶位置等于数组的长度,那么栈已经满了
            if (top == ary.Length)
            {
                //临时数组用来保存原始数组中的内容
                T[] temp = new T[ary.Length];
                //将数组中的内容放入临时数组中
                Array.Copy(ary, temp, ary.Length);
                //将原始数组扩大为原来的两倍
                ary = new T[ary.Length * 2];
                //将临时数组中的内容放入新数组中
                Array.Copy(temp, ary, temp.Length);
            }
            //设置栈顶对象
            ary[top] = x;
            //栈顶位置上升一位
            top++;
            //解锁
            UnLock();
        }
        /// <summary>
        /// 弹出栈顶的对象
        /// </summary>
        /// <returns>被弹出的对象</returns>
        public T Pop()
        {
            Lock();
            //如果栈已经空了
            if (top == 0)
            {
                //抛出栈已空的异常
                UnLock();
                throw new Exception("栈已空");
            }
            //因为是弹出,所以将栈顶位置向下降一位
            top--;
            //提取栈顶的对象
            T x = ary[top];
            //返回提取的对象
            UnLock();
            return x;
        }
        /// <summary>
        /// 提取栈顶的对象
        /// </summary>
        /// <returns>被提取的对象</returns>
        public T Peek()
        {
            //如果栈已经空了
            if (top == 0)
            {
                //抛出栈已空的异常
                throw new Exception("栈已空");
            }
            //获取栈顶的对象
            T x = ary[top - 1];
            //返回栈顶的对象
            return x;
        }
        /// <summary>
        /// 清空栈
        /// </summary>
        public void Clear()
        {
            Lock();
            //将顶部位置设为0
            top = 0;
            //将数组中的内容清空
            ary = new T[ary.Length];
            UnLock();
        }
        /// <summary>
        /// 栈大小(栈中存放的对象的个数)
        /// </summary>
        public int Count
        {
            get
            {
                Lock();
                int x = top;
                UnLock();
                return x;
            }
        }

        /// <summary>
        /// 锁定
        /// </summary>
        private void Lock()
        {
            notice.WaitOne();
        }
        /// <summary>
        /// 解锁
        /// </summary>
        private void UnLock()
        {
            notice.Set();
        }
        /// <summary>
        /// 数组大小加倍
        /// </summary>
        private void DoubleSize()
        {
            T[] temp=new T[ary.Length];
            Array.Copy(ary, temp, ary.Length);
            ary = new T[ary.Length * 2];
            Array.Copy(temp, ary, temp.Length);
        }
    }

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有