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