Tweet
import java.io.*;
public class queue
{
int front=-1,rear=-1;
void insert(int a[],int n)throws IOException
{
int x;
System.out.print("\n Input element : ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
x=Integer.parseInt(br.readLine());
if((front==-1)&&(rear==-1))
{
rear++;
a[++front]=x;
}
else if((rear<n-1)&&(front>-1))
a[++rear]=x;
else
System.out.println("\nQueue overflow !!! ");
}
void delete(int a[])
{
int x;
if(front==-1)
System.out.println("\nQueue underflow !!! ");
else if(front> rear)
System.out.println("\nQueue underflow !!! ");
else
{
x=a[front];
front++;
System.out.println("\nDeleted element is : " + x);
}
}
void display(int a[],int n)
{
int i;
if(front==-1)
System.out.println("\nQueue underflow !!! ");
else if(front>=n)
System.out.println("\nQueue underflow !!! ");
else
{
System.out.print("\nQueue : ");
for(i=front;i<=rear;i++)
System.out.print(a[i] + " ");
}
}
public void main(String args[]) throws IOException
{
System.out.println("Program to implement 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:
delete(a);
display(a,n);
break;
case 3:
display(a,n);
break;
case 4:
break;
default:System.out.println("Enter correct choice...");
break;
}
}while(ch!=4);
}
}
Queue using array in java
Posted by
LAHAUL SETH
~
Queue using array in java
2011-12-07T17:43:00+05:30
LAHAUL SETH
Java
|
Queue Implementation
|
Comments
Queue using array in java
2011-12-07T17:43:00+05:30
LAHAUL SETH
Java
|
Queue Implementation
|