Queue Data Structure Theory and Program in java

0

 Queue Data Structure:

 What Is It?

A queue is described as a linear data structure with open ends and FIFO (First In, First Out) execution of operations.


A queue, according to our definition, is a list in which all additions are made at one end and all deletions are made at the other. The operation is started on the element that is pushed into the order first.

Queue Operations:

  1. Display Queue()
  2. EnQueue()
  3. DeQueue()
  4. isEmpty()
  5. isFull()
  6. Peek()\
  7. Clear Queue()


Queue Program in java:

package queueJhangir;


public class Queuee 

{

 

    static int front, rear, length;


    static int queue[];

    

    Queuee(int x)

    {

        front = rear = 0;


        length = x;


        queue = new int[length];

    }


    public void queueEnqueue(int data)


    {


    queue[rear] = data;


        rear++;

            

        System.out.println("Enqueued Item");

        

    }


    public void queueDequeue()


    {

    for (int i = 0; i < rear - 1; i++) 

    {

   

    queue[i] = queue[i + 1];

   

    }

   

    if (rear < length)

    {

   

    queue[rear] = 0;

   

    }

   

    rear--;

   

    System.out.println("Dequeued Item");

    }

 


    public void Peek()

    {


        if (front == rear) 

        {


            System.out.println(" Queue is Empty ");


        }


        System.out.println("Front or Peek value in queue is : "+ queue[front]);

    }

    

    public boolean isFull()

    {

    if (length == rear) 

    return true;

        else

            return false;

    }

    

    public boolean isEmpty()

    {

    if (front == rear) 

    return true;

        else

            return false;

    }

    

   

    public void clearQueue()

    {

    for(int i=0;i<length;i++)

    {

    queue[i]=0;

    }

    System.out.println("Queue is cleared");

    }

    public void queueDisplay()


    {

        int i;


        if (front == rear) 

        {


            System.out.println(" Queue is Empty ");


        }

 

        for (i = front; i < rear; i++) 

        {

       

            System.out.println("  "+ queue[i]); 

        

        }

        

        System.out.println("stop queue");


    }

public void displayMenu()

{

System.out.println("Choose any of the following:");

System.out.println("1...................Display Queue");

System.out.println("2...................EnQueue");

System.out.println("3...................DeQueue");

System.out.println("4...................isEmpty");

System.out.println("5................... isFull");

System.out.println("6...................Peek");

System.out.println("7...................Clear Queue");

}

}


Main class:

package queueJhangir;

import java.util.Scanner;

public class mainClass

{

public static void main(String[] args)

    {

Queuee q;

        

        System.out.println("Enter length for create a Queue");

        

        Scanner i=new Scanner(System.in);

        

        int s=i.nextInt();

        

        q= new Queuee (s);

        

        int choice=0;

        

        q.displayMenu();

        while(true)

{

Scanner ob= new Scanner(System.in);

choice=ob.nextInt();

switch(choice)

{

case 1:

q.queueDisplay();

break;

case 2:

System.out.println("Enter value to EnQueue");

int n;

n=ob.nextInt();

q.queueEnqueue(n);

break;

case 3:

q.queueDequeue();

break;

case 4:

if(q.isEmpty()==true)

System.out.println("Queue is Empty");

else

System.out.println("Queue is not Empty");

break;

case 5:if(q.isFull()==true)

System.out.println("Queue is Full");

else

System.out.println("Queue is not Full");

break;

case 6:

q.Peek();

break;

case 7:

q.clearQueue();

break;

}

}

}

}



Queue data structure program in java


Post a Comment

0Comments
Post a Comment (0)