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