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. stack[++top] = y;
  20. printf("Pushed\n");
  21. } else {
  22. printf("Overflow\n");
  23. }
  24. }
  25.  
  26. void pop() {
  27. if (count< 2) {
  28. printf("Underflow\n");
  29. } else {
  30. int x = stack[top--];
  31. int y = stack[top--];
  32. if (count< MAX_SIZE ) {
  33. stack[++top] = y;
  34. }
  35. }
  36. }
  37.  
  38. void top_element()
  39. {
  40. if (top== -1) {
  41. printf("Empty\n");
  42. } else {
  43. printf("%d\n", stack[top]);
  44. }
  45. }
  46.  
  47. int main() {
  48. stack;
  49. top = -1;
  50.  
  51. int n;
  52. scanf("%d", &n);
  53.  
  54. for (int i = 0; i < n; i++) {
  55. int command;
  56. scanf("%d", &command);
  57.  
  58. if (command == 1) {
  59. int x, y;
  60. scanf("%d %d", &x, &y);
  61. push( x, y);
  62. } else if (command == 2) {
  63. pop();
  64. } else if (command == 3) {
  65. top_element();
  66. }
  67. }
  68.  
  69. return 0;
  70. }
Success #stdin #stdout 0s 5276KB
stdin
3
1 2 3
2
3
stdout
Pushed
Underflow
3