??????????????????Stack<T>???????T[] _array??????????????????????????Stack()????????????????????Щ???????????????????????????ι????????????_array??????????T[0]?????????_size??????0?????_size?????????????????д????????????????е????????????±???????????????????????????λ?á?
?????????????????Push(T item)???????????????????????????????????ж???ж???_array????????????????????????????????????????????????.NET???????????????????????????????_array?????????????????????????4????new T[4]???????????????5????????????????????????????????μ?T[] array???????????????_array??????2??????????_array??????????????μ?array?У???_array??????array????????????????????????????????????е??????????????????????????????????????Copy(_array??array)??????????????????????????.NET????е??????????????.NET???????????Array.Copy(this._array?? 0?? array?? 0?? this._size)????????????????????C++????????鸴???????????????????????????????????????

?????????????Pop()?????????????????ж?????????????????0??С?????0???????????????_size-=1??????Pop????????????λ?á????_array[_size]??????default(T)???_array[_size]??λ??????????????????????????????????????????????????????????????????????????á????????Pop()????????????
?????????????????????Stack<T>????????????????????????????????????????????£??????????в???100????????ζ???????????4->8->16->32->64->128???5?ε?????????????????????????????????????????е??.NET???Stack<T>???????????????ι????????????????Stack(int capacity)?????????capacity??????????????????????????????????????????????β???????????????????????100??????????????????new Stack<T>(100)??????????????????????????????????Stack??????????
????Queue<T>?????
????Queue?????У????????????????????????к??????????????Enqueue????????Dequeue??????????????????????????????????????Stack<T>????????????????Queue<T>???????????????????????????????????T[] _array????????????????????2???????????????????????????о?????????????????????????????Stack<T>??????????_size?????β???±????б???????????_head?±???????β_tail?±???????????????????????????е?洢Ч??????????漰????????е?????????Queue<T>???????Stack<T>????????Щ???????????????汾??????
using System;
namespace OriginalCode
{
/// <summary>
/// ????.NET??????????
/// </summary>
public class Queue<T>
{
private static T[] EMPTY_ARRAY = new T[0];
private const int _defaultCapacity = 4;
private T[] _array;
private int _head; //?λ??
private int _tail; //βλ??
private int _size; //??????????
public Queue()
{
_array = EMPTY_ARRAY;
_head = 0;
_tail = 0;
_size = 0;
}
public Queue(int capacity)
{
_array = new T[capacity];
_head = 0;
_tail = 0;
_size = 0;
}
/// <summary>
/// ??????
/// </summary>
/// <param name="item">????????</param>
public void Enqueue(T item)
{
if (_size == _array.Length)
{
//??????????????С
int capacity = _array.Length * 2;
if (capacity < _array.Length + _defaultCapacity)
{
//.NET????????????Щ????????
//??????????????Queue(int capacity)????????? capacity????=1 | 2 | 3
//????????+4???ж? ?????????????????? ???統(tǒng)capacity = 1????? *2 = 2 ????2??????????????????
//????????о?Ч???????? ?е????????????
capacity = _array.Length + _defaultCapacity;
}
//?????????????????????
T[] array = new T[capacity];
if (_size > 0)
{
//????????·?????????????? ??????????е????? ?????_head???????_tail
//???????_array[_head]??_array[_size-1] ????? ??????array[0]...[_size - _head - 1]
ArrayCopy(_array?? array?? 0?? _head?? _size - _head);
//???????_array[0]??_array[_head-1] ????? ??????array[_size - _head]...[_size - 1]
ArrayCopy(_array?? array?? _size - _head?? 0?? _head);
}
_array = array; //?????????????????
_head = 0; //??????λ??????0
_tail = _size; //?????βλ??????_size
}
_array[_tail] = item;
_tail = (_tail + 1) % _array.Length;
_size += 1;
}
/// <summary>
/// ???????
/// </summary>
/// <returns>???????</returns>
public T Dequeue()
{
if (_size == 0)
{
throw new Exception("?????????? ??????г??????");
}
T result = _array[_head];
_array[_head] = default(T);
_head = (_head + 1) % _array.Length;
_size -= 1;
return result;
}
/// <summary>
/// ????????????????????(?????????????????????????.NET???????C++????????Ч?????)
/// </summary>
/// <param name="oldArray">??????</param>
/// <param name="newArray">??????</param>
/// <param name="newArrayBeginIndex">??????????±?</param>
/// <param name="oldArrayBeginIndex">??????????±?</param>
/// <param name="copyCount">???????</param>
private void ArrayCopy(T[] oldArray?? T[] newArray?? int newArrayBeginIndex?? int oldArrayBeginIndex?? int copyCount)
{
for (int i = oldArrayBeginIndex?? j = newArrayBeginIndex; i < oldArrayBeginIndex + copyCount; i++??j++)
{
newArray[j] = oldArray[i];
}
}
}
}