/* File Name : MyQueue.java*/
import java.util.Scanner;
import java.io.*;
class MyQueue //Start of stack class
{
int a[];
int front,rear;
MyQueue() //Initialize Stack in Default Manner
{
a=new int[5];
front=rear=-1;
}
MyQueue(int size)
{
a=new int[size];
front=rear=-1;
}
boolean empty() // Start of Empty Method
{
return(front==-1 || rear==-1) ?true:false;
} //End of Empty Method
void insert(int ele) //Start of insert Method
{
if(front==(rear+1)%a.length)
System.out.println("\n\t\t Queue Overflow.");
else
{
rear=(rear+1)%a.length;
a[rear]=ele;
System.out.println("\n\t\t Queue-->");
for(int i=rear;i>=0;i--)
System.out.println("\n\t\t\t\t"+a[i]);
}
if(front==-1)
front++;
} //End of insert Method
String remove() // Start of remove Method
{
if(!empty()) {// Checking stack for empty
String s ="\n\t\t Removed ELEMENT:: "+a[front];
if(front==rear)
front=rear=-1;
else{
front=(front+1)%a.length;
return s;}
}
else {
return"\n\t\t Queue is empty";}
} //End of remove Method
void display()
{
System.out.println("\n\t\t Queue-->");
for(int i=front;i<=rear;i++)
System.out.println("\n\t\t\t\t"+a[i]);
}
public static void main(String []arg)throws IOException //Start of main method
{
int ch,n;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//Scanner sc=new Scanner(System.in);
MyQueue st = new MyQueue();
while(true)
{
System.out.println("\n\t\t\t\t Queue");
System.out.println("\t\t1.Initialize");
System.out.println("\t\t2.Insert");
System.out.println("\t\t3.Remove");
System.out.println("\t\t4.Display");
System.out.println("\t\t5.Exit");
System.out.print("\n\t\tEnter your choice : ");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
{
System.out.print("\n\t\tEnter the Size of Queue :: ");
int a=Integer.parseInt(br.readLine());
st=new MyQueue(a);
}
break;
case 2:
System.out.print("\n\t\tEnter Number : ");
n=Integer.parseInt(br.readLine());
st.insert(n);
break;
case 3:
System.out.print(st.remove());
st.display();
break;
case 4:
st.display();
break;
case 5:
return;
default:
System.out.println("\n \t \t Please Select From 1 To 5 ");
}
}
} //End of Main Method
}//End of Class
import java.util.Scanner;
import java.io.*;
class MyQueue //Start of stack class
{
int a[];
int front,rear;
MyQueue() //Initialize Stack in Default Manner
{
a=new int[5];
front=rear=-1;
}
MyQueue(int size)
{
a=new int[size];
front=rear=-1;
}
boolean empty() // Start of Empty Method
{
return(front==-1 || rear==-1) ?true:false;
} //End of Empty Method
void insert(int ele) //Start of insert Method
{
if(front==(rear+1)%a.length)
System.out.println("\n\t\t Queue Overflow.");
else
{
rear=(rear+1)%a.length;
a[rear]=ele;
System.out.println("\n\t\t Queue-->");
for(int i=rear;i>=0;i--)
System.out.println("\n\t\t\t\t"+a[i]);
}
if(front==-1)
front++;
} //End of insert Method
String remove() // Start of remove Method
{
if(!empty()) {// Checking stack for empty
String s ="\n\t\t Removed ELEMENT:: "+a[front];
if(front==rear)
front=rear=-1;
else{
front=(front+1)%a.length;
return s;}
}
else {
return"\n\t\t Queue is empty";}
} //End of remove Method
void display()
{
System.out.println("\n\t\t Queue-->");
for(int i=front;i<=rear;i++)
System.out.println("\n\t\t\t\t"+a[i]);
}
public static void main(String []arg)throws IOException //Start of main method
{
int ch,n;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//Scanner sc=new Scanner(System.in);
MyQueue st = new MyQueue();
while(true)
{
System.out.println("\n\t\t\t\t Queue");
System.out.println("\t\t1.Initialize");
System.out.println("\t\t2.Insert");
System.out.println("\t\t3.Remove");
System.out.println("\t\t4.Display");
System.out.println("\t\t5.Exit");
System.out.print("\n\t\tEnter your choice : ");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
{
System.out.print("\n\t\tEnter the Size of Queue :: ");
int a=Integer.parseInt(br.readLine());
st=new MyQueue(a);
}
break;
case 2:
System.out.print("\n\t\tEnter Number : ");
n=Integer.parseInt(br.readLine());
st.insert(n);
break;
case 3:
System.out.print(st.remove());
st.display();
break;
case 4:
st.display();
break;
case 5:
return;
default:
System.out.println("\n \t \t Please Select From 1 To 5 ");
}
}
} //End of Main Method
}//End of Class
Comments
Post a Comment