import java.io.*;
class DeSub{
int q[];
int size,r=-1,f=0;
public DeSub(int s)
{
size = s;
q=new int[s];
}
public void insert_first(int val)
{
if (r-f==size-1)
System.out.println("queue full");
else if (r==-1)
{
q[++r]=val;
}
class DeSub{
int q[];
int size,r=-1,f=0;
public DeSub(int s)
{
size = s;
q=new int[s];
}
public void insert_first(int val)
{
if (r-f==size-1)
System.out.println("queue full");
else if (r==-1)
{
q[++r]=val;
}
else {
for(int j=r;j>=f;j--)
{
q[j+1]=q[j];
q[f]=val;
}
}}
public void insert_last(int val)
{
if (r-f==size-1)
System.out.println("queue full");
else
q[++r]=val;
}
public void remove_first()
{
if(r<f)
System.out.println("queue empty");
else
f++;
}
public void remove_last()
{
if(r<f)
System.out.println("queue empty ");
else
r--;
}
public void show()
{
for(int i=f;i<=r;i++)
{
System.out.println(" " +q[i]);
}}
}
class DeQueue
{
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());
DeSub s =new DeSub(q);
int g,e=0,i;
do
{
System.out.print("\nWhat u want to do?? 1-insertfirst 2-insertlast 3-deletefirst 4. delete last 5-exit ");
g=Integer.parseInt(br.readLine());
if((g==1)||(g==2))
{
System.out.print("enter the value");
e=Integer.parseInt(br.readLine());
}
switch(g)
{
case 1:s.insert_first(e);break;
case 2:s.insert_last(e);break;
case 3:s.remove_first();break;
case 4:s.remove_last();break;
case 5:s.show();
}
System.out.print("Do you want to continue 1...y 2....n ???");
i=Integer.parseInt(br.readLine());
}while(i<=1);
}
}
0 comments:
Post a Comment