WikiGalaxy

Personalize

Push Operations on Stack

Understanding Stack Push Operation:

The stack data structure follows a Last In First Out (LIFO) principle. The push operation adds an element to the top of the stack. If the stack is full, an overflow condition occurs.


      // Example 1: Basic Push Operation
      Stack stack = new Stack<>();
      stack.push(10);
      stack.push(20);
      System.out.println(stack); // Output: [10, 20]
    

Step-by-Step Diagram:

      Step 1: Initial Stack State
      [Empty]

      Step 2: Push 10
      [10]

      Step 3: Push 20
      [10, 20]
    

Example 2: Handling Overflow

Scenario:

If the stack has a fixed size, attempting to push an element when the stack is full will cause an overflow error.


      // Example 2: Stack Overflow
      Stack stack = new Stack<>();
      stack.push(1);
      stack.push(2);
      // Assume stack size is 2
      stack.push(3); // This will cause an overflow
    

Step-by-Step Diagram:

      Step 1: Initial Stack State
      [Empty]

      Step 2: Push 1
      [1]

      Step 3: Push 2
      [1, 2]

      Step 4: Attempt to Push 3
      Overflow Error
    

Example 3: Push with Dynamic Stack

Dynamic Stack:

A dynamic stack automatically resizes when it reaches its capacity, allowing for more elements to be pushed without overflow.


      // Example 3: Dynamic Stack
      ArrayList dynamicStack = new ArrayList<>();
      dynamicStack.add(5);
      dynamicStack.add(15);
      dynamicStack.add(25);
      System.out.println(dynamicStack); // Output: [5, 15, 25]
    

Step-by-Step Diagram:

      Step 1: Initial Stack State
      [Empty]

      Step 2: Add 5
      [5]

      Step 3: Add 15
      [5, 15]

      Step 4: Add 25
      [5, 15, 25]
    

Example 4: Push Operation in Java Collection

Java Collections:

Java's Stack class is part of the Java Collections Framework and allows for easy push operations.


      // Example 4: Using Java Stack Class
      Stack stack = new Stack<>();
      stack.push("Apple");
      stack.push("Banana");
      System.out.println(stack); // Output: [Apple, Banana]
    

Step-by-Step Diagram:

      Step 1: Initial Stack State
      [Empty]

      Step 2: Push "Apple"
      ["Apple"]

      Step 3: Push "Banana"
      ["Apple", "Banana"]
    

Example 5: Push in Custom Stack Implementation

Custom Stack:

Implementing a custom stack allows for a deeper understanding of the stack operations like push, pop, and peek.


      // Example 5: Custom Stack Implementation
      class CustomStack {
          private List stack = new ArrayList<>();

          public void push(int value) {
              stack.add(value);
          }

          @Override
          public String toString() {
              return stack.toString();
          }
      }

      CustomStack myStack = new CustomStack();
      myStack.push(100);
      myStack.push(200);
      System.out.println(myStack); // Output: [100, 200]
    

Step-by-Step Diagram:

      Step 1: Initial Stack State
      [Empty]

      Step 2: Push 100
      [100]

      Step 3: Push 200
      [100, 200]
    
logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025