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. void top_element()
  45. {
  46. if (top== -1) {
  47. printf("Empty\n");
  48. } else {
  49. printf("%d\n", stack[top]);
  50. }
  51. }
  52.  
  53. int main() {
  54. stack;
  55. top = -1;
  56.  
  57. int n;
  58. scanf("%d", &n);
  59.  
  60. for (int i = 0; i < n; i++) {
  61. int command;
  62. scanf("%d", &command);
  63.  
  64. if (command == 1) {
  65. int x, y;
  66. scanf("%d %d", &x, &y);
  67. push( x, y);
  68. } else if (command == 2) {
  69. pop();
  70. } else if (command == 3) {
  71. top_element();
  72. }
  73. }
  74.  
  75. return 0;
  76. }
Success #stdin #stdout 0s 5288KB
stdin
3
3
2
1 2 3
stdout
Empty
Underflow
Pushed