1. #include "stdio.h"
    2. #include "stdlib.h"
    3. #include "io.h"
    4. #include "math.h"
    5. #include "time.h"
    6. #define OK 1
    7. #define ERROR 0
    8. #define TRUE 1
    9. #define FALSE 0
    10. #define MAXSIZE 20 /* 存储空间初始分配量 */
    11. typedef int Status;
    12. typedef int SElemType; /* SElemType类型根据实际情况而定,这里假设为int */
    13. /* 两栈共享空间结构 */
    14. typedef struct
    15. {
    16. SElemType data[MAXSIZE];
    17. int top1; /* 栈1栈顶指针 */
    18. int top2; /* 栈2栈顶指针 */
    19. }SqDoubleStack;
    20. Status visit(SElemType c)
    21. {
    22. printf("%d ",c);
    23. return OK;
    24. }
    25. /* 构造一个空栈S */
    26. Status InitStack(SqDoubleStack *S)
    27. {
    28. S->top1=-1;
    29. S->top2=MAXSIZE;
    30. return OK;
    31. }
    32. /* 把S置为空栈 */
    33. Status ClearStack(SqDoubleStack *S)
    34. {
    35. S->top1=-1;
    36. S->top2=MAXSIZE;
    37. return OK;
    38. }
    39. /* 若栈S为空栈,则返回TRUE,否则返回FALSE */
    40. Status StackEmpty(SqDoubleStack S)
    41. {
    42. if (S.top1==-1 && S.top2==MAXSIZE)
    43. return TRUE;
    44. else
    45. return FALSE;
    46. }
    47. /* 返回S的元素个数,即栈的长度 */
    48. int StackLength(SqDoubleStack S)
    49. {
    50. return (S.top1+1)+(MAXSIZE-S.top2);
    51. }
    52. /* 插入元素e为新的栈顶元素 */
    53. Status Push(SqDoubleStack *S,SElemType e,int stackNumber)
    54. {
    55. if (S->top1+1==S->top2) /* 栈已满,不能再push新元素了 */
    56. return ERROR;
    57. if (stackNumber==1) /* 栈1有元素进栈 */
    58. S->data[++S->top1]=e; /* 若是栈1则先top1+1后给数组元素赋值。 */
    59. else if (stackNumber==2) /* 栈2有元素进栈 */
    60. S->data[--S->top2]=e; /* 若是栈2则先top2-1后给数组元素赋值。 */
    61. return OK;
    62. }
    63. /* 若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR */
    64. Status Pop(SqDoubleStack *S,SElemType *e,int stackNumber)
    65. {
    66. if (stackNumber==1)
    67. {
    68. if (S->top1==-1)
    69. return ERROR; /* 说明栈1已经是空栈,溢出 */
    70. *e=S->data[S->top1--]; /* 将栈1的栈顶元素出栈 */
    71. }
    72. else if (stackNumber==2)
    73. {
    74. if (S->top2==MAXSIZE)
    75. return ERROR; /* 说明栈2已经是空栈,溢出 */
    76. *e=S->data[S->top2++]; /* 将栈2的栈顶元素出栈 */
    77. }
    78. return OK;
    79. }
    80. Status StackTraverse(SqDoubleStack S)
    81. {
    82. int i;
    83. i=0;
    84. while(i<=S.top1)
    85. {
    86. visit(S.data[i++]);
    87. }
    88. i=S.top2;
    89. while(i<MAXSIZE)
    90. {
    91. visit(S.data[i++]);
    92. }
    93. printf("\n");
    94. return OK;
    95. }
    96. int main()
    97. {
    98. int j;
    99. SqDoubleStack s;
    100. int e;
    101. if(InitStack(&s)==OK)
    102. {
    103. for(j=1;j<=5;j++)
    104. Push(&s,j,1);
    105. for(j=MAXSIZE;j>=MAXSIZE-2;j--)
    106. Push(&s,j,2);
    107. }
    108. printf("栈中元素依次为:");
    109. StackTraverse(s);
    110. printf("当前栈中元素有:%d \n",StackLength(s));
    111. Pop(&s,&e,2);
    112. printf("弹出的栈顶元素 e=%d\n",e);
    113. printf("栈空否:%d(1:空 0:否)\n",StackEmpty(s));
    114. for(j=6;j<=MAXSIZE-2;j++)
    115. Push(&s,j,1);
    116. printf("栈中元素依次为:");
    117. StackTraverse(s);
    118. printf("栈满否:%d(1:否 0:满)\n",Push(&s,100,1));
    119. ClearStack(&s);
    120. printf("清空栈后,栈空否:%d(1:空 0:否)\n",StackEmpty(s));
    121. return 0;
    122. }