0% found this document useful (0 votes)
33 views

Queue

This document contains code for a queue data structure implemented using an array. The QueueMain class contains a main method that displays a menu allowing the user to enqueue, dequeue, or display elements in the queue. The QueueOperation class implements the queue methods - enqueue adds an element to the rear, dequeue removes from the front, and display prints all elements. The output shows sample interactions of adding and removing elements from the queue and displaying its contents.

Uploaded by

prrahul14
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Queue

This document contains code for a queue data structure implemented using an array. The QueueMain class contains a main method that displays a menu allowing the user to enqueue, dequeue, or display elements in the queue. The QueueOperation class implements the queue methods - enqueue adds an element to the rear, dequeue removes from the front, and display prints all elements. The output shows sample interactions of adding and removing elements from the queue and displaying its contents.

Uploaded by

prrahul14
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Queue Main .

java

import java.io.*;
import xxx.java.com.QueueOperation;
public class Queuemain
{
public static void main(String args[])
{
QueueOperation qp=new QueueOperation();
int choice=0;
do
{
System.out.println("enter the
option:"+"\n"+"1.Enqueue"+"\n"+"2.Dequeue"+"\n"+"3.Display"+"\n"+"4.Exit");
InputStreamReader is=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(is);
try
{
choice=Integer.parseInt(br.readLine());
switch(choice)
{
case 1:
System.out.println("Insert the element int in Queue");
int p=Integer.parseInt(br.readLine());
qp.enqueue(p);
break;
case 2:
qp.dequeue();
break;
case 3:
qp.display();
break;
case 4:
break;
default:
System.out.println("Invalid choice");
break;
}}
catch(IOException e)
{
}
}
while(choice<4);
}
}
Queue Operation.java

package xxx.java.com;
public class QueueOperation
{
public static int q[]=new int[5];
public static int front=-1;
public static int rear=-1;
public void enqueue(int x)
{
if(rear>=4)
{
System.out.println("Queue is full");
}
else
{
int v=x;
rear=rear+1;
q[rear]=v;
System.out.println("the element inserted is:+q[rear]");
}
}
public void dequeue()
{
if(front==rear)
{
System.out.println("queue is empty");
}
else
{
front=front+1;
System.out.println("the element removed is:"+q[front]);
}
}
public void display()
{
if(front==rear)
{
System.out.println("queue is empty");
}
else
{
System.out.println("------------->Front");
for(int i=front+1;i<=rear;i++)
{
System.out.println(q[i]);
}
System.out.println("------------->Rear");
}
}
}
Output:

Enter the option


1.Enqueue
2.Dequeue
3.Display
4.Exit

1
Insert the element into queue
5
The element inserted is 5

Enter the option


1.Enqueue
2.Dequeue
3.Display
4.Exit

2
The element removed is 5
Enter the option
1.Enqueue
2.Dequeue
3.Display
4.Exit

3
Queue is empty

Enter the option


1.Enqueue
2.Dequeue
3.Display
4.Exit

1
Insert the element into queue
9
The element inserted is 9

Enter the option


1.Enqueue
2.Dequeue
3.Display
4.Exit

You might also like