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