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