fork(1) download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define MAXSIZE 10
  4.  
  5. struct stack
  6. {
  7. int stk[MAXSIZE];
  8. int top;
  9. };
  10. struct stack s,m;
  11.  
  12.  
  13. void push (struct stack *T, int n)
  14. {
  15. struct stack temp;
  16. temp = *T;
  17. // printf("\nElement %d\n",n);
  18. int y;
  19. if (temp.top == (MAXSIZE - 1))
  20. {
  21. printf ("Stack is Full\n");
  22. return;
  23. }
  24. else
  25. {
  26. temp.top = temp.top + 1;
  27. temp.stk[temp.top] = n;
  28. }
  29. //printf("Top index %d\n",temp.top);
  30. *T=temp;
  31. return;
  32. }
  33. int pop(struct stack *T){
  34. int y;
  35. struct stack temp;
  36. temp = *T;
  37.  
  38. if (temp.top == - 1)
  39. {
  40. printf ("Stack is Empty\n");
  41. *T=temp;
  42. return -1;
  43. }
  44. else
  45. {
  46. y=temp.stk[temp.top];
  47. temp.top = temp.top - 1;
  48. *T=temp;
  49. return(y);
  50. }
  51.  
  52. }
  53.  
  54. void specialPush(int num){
  55. int y;
  56. if (s.top == (MAXSIZE - 1))
  57. {
  58. printf ("Stack is Full\n");
  59. return;
  60. }
  61. else if(s.top == -1) //empty stacks
  62. {
  63. //printf("\nFirst time called for %d",num);
  64. push(&s,num);
  65. push(&m,num);
  66. //printf("\nFirst time Ended");
  67. //pushing num in both stack
  68. }
  69. else{//if stack is having elements
  70. y = pop(&m);//pop top to compare
  71. push(&m,y); //push again top element
  72.  
  73. if(num>y){
  74. //printf("pop if called %d",y);
  75. push(&m,y);
  76. push(&s,num);
  77. }
  78. else{
  79. // printf("pop if called");
  80. push(&m,num);
  81. push(&s,num);
  82. }
  83. }
  84. }
  85.  
  86. int getMin(){
  87. int y;
  88. y = pop(&m);
  89. //printf("poped%d\n",y);
  90. push(&m,y);
  91. return y;
  92. }
  93.  
  94. void display (struct stack *T)
  95. {
  96. struct stack temp;
  97. temp = *T;
  98. int i;
  99. if (temp.top == -1)
  100. {
  101. printf ("Stack is empty\n");
  102. return;
  103. }
  104. else
  105. {
  106. printf ("\nThe status of the stack is\n");
  107. for (i = temp.top; i >= 0; i--)
  108. {
  109. printf ("%d\n", temp.stk[i]);
  110. }
  111. }
  112. printf ("\n");
  113. *T=temp;
  114. }
  115.  
  116. int main()
  117. {
  118. s.top = -1;
  119. m.top = -1;
  120.  
  121. specialPush(10);
  122. specialPush(5);
  123. specialPush(20);
  124. specialPush(30);
  125. printf("\nAfter 10,5,20,30\n");
  126. display(&m);
  127. printf("Min->%d\n",getMin());
  128.  
  129. printf("After 10,5,20,30,1 \n");
  130. specialPush(1);
  131. display(&m);
  132. printf("Min->%d\n",getMin());
  133. return 0;
  134.  
  135. }
Success #stdin #stdout 0s 2292KB
stdin
Standard input is empty
stdout
After 10,5,20,30

The status of the stack is
5
5
5
10

Min->5
After 10,5,20,30,1 

The status of the stack is
1
5
5
5
10

Min->1