1. public class ArrayStack<T>
    2. {
    3. int maxSize;//栈的大小
    4. T[] stack;//数组模拟栈,数据就放在该数组
    5. int top = -1;//表示栈顶,初始化为-1
    6. public ArrayStack(int maxSize)
    7. {
    8. this.maxSize = maxSize;
    9. stack = new T[this.maxSize];
    10. }
    11. //栈满
    12. public bool isFull()
    13. {
    14. return top == maxSize - 1;
    15. }
    16. //栈空
    17. public bool isEmpty()
    18. {
    19. return top == -1;
    20. }
    21. //入栈push
    22. public void push(T value)
    23. {
    24. if (isFull())
    25. {
    26. Console.WriteLine("栈满");
    27. return;
    28. }
    29. stack[++top] = value;
    30. }
    31. //出栈pop,将栈顶的数据返回
    32. public T pop()
    33. {
    34. if (isEmpty())
    35. {
    36. throw new Exception("栈空");
    37. }
    38. return stack[top--];
    39. }
    40. //显示栈的数据,遍历栈,需要从栈顶开始显示数据
    41. public void list()
    42. {
    43. if (isEmpty())
    44. {
    45. Console.WriteLine("栈空");
    46. return;
    47. }
    48. for(int i = top; i >= 0; i--)
    49. {
    50. Console.WriteLine(stack[i]);
    51. }
    52. }
    53. //返回栈头
    54. public T peak()
    55. {
    56. if (isEmpty())
    57. {
    58. return default(T);
    59. }
    60. return stack[top];
    61. }
    62. }