06-04-2011, 01:16 PM
Code:
import java.io.*;
class Demo
{
static final int max=4;
int a[]=new int[max];
int top,it,rear,front;
Demo()
{
rear=-1;
front=-1;
}
void insert(int item)
{
if(rear==max)
System.out.println("Queue is full");
else
{
if(rear==-1)
{
front=0;
rear=0;
}
a[rear++]=item;
}
}
int del()
{
if(front==-1)
{
it=0;
}
else
{
it=a[front];
if(front==rear)
{
rear=-1;
front=-1;
}
else
front=front+1;
}
return(it);
}
void display()
{
System.out.println("New Queue Is:");
for(int i=front;i<rear;i++)
System.out.println(a[i]);
}
}
class Queue
{
public static void main(String args[])throws IOException
{
DataInputStream dim=new DataInputStream(System.in);
Demo s=new Demo();
int op;
do
{
System.out.println("\n1.insert\n2.delete\n3.display4.exit\n");
op=Integer.parseInt(dim.readLine());
switch(op)
{
case 1:System.out.println("enter the item to insert");
int n=Integer.parseInt(dim.readLine());
s.insert(n);
break;
case 2:int f=s.del();
System.out.println(f);
if(f==0)
{
System.out.println("empty queue:");
}
else
{
System.out.println("Deleted element is:"+f);
}
break;
case 3:s.display();break;
case 4:break;
default:System.out.println("wrong choice?");
}
}while(op!=4);
}
}
OUTPUT
E:\3msc\java\joby>java Queue
1.insert
2.delete
3.display4.exit
1
enter the item to insert
1
1.insert
2.delete
3.display4.exit
1
enter the item to insert
2
1.insert
2.delete
3.display4.exit
1
enter the item to insert
3
1.insert
2.delete
3.display4.exit
3
New Queue Is:
1
2
3
1.insert
2.delete
3.display4.exit
2
1
Deleted element is:1
1.insert
2.delete
3.display4.exit
3
New Queue Is:
2
3
1.insert
2.delete
3.display4.exit
4