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