queue

Watch java videos

Queue : Data Structure is a way to collect and organize data in such a way that we can perform operations on these data in an effective way.
It is a way to store and organize data so that it can be used efficiently. In computer science, it is a data organization, management, and storage format that enables efficient access and modification.

In this post we will discuss the Queue data structure. It follows the FIFO(First In First Out) principal. In this data structure the elements are inserted and deleted from the same end. It is a data structure where the element that is inserted the first is removed the first. The end from where the elements are inserted is called rear. The end from where the elements are removed is called front.

Enqueue : When an element is inserted in the queue, it is called enqueue.
Dequeue : When an element is removed from the queue, this process is called dequeue.
Applications of queue: It is used to CPU scheduling, Disk scheduling,

Queue
import java.io.*;
public class Queue
{
    int Q[];
    int size;
    int front;
    int rear;
    Queue(int cap)
    {
        size = cap;
        Q = new int[size];
        front = 0;
        rear = 0;
    }

    void insert(int v)
    {
        if(rear == size)
        {
            System.out.println("OVERFLOW");
        }
        else
        {
            Q[rear] = v;
            rear = rear + 1;
        }
    }

    int delete()
    {
        if(front == 0 && rear == 0)
        {
            System.out.println("UNDERFLOW");
            return -999;
        }
        else
        {
            int val = Q[front];
            front = front + 1;
            if(front == rear)
            {
                front = 0;
                rear = 0;
            }
            return val;
        }
    }

    void display()
    {
        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(Q[i]);
            }
        }
    }

    public static void main()throws IOException
    {
        System.out.println("************************************ Queue ***********************************");
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter size of Queue:"); int n=Integer.parseInt(br.readLine()); Queue obj=new Queue(n); int ch, gr, t;
        do
        {
            System.out.println("Enter 1 to push element");
            System.out.println("Enter 2 to remove element"); System.out.println("Enter 3 to print list"); System.out.println("Enter 0 to Exit"); System.out.println("Enter your choice"); ch=Integer.parseInt(br.readLine()); switch(ch)
            {
                case 1:
                System.out.println("Enter the value");
                gr=Integer.parseInt(br.readLine());
                obj.insert(gr);
                break;
                case 2:
                gr=obj.delete();
                System.out.println("Obtained Value:"+gr);
                System.out.println("Enter 1 to continue");
                t=Integer.parseInt(br.readLine());
                break;
                case 3:
                obj.display();
                System.out.println("Press 1 to continue");
                t=Integer.parseInt(br.readLine());
                break;
                case 0:
                System.out.println("Thanks For Operating"); break;
                default:
                System.out.println("Re-enter your choice");
            }
        }
        while(ch!=0);
}
}

Output:
********************************Queue *********************************
Enter size of Queue:
4
Enter 1 to push element
Enter 2 to remove element
Enter 3 to print list
Enter 0 to Exit
Enter your choice
1
Enter the value
45
Enter 1 to push element
Enter 2 to remove element
Enter 3 to print list
Enter 0 to Exit
Enter your choice
1
Enter the value
67
Enter 1 to push element
Enter 2 to remove element
Enter 3 to print list
Enter 0 to Exit
Enter your choice
3
The elements in the queue are :
45
67
Press 1 to continue

Array programs

LEAVE A REPLY

Please enter your comment!
Please enter your name here