0% found this document useful (1 vote)
581 views

Bluej Program On Queue

This Java code defines a Queue class with methods to add and remove elements, check if the queue is full or empty, and display the queue. It uses an array to store the queue elements, with front and rear pointers to track the first and last elements. The main method runs an interactive menu to test the queue by allowing the user to add/remove elements and view the queue.

Uploaded by

Deep Patel
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
581 views

Bluej Program On Queue

This Java code defines a Queue class with methods to add and remove elements, check if the queue is full or empty, and display the queue. It uses an array to store the queue elements, with front and rear pointers to track the first and last elements. The main method runs an interactive menu to test the queue by allowing the user to add/remove elements and view the queue.

Uploaded by

Deep Patel
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.io.

*; public class Queue { final int MAX=10; int arr[]; int front,rear; public Queue() { arr=new int[MAX]; front=rear=-1; } public boolean isFull() { if(rear==(MAX-1)) return true; else return false; } public boolean isEmpty() { if(front==-1) return true; else return false; } public void enqueue(int n) {if(isFull()) System.out.println("****Queue overflow******"); else { rear++; arr[rear]=n; if(front==-1) front=0; System.out.println("******Element added to the queue*****"); } } public int dequeue() { int x; if(isEmpty()) x=-999; else { x=arr[front]; arr[front]=0; if(front==rear) front=rear=-1; else front++; } return x; } public void show() { if(isEmpty()) System.out.print("****Empty*****"); else

{ for (int i=front;i<=rear;i++) System.out.print(arr[i]+" } System.out.println(); } public static void main(String args[]) throws IOException { Queue obj=new Queue(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n=0, ch=0; do { System.out.println("Enter 1 for adding an element to a queue"); System.out.println("Enter 2 for deleting an element from a queue"); System.out.println("Enter 3 for showing elements of a queue"); System.out.println("Enter 4 for exit"); System.out.println("Enter your choice"); ch=Integer.parseInt(br.readLine()); switch (ch) { case 1: System.out.println("Enter an integer to be added to the queue"); n=Integer.parseInt(br.readLine()); obj.enqueue(n); break; case 2: n=obj.dequeue(); if(n==-999) System.out.println("******Queue underflow******"); else System.out.println("****Element deleted is: "+n+" ******"); break; case 3: System.out.println("******Queue status*******"); obj.show(); break; case 4: System.out.println("*****Program terminates******"); System.exit(0); default: System.out.println("****Wrong code. please enter code 1 to 4 only****"); } }while(ch!=4); } } ");

You might also like