WikiGalaxy

Personalize

Queue Underflow and Overflow

Understanding Queue Overflow:

Queue overflow occurs when an attempt is made to add an element to a full queue. This situation arises when the queue has reached its maximum capacity and cannot accommodate any more elements.

Understanding Queue Underflow:

Queue underflow occurs when an attempt is made to remove an element from an empty queue. This situation arises when there are no elements present in the queue to be dequeued.


      // Visualization of Queue Overflow and Underflow
      // Assume a queue with a capacity of 3
      // Initial State: []
      // Enqueue Operation: [A]
      // Enqueue Operation: [A, B]
      // Enqueue Operation: [A, B, C]
      // Attempt to Enqueue D: Overflow Error (Queue is full)
      
      // Dequeue Operation: [B, C]
      // Dequeue Operation: [C]
      // Dequeue Operation: []
      // Attempt to Dequeue: Underflow Error (Queue is empty)
    

      import java.util.LinkedList;
      import java.util.Queue;

      class QueueExample {
        public static void main(String[] args) {
          Queue queue = new LinkedList<>();
          int maxSize = 3;

          // Enqueue elements
          if(queue.size() < maxSize) {
            queue.add("A");
          } else {
            System.out.println("Overflow Error");
          }

          if(queue.size() < maxSize) {
            queue.add("B");
          } else {
            System.out.println("Overflow Error");
          }

          if(queue.size() < maxSize) {
            queue.add("C");
          } else {
            System.out.println("Overflow Error");
          }

          // Attempt to enqueue beyond capacity
          if(queue.size() < maxSize) {
            queue.add("D");
          } else {
            System.out.println("Overflow Error");
          }

          // Dequeue elements
          if(!queue.isEmpty()) {
            queue.remove();
          } else {
            System.out.println("Underflow Error");
          }

          if(!queue.isEmpty()) {
            queue.remove();
          } else {
            System.out.println("Underflow Error");
          }

          if(!queue.isEmpty()) {
            queue.remove();
          } else {
            System.out.println("Underflow Error");
          }

          // Attempt to dequeue from empty queue
          if(!queue.isEmpty()) {
            queue.remove();
          } else {
            System.out.println("Underflow Error");
          }
        }
      }
    

Detailed Explanation of Code:

This Java code demonstrates queue operations and handles overflow and underflow conditions. It uses a LinkedList to implement a queue with a maximum size. The code checks the queue's size before enqueuing to prevent overflow and checks if the queue is empty before dequeuing to avoid underflow.

Console Output:

Overflow Error

Underflow Error

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025