WikiGalaxy

Personalize

Operations on Queue

Introduction to Queues:

A queue is a linear data structure that follows the First In First Out (FIFO) principle. It is used in various applications such as scheduling processes, managing requests in a web server, and handling asynchronous data.

Enqueue Operation:

Adding an element to the end of the queue is called enqueue. This operation increases the size of the queue by one and places the new element at the rear.


        // Enqueue Operation Diagram
        [Front] -> [Data1] -> [Data2] -> [New Data] -> [Rear]
      

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

        public class QueueExample {
          public static void main(String[] args) {
            Queue queue = new LinkedList<>();
            queue.add("A");
            queue.add("B");
            queue.add("C");
            System.out.println(queue);
          }
        }
      

Console Output:

[A, B, C]

Dequeue Operation:

Removing an element from the front of the queue is called dequeue. This operation decreases the size of the queue by one and removes the front element.


        // Dequeue Operation Diagram
        [Front] -> [Data1] -> [Data2] -> [Data3] -> [Rear]
        After Dequeue:
        [Front] -> [Data2] -> [Data3] -> [Rear]
      

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

        public class QueueExample {
          public static void main(String[] args) {
            Queue queue = new LinkedList<>();
            queue.add("A");
            queue.add("B");
            queue.add("C");
            queue.poll();  // removes "A"
            System.out.println(queue);
          }
        }
      

Console Output:

[B, C]

Peek Operation:

The peek operation retrieves the front element of the queue without removing it. It is useful for checking the next element to be processed.


        // Peek Operation Diagram
        [Front] -> [Data1] -> [Data2] -> [Data3] -> [Rear]
        Peek Result: Data1
      

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

        public class QueueExample {
          public static void main(String[] args) {
            Queue queue = new LinkedList<>();
            queue.add("A");
            queue.add("B");
            queue.add("C");
            String front = queue.peek();  // retrieves "A"
            System.out.println(front);
          }
        }
      

Console Output:

A

IsEmpty Operation:

The isEmpty operation checks whether the queue is empty. It returns true if the queue has no elements, and false otherwise.


        // IsEmpty Operation Diagram
        [Front] -> [ ] -> [Rear]
        IsEmpty Result: true
      

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

        public class QueueExample {
          public static void main(String[] args) {
            Queue queue = new LinkedList<>();
            boolean isEmpty = queue.isEmpty();
            System.out.println(isEmpty);
          }
        }
      

Console Output:

true

Size Operation:

The size operation returns the number of elements present in the queue. It helps in determining how many elements are currently stored in the queue.


        // Size Operation Diagram
        [Front] -> [Data1] -> [Data2] -> [Data3] -> [Rear]
        Size Result: 3
      

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

        public class QueueExample {
          public static void main(String[] args) {
            Queue queue = new LinkedList<>();
            queue.add("A");
            queue.add("B");
            queue.add("C");
            int size = queue.size();
            System.out.println(size);
          }
        }
      

Console Output:

3

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025