Stack_Implementation in Data structure in java
Stack_Implementation in Data structure in java
// Method to push an element onto the stack // Main method to test stack operations
public void push(int x) { public static void main(String[] args) {
if (top == capacity - 1) { // Check for stack overflow Main stack = new Main(5); // Create a stack of size 5
System.out.println("Stack Overflow");
return; stack.push(1); // Push 1 onto the stack
} stack.push(2); // Push 2 onto the stack
arr[++top] = x; // Increment top and insert element stack.pop(); // Pop the top element (should remove 2)
System.out.println("Pushed: " + x); }
}
Stack Implementation Using Linked List