1. #include<stdio.h>
    2. #include<malloc.h>
    3. #define MAXLEN 10
    4. typedef int elementtype;
    5. typedef struct {
    6. elementtype element[MAXLEN];
    7. int top;
    8. } SqStack;
    9. SqStack InitStack_sq() {
    10. SqStack s;
    11. s.top = -1;
    12. return (s);
    13. }
    14. int Push_sq(SqStack *s, elementtype x) {
    15. if (s->top = MAXLEN - 1)
    16. return (0);
    17. s->top++;
    18. s->element[s->top] = x;
    19. return (1);
    20. }
    21. int Pop_sq(SqStack *s, elementtype *x) {
    22. if (s->top == -1)
    23. return (0);
    24. *x = s->element[s->top];
    25. s->top--;
    26. return (1);
    27. }
    28. int Empty_sq(SqStack *s) {
    29. return (s->top == -1);
    30. }
    31. void print(SqStack s) {
    32. int i;
    33. if (s.top != -1) {
    34. printf("输出元素的堆栈:");
    35. for (i = 0; i <= s.top; i++)
    36. printf("%d", s.element[i]);
    37. } else
    38. printf("栈是空的!!!");
    39. printf("\n");
    40. }
    41. main() {
    42. SqStack stack;
    43. int i;
    44. elementtype y;
    45. elementtype z;
    46. stack = InitStack_sq();
    47. /*if(Empty_sq(&stack)!=0)
    48. printf("\n栈是空的!");
    49. else
    50. printf("\n栈并不是空的!");*/
    51. printf("\n把9个元素堆栈:");
    52. for (i = 1; i <= 9; i++) {
    53. scanf("%d", &y);
    54. Push_sq(&stack, y);
    55. }
    56. print(stack);
    57. printf("出栈4个元素:");
    58. for (i = 6; i <= 9; i++) {
    59. Pop_sq(&stack, &z);
    60. printf("%d", z);
    61. }
    62. printf("\n");
    63. print(stack);
    64. if (Empty_sq(&stack) != 0)
    65. printf("栈是空的!");
    66. else
    67. printf("栈并不是空的!");
    68. }