fork download
  1. // Stack class to implement stack operations
  2. class Stack {
  3. private int[] stackArray; // Array to hold stack elements
  4. private int top; // Index of the top element in the stack
  5. private int capacity; // Maximum capacity of the stack
  6.  
  7. // Constructor to initialize the stack with a given size
  8. public Stack(int size) {
  9. stackArray = new int[size]; // Create an array of given size
  10. capacity = size;
  11. top = -1; // Initially, the stack is empty
  12. }
  13.  
  14. // Push operation: Adds an element to the stack
  15. public void push(int item) {
  16. if (top == capacity - 1) {
  17. System.out.println("Stack Overflow. Cannot push " + item);
  18. } else {
  19. stackArray[++top] = item; // Increment top and insert element
  20. System.out.println("Pushed: " + item);
  21. }
  22. }
  23.  
  24. // Pop operation: Removes the top element from the stack
  25. public int pop() {
  26. if (isEmpty()) {
  27. System.out.println("Stack Underflow. Cannot pop.");
  28. return -1; // Return -1 to indicate stack is empty
  29. } else {
  30. int poppedItem = stackArray[top--]; // Return the top element and decrement top
  31. System.out.println("Popped: " + poppedItem);
  32. return poppedItem;
  33. }
  34. }
  35.  
  36. // Peek operation: Returns the top element of the stack without removing it
  37. public int peek() {
  38. if (isEmpty()) {
  39. System.out.println("Stack is empty. Cannot peek.");
  40. return -1; // Return -1 if the stack is empty
  41. } else {
  42. System.out.println("Top element: " + stackArray[top]);
  43. return stackArray[top];
  44. }
  45. }
  46.  
  47. // isEmpty operation: Checks if the stack is empty
  48. public boolean isEmpty() {
  49. return top == -1; // Stack is empty if top is -1
  50. }
  51.  
  52. // Size operation: Returns the current size of the stack
  53. public int size() {
  54. return top + 1; // The size is the number of elements in the stack
  55. }
  56. }
  57.  
  58. // Driver class to test stack operations
  59. public class Main {
  60. public static void main(String[] args) {
  61. Stack stack = new Stack(5); // Create a stack of capacity 5
  62.  
  63. // Push some items to the stack
  64. stack.push(10);
  65. stack.push(20);
  66. stack.push(30);
  67. stack.push(40);
  68. stack.push(50);
  69.  
  70. // Attempt to push beyond the stack's capacity
  71. stack.push(60);
  72.  
  73. // Peek at the top element
  74. stack.peek();
  75.  
  76. // Pop the top element
  77. stack.pop();
  78.  
  79. // Check if the stack is empty
  80. System.out.println("Is the stack empty? " + stack.isEmpty());
  81.  
  82. // Get the size of the stack
  83. System.out.println("Stack size: " + stack.size());
  84.  
  85. // Pop all items from the stack
  86. stack.pop();
  87. stack.pop();
  88. stack.pop();
  89. stack.pop();
  90.  
  91. // Try to pop from an empty stack
  92. stack.pop();
  93. }
  94. }
  95.  
Success #stdin #stdout 0.1s 53784KB
stdin
Standard input is empty
stdout
Pushed: 10
Pushed: 20
Pushed: 30
Pushed: 40
Pushed: 50
Stack Overflow. Cannot push 60
Top element: 50
Popped: 50
Is the stack empty? false
Stack size: 4
Popped: 40
Popped: 30
Popped: 20
Popped: 10
Stack Underflow. Cannot pop.