基于可变数组的Stack栈


using System;
using UnityEngine;
/// <summary>
/// 栈
/// </summary>
public class MyStack<T>
{
    /// <summary>
    /// 栈的实现还是使用数组的方式存储
    /// </summary>
    public T[] content { get; set; }
    public int Count {
         get;
        private set;
    }
    public int capacity { get; set; }
    /// <summary>
    /// 初始化 默认深度
    /// </summary>
    public MyStack(int capacity)
    {
        content = new T[capacity];
        this.capacity = capacity;
        this.Count =0;
    }
    /// <summary>
    /// 压栈
    /// </summary>
    /// <param name="t"></param>
    public virtual void Push(T t)
    {
        //如果数组满了,则扩容(翻倍扩容)
        if (Count+1 > content.Length)
        {
            //扩容次数为2的指数级
            this.capacity *= 2;
            T[] newArray = new T[capacity];
            //将原内容拷贝到新的数组中 (扩容次数 2的指数级扩容,减少拷贝带来的时间消耗)
            Array.Copy(content, 0, newArray, 0, Count);
            content = newArray;
        }
        //写入数组
        content[Count++] = t;       
    }
    /// <summary>
    /// 取下一个出栈值,但不删除 (偷看下~)
    /// </summary>
    /// <returns></returns>
    public virtual T Peek()
    {
        if (Count <= 0)
            return default(T);
        return content[Count-1];
    }
    /// <summary>
    /// 出栈
    /// </summary>
    /// <returns></returns>
    public virtual T Pop()
    {
        if (Count <= 0)
            return default(T);
        T t = content[Count-1];
        content[Count-1] = default(T);
        Count--;

        //如果top小于容量的1/4,容量缩小为原来的1/2
        if (Count / capacity < 1 / 4)
        {
            //扩容次数为2的指数级
            this.capacity /= 2;
            T[] newArray = new T[capacity];
            //将原内容拷贝到新的数组中 (扩容次数 2的指数级扩容,减少拷贝带来的时间消耗)
            Array.Copy(content, 0, newArray, 0, Count);
            content = newArray;
        }
        return t;
    }

    public void Clear()
    {
        if (Count > 0)
        {
            Count =0;
            capacity = 8;
            content= new T[capacity];
        }
    }

    //实现该方法是为了 暂不实现 ICollection -
    public void ShowAll()
    {
        for (int i = Count-1; i >= 0; i--)
        {
            Debug.Log($" {content[i]}");
        }
    }
}
/// <summary>
/// 异步栈
/// </summary>
public class MySyncStack<T> : MyStack<T>
{
    private MyStack<T> myStack;
    private object lookObj = new object();

    public MySyncStack(int capacity) : base(capacity)
    {

    }

    public override void Push(T value)
    {
        lock (lookObj)
        {
            myStack.Push(value);
        }
    }

    public override T Pop()
    {
        lock (lookObj)
        {
            return myStack.Pop();
        }
    }

    public override T Peek()
    {
        lock (lookObj)
        {
            return myStack.Peek();
        }
    }
}
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值