Queue Code Example Java Programming



package yasolution.org;
import java.util.Scanner;

public class YaSolution {

static int front=-1, rear=-1;
static int[] queue= new int[5];
static boolean isRunning=true;
   
public static void main(String[] arg){
   
do{
switch(get_cmd()){
case 1:
rearFunc();
break;
case 2:
frontFunc();
break;
case 3:
showFunc();
break;
case 4:
isRunning=false;
break;
default:
System.out.println("Invalid option.");
}
}while(isRunning);
}

static int get_cmd(){
int n;
System.out.println("===========Menu===========");
System.out.println("1. Add");
System.out.println("2. Remove");
System.out.println("3. Show");
System.out.println("4. Exit");
System.out.print("Select option: ");
        Scanner scan = new Scanner(System.in);
n=scan.nextInt();
return n;
}

static void rearFunc(){
if(rear==4){
System.out.println("Queue is full.");
}else{
rear++;
System.out.print("Input a value: ");
                Scanner scan = new Scanner(System.in);
queue[rear]=scan.nextInt();
}
}

static void frontFunc(){
if(front==rear){
System.out.println("Queue is empty.");
}else{
front++;
}
System.out.println();
}

static void showFunc(){
if(rear==front){
System.out.println("Queue is empty.");
}else{
System.out.print("Queue data: ");
for(int i=front+1;i<=rear;i++){
System.out.print(queue[i]+"\t");
}
}
System.out.println();
}
}