fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define MAX_SIZE 100
  5.  
  6.  
  7. int stack[MAX_SIZE];
  8. int top=-1,count=0;
  9. int size()
  10. {
  11. return count;
  12. }
  13.  
  14. void push(int x, int y)
  15. {
  16. if (count+2<=MAX_SIZE )
  17. {
  18. stack[++top] = x;
  19. count++;
  20. stack[++top] = y;
  21. count++;
  22. printf("Pushed\n");
  23. } else {
  24. printf("Overflow\n");
  25. }
  26. }
  27.  
  28. void pop() {
  29. if (count< 2) {
  30. printf("Underflow\n");
  31. } else {
  32. int x = stack[top--];
  33. count--;
  34. int y = stack[top--];
  35. count--;
  36. int sum=x+y;
  37.  
  38. if (count< MAX_SIZE ) {
  39. stack[++top] = sum;
  40. }
  41. }
  42. }
  43.  
  44. int top_element()
  45. {
  46. return (count>0)?stack[top]:-1;
  47. }
  48.  
  49. int main() {
  50. stack;
  51. top = -1;
  52.  
  53. int n;
  54. scanf("%d", &n);
  55.  
  56. for (int i = 0; i < n; i++) {
  57. int command;
  58. scanf("%d", &command);
  59.  
  60. if (command == 1) {
  61. int x, y;
  62. scanf("%d %d", &x, &y);
  63. push( x, y);
  64. } else if (command == 2) {
  65. pop();
  66. } else if (command == 3) {
  67. top_element();
  68. }
  69. }
  70.  
  71. return 0;
  72. }
Success #stdin #stdout 0s 5252KB
stdin
3
1 2 3
2
3
stdout
Pushed