Tweet
/* Program to implement circular queue in java */
import java.io.*;
public class cqueue
{
int front=-1,rear=-1;
void insert(int a[],int n)throws IOException
{
int val;
System.out.print("\nInput Element : ");
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
val=Integer.parseInt(br.readLine());
if(front==0 && rear==n-1)
System.out.print("\nQueue overflow... \n");
else if(rear == front - 1)
System.out.print("\nQueue Overflow...\n");
else if(front==-1 && rear==-1)
{
front=0;
a[++(rear)]=val;
}
else if(rear==n-1)
{
rear=0;
a[rear]=val;
}
else
a[++(rear)]=val;
}
void del(int a[],int n)
{
if(front==-1)
System.out.print("\nQueue underflow ... \n");
else if(front == rear)
{
front=-1;
rear=-1;
}
else if(front==n-1) front=0;
else (front)++;
}
void display(int a[],int n)
{
int i;
if(front<0)
System.out.print("\nQueue underflow ... \n");
else if(rear >= front)
{
System.out.print("\nQueue : ");
for(i=front;i<=rear;i++)
System.out.print(a[i] + " ");
}
else
{
System.out.print("\nQueue : ");
for(i=front;i<n;i++)
System.out.print(a[i] + " ");
for(i=0;i<=rear;i++)
System.out.print(a[i] + " ");
}
}
public void main(String args[]) throws IOException
{
System.out.println("Program to implement circular queue : \n");
int n,ch;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter array capacity : ");
n=Integer.parseInt(br.readLine());
int [] a;
a=new int[n];
do
{
System.out.println("\n[1] Insert [2] Delete [3] Display [4] Exit\n");
System.out.print("Enter your choice : ");
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
ch=Integer.parseInt(br1.readLine());
switch(ch)
{
case 1:
insert(a,n);
display(a,n);
break;
case 2:
del(a,n);
display(a,n);
break;
case 3:
display(a,n);
break;
case 4:
break;
default:System.out.println("Enter correct choice...");
break;
}
}while(ch!=4);
}
}
Array Circular Queue in Java
Posted by
LAHAUL SETH
~
Array Circular Queue in Java
2011-12-07T17:59:00+05:30
LAHAUL SETH
Java
|
Queue Implementation
|
Comments
Array Circular Queue in Java
2011-12-07T17:59:00+05:30
LAHAUL SETH
Java
|
Queue Implementation
|