1.栈的顺序存储

    1. #include<stdio.h>
    2. #include<stdlib.h>
    3. #define MaxSize 100
    4. typedef int ElementType;
    5. typedef struct SNode *Stack;
    6. struct SNode
    7. {
    8. ElementType Data[MaxSize];
    9. int Top;
    10. };
    11. Stack S;
    12. Stack CreateStack();
    13. bool IsFull(Stack S);
    14. bool IsEmpty(Stack S);
    15. void Push(Stack S,ElementType item);
    16. ElementType Pop(Stack S);
    17. int main(void)
    18. {
    19. S = CreateStack();
    20. printf("5入栈\n");
    21. Push(S,5);
    22. printf("7入栈\n");
    23. Push(S,7);
    24. printf("66入栈\n");
    25. Push(S,66);
    26. printf("44入栈\n");
    27. Push(S,44);
    28. printf("%d出栈\n",Pop(S));
    29. printf("%d出栈\n",Pop(S));
    30. printf("%d出栈\n",Pop(S));
    31. printf("%d出栈\n",Pop(S));
    32. if(IsEmpty(S))
    33. {
    34. printf("堆栈空");
    35. }
    36. return 0;
    37. }
    38. Stack CreateStack()
    39. {
    40. S = (Stack)malloc(sizeof(struct SNode));
    41. S->Top = -1;
    42. return S;
    43. }
    44. bool IsFull(Stack S)
    45. {
    46. return (S->Top == MaxSize-1);
    47. }
    48. bool IsEmpty(Stack S)
    49. {
    50. return (S->Top == -1);
    51. }
    52. void Push(Stack S,ElementType item)
    53. {
    54. if(IsFull(S))
    55. {
    56. printf("堆栈满");
    57. return;
    58. }else
    59. {
    60. S->Top++;
    61. S->Data[S->Top] = item;
    62. return;
    63. }
    64. }
    65. ElementType Pop(Stack S)
    66. {
    67. if(IsEmpty(S))
    68. {
    69. printf("堆栈空");
    70. return -1;
    71. }
    72. else
    73. {
    74. ElementType val = S->Data[S->Top];
    75. S->Top--;
    76. return val;
    77. }
    78. }

    2.栈的链式存储

    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. struct SNode
    4. {
    5. int Data;
    6. struct SNode *Next;
    7. };
    8. typedef struct SNode *Stack;
    9. Stack S;
    10. Stack CreateStack();
    11. bool IsEmpty(Stack S);
    12. void Push(Stack S,int item);
    13. int Pop( Stack S );
    14. int main(void)
    15. {
    16. S = CreateStack();
    17. printf("5入栈\n");
    18. Push(S,5);
    19. printf("7入栈\n");
    20. Push(S,7);
    21. printf("66入栈\n");
    22. Push(S,66);
    23. printf("44入栈\n");
    24. Push(S,44);
    25. printf("%d出栈\n",Pop(S));
    26. printf("%d出栈\n",Pop(S));
    27. printf("%d出栈\n",Pop(S));
    28. printf("%d出栈\n",Pop(S));
    29. if(IsEmpty(S))
    30. {
    31. printf("堆栈空");
    32. }
    33. return 0;
    34. }
    35. Stack CreateStack()
    36. {
    37. Stack S;
    38. S = (Stack)malloc(sizeof(struct SNode));
    39. S->Next = NULL;
    40. return S;
    41. }
    42. bool IsEmpty(Stack S)
    43. {
    44. return (S->Next == NULL);
    45. }
    46. void Push(Stack S,int item)
    47. {
    48. Stack tmp;
    49. tmp = (Stack)malloc(sizeof(struct SNode));
    50. tmp->Data = item;
    51. tmp->Next = S->Next;
    52. S->Next = tmp;
    53. }
    54. int Pop( Stack S )
    55. {
    56. Stack Firstcell;
    57. int Toptmp;
    58. if ( IsEmpty(S) )
    59. {
    60. printf("栈空\n");
    61. return -1;
    62. }
    63. Firstcell = S->Next;
    64. S->Next = Firstcell->Next;
    65. Toptmp = Firstcell->Data;
    66. free(Firstcell);
    67. return Toptmp;
    68. }