• Source
    1. #include <stdio.h>
    2. #include <limits.h>
    3.  
    4. #define ARRAY_SIZE 100
    5. #define LEFT_STACK 0
    6. #define RIGHT_STACK 1
    7.  
    8. struct st {
    9. int array[ARRAY_SIZE];
    10. int top1, top2;
    11. } st;
    12.  
    13. void initialize() {
    14. st.top1 = -1;
    15. st.top2 = ARRAY_SIZE;
    16. }
    17.  
    18. void push(int stack, int num) {
    19. if(stack == LEFT_STACK) {
    20. if (st.top1 < st.top2-1) {
    21. st.top1++;
    22. st.array[st.top1] = num;
    23. } else {
    24. printf("Left Stack Full");
    25. return;
    26. }
    27. } else if(stack == RIGHT_STACK) {
    28. if (st.top1 < st.top2-1) {
    29. st.top2--;
    30. st.array[st.top2] = num;
    31. } else {
    32. printf("Right Stack Full");
    33. return;
    34. }
    35. }
    36. }
    37.  
    38. int pop(int stack) {
    39. if(stack == LEFT_STACK) {
    40. if(st.top1 >= 0){
    41. return st.array[st.top1--];
    42. } else {
    43. printf("Left Stack is Empty");
    44. return INT_MIN;
    45. }
    46. } else if(stack == RIGHT_STACK) {
    47. if(st.top2 <= ARRAY_SIZE-1){
    48. return st.array[st.top2++];
    49. } else {
    50. printf("Right Stack is Empty");
    51. return INT_MIN;
    52. }
    53. }
    54. }
    55.  
    56. int main() {
    57. initialize();
    58. /* Push element in left stack */
    59. push(LEFT_STACK, 2);
    60. push(LEFT_STACK, 4);
    61. push(LEFT_STACK, 6);
    62. /* Push element in right stack */
    63. push(RIGHT_STACK, 1);
    64. push(RIGHT_STACK, 3);
    65. push(RIGHT_STACK, 5);
    66. /*Pop Elements from left stack */
    67. printf("Pop from left stack %d\n", pop(LEFT_STACK));
    68. /*Pop Elements from right stack */
    69. printf("Pop from right stack %d\n", pop(RIGHT_STACK));
    70. return 0;
    71. }