Are the conditions which I have included for insertion and deletion from front and rear sufficient and necessary? Have I missed some condition check? Or have I included some redundant condition?
import java.io.*;
class Dequeue
{
int arr[];
int lim;
int front;
int rear;
public Dequeue(int l)
{
lim=l;
front=rear=0;
arr=new int[(l+1)];
}
void addrear(int val)
{
if(front==0 && rear ==0)
{
front=1;
rear=1;
arr[1]=val;
}
else if(rear==lim)
{
System.out.println("Overflow");
}
else
{
arr[++rear]=val;
}
}
void addfront(int val)
{
if(front==0 && rear ==0)
{
front=1;
rear=1;
arr[1]=val;
}
else if(front==1)
{
System.out.println("Overflow");
}
else
{
arr[--front]=val;
}
}
void poprear()
{
if(front==rear)
{
System.out.println(arr[front]);
front=0;
rear=0;
}
else if(front==0 && rear==0)
{
System.out.println("Underflow");
}
else
{
System.out.println(arr[rear]);
rear--;
}
}
void popfront()
{
if(front==rear)
{
System.out.println(arr[front]);
front=0;
rear=0;
}
else if(front==0 && rear==0)
{
System.out.println("Underflow");
}
else
{
System.out.println(arr[front]);
front++;
}
}
void display()
{
if(front!=0 || rear!=0)
{
for(int i=front;i<=rear;i++)
{
System.out.print(arr[i]);
}
System.out.println();
}
else
System.out.println("Empty");
}
public static void main(String args[])throws IOException
{
int flag=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter size of array");
Dequeue obj=new Dequeue(Integer.parseInt(br.readLine()));
while(flag==0)
{
System.out.println("1 for addrear");
System.out.println("2 for addfront");
System.out.println("3 for poprear");
System.out.println("4 for popfront");
System.out.println("5 for exit");
int ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.println("Enter element");
obj.addrear(Integer.parseInt(br.readLine()));
obj.display();
break;
case 2:System.out.println("Enter element");
obj.addfront(Integer.parseInt(br.readLine()));
obj.display();
break;
case 3:obj.poprear();
obj.display();
break;
case 4:obj.popfront();
obj.display();
break;
case 5:flag=1;
break;
}
}
}
}