Friday 8 June 2012

 
import java.io.*;
class Queue
{
int s,f=0,a[],i,r=-1,count=0;
Queue(int x)
{
s=x;
a=new int [s];
}
public void input(int e)
{
if(count==s)// count controls the whole Q
{System.out.print("it is full...");
}
count++;
if (r==(s-1))
{
r=0;
a[r]=e;
}
else
{
r++;
a[r]=e;
}}
public void bot()
{if(count==0)
{System.out.print("Q is empty ...");
}

count--;
if (f==s)
{f=0;
System.out.print("next element out is "+a[f]);
}else
{
System.out.print("next element out is "+a[f]);
f++;
}
}
void diplay()
{
if(r<f)
{
 for(i=f;i<=s-1;i++)            //to print frm fnt up to max
 System.out.println("   -> "+a[i]);
 for(i=0;i<=r;i++)              //to print frm  o to rear value... 
 System.out.println("   -> "+a[i]);
}
else
{
        for(i=f;i<=r;i++)
        System.out.println("   -> "+a[i]);
}}}
class CirQ
{
public static  void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("enter no: Of elements...");
int q=Integer.parseInt(br.readLine());
Queue s =new Queue(q);
System.out.print("Enter the elements...");
for(int i=0;i<q;i++)
{
int d=Integer.parseInt(br.readLine());
s.input(d);
}
int g=0;
while(g<4)
{
System.out.print("\nWhat u want to do?? 1-insert 2-delete 5-exit ");
g=Integer.parseInt(br.readLine());
if(g==1)
{
System.out.print("Enter the element...");
int d=Integer.parseInt(br.readLine());
s.input(d);
}
else if(g==2)
{s.bot();}
else
{
s.diplay();
break;
}}}}

0 comments:

Post a Comment

Blogroll

Blogger templates

About