Queue Program in Java

Queue Program in Java

Queue program in Java

What is a Queue?


The queue is a linear list of data working on FIFO(First In First Out) nature. The Insertion can take place from the Front end and deletion can take place from the Rear end. The process of insertion of an element into a queue is called Enqueue and the process of deletion of an element from the queue is called Dequeue. All operation works in constant time i.e, O(1)

Few important points regarding Queue

  • It is a Linear Data Structure
  • It follows FIFO: First In First Out algorithm
  • The Insertion can be taken place from the rear end.
  • The Deletion can be taken place from the front end.
Courtesy: Wikimedia

Insertion of Data into Queue

Deletion of Data from Queue

Java program for Queue

import java.io.*;
class Queue
{
    int queue[]; // Array of Queue
    int size;    // Size of the Queue
    int front;   // Front element
    int rear;    // Rear element
    int value;   // For value
    Queue(int size) // Parameterised Constructor
    {
        this.size = size;
        queue = new int[size];
        front = 0;
        rear = 0;
    }
    
    void insert(int value) // Function to insert element in Queue
    {
        if(rear == size) // Condition for Overflow
        {
            System.out.println("OVERFLOW");
        }
        else
        {
            queue[rear] = value; // Storing value in Queue
            rear = rear + 1;  
        }
    }    
    int delete() // Function to delete element from Queue
    {
        if(front == 0 && rear == 0) // Condition for Underflow
        {
            System.out.println("Under flow");
            return -999;
        }
        else
        {
            value = queue[front]; // Storing the element which will be removed
            front = front + 1;
            if(front == rear) // Condition for emptying the Queue
            {
                front = 0;
                rear = 0;
            }
            return value;
        }
    }
    
    void display() // Function for printing elements in the queue
    {
        if(front == 0 && rear == 0)
        {
            System.out.println("The Queue is empty");
        }
        else
        {
            System.out.println("The elements in the queue are : ");
            for(int i=front; i<rear; i++)
            {
                System.out.println(queue[i]);
            }
        }
    }
    public static void main(String args[])throws IOException
    {
        int n, value, i;
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br=new BufferedReader(in);
        System.out.println("\nEnter the size of array Queue:");
        n=Integer.parseInt(br.readLine());
        Queue ob = new Queue(n); // Object create with a parameter 
        System.out.println("Enter value into Queue:");
        for(i=0; i<n ;i++)
        {
            value=Integer.parseInt(br.readLine());
            ob.insert(value);
        }
        System.out.println("Queue Elements:");
        ob.display();
    }
}

Sample output:
Enter the size of array Queue:
5
Enter value into Queue:
4
5
3
7
8
Queue Elements:
The elements in the queue are :
4
5
3
7
8

Deletion from Queue:
4 Value is deleted
The elements in the queue are :
5
3
7
8

circular prime number program in java

circular prime number program in java

Java Program for Circular Prime

Java Program for Circular Prime

According to Wikipedia, A circular prime is a prime number with the property that the number generated at each intermediate step when cyclically permuting its (base 10) digits will be prime.

Examples : 113, 197, 199, 337, 1193, 3779, 11939, 19937 etc


Say 113 -> 131 -> 311 is circular prime.



Solution
import java.util.*;
public class CircularPrime
{
    int n, m, c, f, i, a, b, j, f1; // Instance Variables
    void circularPrime(int x)  
    {
        c=0; // Total Digit Calculate
        n=x;
        f=0;
        while(n!=0)
        {
            n/=10;
            c++;
        }
        n=x;
        System.out.println("Output");
        System.out.println("======");
        for(i=0;i<c;i++)
        {

            f=isPrime(n); // Returing 1 if Prime or 0 if not prime
            if(f==1)
                System.out.println(n);
            else
            {
                System.out.println("Not a Circular Number");
                break;
            }
            // Example 113 113 -> 131 -> 311
            a=n%((int)Math.pow(10,c-1));    // Reminder 13
            b=n/((int)Math.pow(10,c-1));    // quotient 1  
            m=a*10+b;                 // here m = 13 * 10 + 1 =131
            n=m;                      // then n = 131 this process repeated till the loop end
        }
        if(f==1)
        System.out.println("Circular Prime");
    }
    // Prime checking method which return 1 if prime else 0
    int isPrime(int x)
    {
        f1=1;
        for(j=2;j<x;j++)
        {
            if(x%j==0)
            {
                f1=0;
                break;
            }
        }
        return f1;
    }
    // Main method
    public static void main(String args[])
    {
        int n,m,c,f,i;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number :");
        n= sc.nextInt();
        CircularPrime ob=new CircularPrime();
        ob.circularPrime(n);
    }

}

Sample output
Enter a number :
113
Output
======
113
131
311
Circular Prime
Enter a number :
137
Output
======
137
Not a Circular Number

In the above program, I have used two used to define methods, namely circularPrime() and isPrime(). The circularPrime(int x) takes a value and first it find out the number of digits in the given number. After then isPrime(n) is checking number is prime or not and it checks again after interchanging the number in this fashion 113 -> 131 -> 311


More Program