一、栈的定义
栈(Stack)是一种特殊的线性表,仅能在线性表的一端操作,栈顶允许操作,栈底不允许操作。
栈中允许进行插入、删除操作的一端叫做栈顶(Top),另一端则叫做栈底(Bottom)。当栈中没有元素时,称之为空栈。栈的插入运算通常称为压栈、进栈或入栈(Push),栈的删除运算通常称为弹栈或出栈(Pop)。
示意图如下:
二、栈与线性表的区别与联系
(1)栈是特殊的线性表
(2)栈的插入与删除操作只能在栈顶进行,而线性表的插入和删除可在线性表中的任意位置进行
三、栈的特性:后进先出(如上图所示)
四、栈的基本操作
包括创建栈、销毁栈、出栈、入栈、获取栈顶元素、获取包含元素、获取栈的大小、清空栈。
例子:(在Unity中测试)
using System.Collections;
using UnityEngine;
public class Stack_Demo : MonoBehaviour
{
private object str;
private void Start()
{
//创建栈
Stack mStack = new Stack();
//入栈
mStack.Push("1");
mStack.Push("2");
mStack.Push("3");
mStack.Push("4");
mStack.Push("5");
mStack.Push("6");
mStack.Push("7");
mStack.Push("8");
mStack.Push("9");
//获取栈的大小
Debug.LogFormat("----------栈中元素的数量----->{0}", mStack.Count);
//出栈
str = mStack.Pop();
Debug.LogFormat("栈顶的内容为:---->{0}", str);
str = mStack.Peek();
Debug.LogFormat("栈顶的内容为:---->{0}", str);
str = mStack.Pop();
Debug.LogFormat("栈顶的内容为:---->{0}", str);
Debug.LogFormat("----------栈中元素的数量----->{0}", mStack.Count);
//栈中包含对象查询
Debug.LogFormat("栈中包含查询----->{0}", mStack.Contains("3"));
//清空栈
mStack.Clear();
//再次获取栈的大小
if (null != mStack)
{
Debug.LogFormat("----------栈中元素的数量----->{0}", mStack.Count);
}
}
}
Log
从上面的Log发现 pop()和 peek()都是出栈,但是也有不同
二者区别:
peek():只返回栈顶元素
pop():返回栈顶元素,并删除栈顶元素
四、栈在游戏开发中的基本应用
栈的特性是先进后出,实际开发中我们一般在UI架构中会用到栈,打开UI面板时压入栈中,在UI处理返回上一级UI面板时关闭当前UI(出栈),显示上一个UI用到了栈。还有在对象池也会用到栈!例:PoolManager
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public sealed class PoolManager
{
public static readonly PoolManager Instance = new PoolManager();
//缓存集合
private Dictionary<string, Stack<GameObject>> poolDic;
//缓存池对象
private Transform poolRoot;
public PoolManager()
{
poolDic = new Dictionary<string, Stack<GameObject>>();
GameObject go = new GameObject("Pool Root");
GameObject.DontDestroyOnLoad(go);
go.SetActive(false);
poolRoot = go.transform;
}
/// <summary>
/// 添加到缓存池
/// </summary>
public void AddToPool(string prefab, GameObject obj)
{
obj.transform.parent = poolRoot;
Stack<GameObject> list;
if (!poolDic.TryGetValue(prefab, out list))
{
list = new Stack<GameObject>();
poolDic.Add(prefab, list);
}
list.Push(obj);
}
/// <summary>
/// 从缓存池中获得
/// </summary>
public GameObject GetAtPool(string prefab, Transform parent)
{
GameObject obj = null;
Stack<GameObject> list;
if (poolDic.TryGetValue(prefab, out list))
{
if (list.Count > 0)
{
obj = list.Pop();
}
}
if (obj == null)
{
Object pObj = Resources.Load(prefab);
if (pObj != null)
{
obj = (GameObject)GameObject.Instantiate(pObj);
}
}
if (obj != null)
{
if (parent != null)
{
Transform tf = obj.transform;
tf.parent = parent;
tf.localPosition = Vector3.zero;
tf.localRotation = Quaternion.identity;
tf.localScale = Vector3.one;
}
obj.SetActive(true);
}
return obj;
}
/// <summary>
/// 从缓存池中获得
/// </summary>
public GameObject GetAtPool(string package, string prefab, Transform parent)
{
GameObject obj = null;
Stack<GameObject> list;
if (poolDic.TryGetValue(prefab, out list))
{
if (list.Count > 0)
{
obj = list.Pop();
}
}
if (obj == null)
{
AssetBundle assetBundle = ResourceManager.instance.GetResource<AssetBundle>(package);
if (assetBundle != null)
{
Object pObj = assetBundle.LoadAsset(prefab);
if (pObj != null)
{
obj = (GameObject)GameObject.Instantiate(pObj);
}
}
}
if (obj != null)
{
if (parent != null)
{
Transform tf = obj.transform;
tf.parent = parent;
tf.localPosition = Vector3.zero;
tf.localRotation = Quaternion.identity;
tf.localScale = Vector3.one;
}
obj.SetActive(true);
}
return obj;
}
/// <summary>
/// 实例化任何预设
/// </summary>
public GameObject GetGameObject(GameObject prefab, Transform parent)
{
GameObject obj = null;
if (prefab != null)
{
obj = GameObject.Instantiate(prefab);
}
if (obj != null)
{
if (parent != null)
{
Transform tf = obj.transform;
tf.parent = parent;
tf.localPosition = Vector3.zero;
tf.localRotation = Quaternion.identity;
tf.localScale = Vector3.one;
}
obj.SetActive(true);
}
return obj;
}
/// <summary>
/// 清理缓存
/// </summary>
public void Clear(string prefab)
{
Stack<GameObject> list;
if (poolDic.TryGetValue(prefab, out list))
{
while (list.Count > 0)
{
GameObject.DestroyImmediate(list.Pop());
}
poolDic.Remove(prefab);
}
}
/// <summary>
/// 清理缓存
/// </summary>
public void Clear()
{
foreach (Stack<GameObject> item in poolDic.Values)
{
while (item.Count > 0)
{
GameObject.DestroyImmediate(item.Pop());
}
}
poolDic.Clear();
}
/// <summary>
/// 获得缓存根节点
/// </summary>
public Transform PoolRoot
{
get { return poolRoot; }
}
/// <summary>
/// 检测缓存池
/// </summary>
private void CheckPool()
{
}
public void PoolGo(GameObject obj)
{
AddToPool(obj.name, obj);
}
public GameObject GetGo(GameObject go, Transform parent)
{
GameObject instance = null;
if (go != null)
{
Stack<GameObject> objects;
if (poolDic.TryGetValue(string.Format("{0}(Clone)", go.name), out objects) && objects.Count > 0)
{
instance = objects.Pop();
}
else
{
instance = Object.Instantiate(go) as GameObject;
if (!instance.activeSelf)
instance.SetActive(true);
}
if (parent != null)
{
instance.transform.parent = parent;
instance.transform.localScale = Vector3.one;
}
}
return instance;
}
public GameObject GetGo(GameObject go)
{
return GetGo(go, null);
}
}