
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Menu Driven Program to Perform Queue Operation
Queue is a linear data structure, in Java considered as a collection whch works on the principle of FIFO (First In First Out).
In this article we will see how to perform different queue operations like Enqueue, Dequeue, front of the queue, size of the queue, is queue empty or not using Java programming language. We will be implementing the application using a switch case.
To show you some instances
Instance-1
Suppose we have entered a queue of size 6 with elements [2, 6, 5, 8, 7, 3]. Then we will perform the Enqueue operation and add element 0. So the updated list is - [2, 6, 5, 8, 7, 3, 0]
Instance-2
In the same queue we perform Dequeue operation and remove element 2. Then the updated list is - [6, 5, 8, 7, 3, 0]
Instance-3
Now we find the front of the queue. The front element is 6.
Instance-4
Suppose we have created an array containing 6 elements and array elements are [2,4,6,2,6,8]. Now we will print the smallest element in an array. And hence result will be. Smallest element present in given array: 2
Instance-5
Now we find if the queue is empty or not. And the result is “The queue is not empty”.
Syntax
To enqueue the element in the queue we use add() method
Following is the syntax for “add()”
list.add(s);
To dequeue the element in the queue we use remove() method
Following is the syntax for “remove()”
list.remove(s);
To view the front element in the queue we use peek() method
Following is the syntax for “peek()”
list.peek();
To chech if queue is empty or not we use isEmpty() method
Following is the syntax for ““isEmpty()”
list.isEmpty();
Algorithm
Step-1 − Ask the user to input the desired Queue.
Step-2 − Display the menu.
Step-3 − Ask the user to enter their choice.
Step-4 − Use a switch case to go to the choice and perform the operation.
Step-5 − Print the result.
Let’s see the program to understand it clearly.
Example
import java.util.*; public class Main{ public static void main(String args[]){ LinkedList<String> list = new LinkedList<>(); //declare your list Scanner sc = new Scanner(System.in); //create a scanner class object System.out.print("Enter the queue size : "); int nbr = sc.nextInt(); //read the number of element System.out.println("Enter the element : "); sc.nextLine(); do { list.add(sc.nextLine()); nbr--;//decrement the index } while (nbr > 0); //repeat until the index will be 0 System.out.println("The queue contains: "); System.out.println(list);//print your list mainLoop: while (true) { Scanner sc1 = new Scanner(System.in); System.out.println("\n***Menu***"); System.out.println("1. Perform Enqueue operation"); System.out.println("2. Perform Dequeue operation"); System.out.println("3. Prints the front of the queue"); System.out.println("4. Print the size of the queue"); System.out.println("5. Check if the queue is empty"); System.out.println("6. Terminate the program"); System.out.println("Enter action number (1-6): "); int command = sc.nextInt(); switch(command){ case 1: System.out.print("Enter the element you want to enter in the queue : "); int num = sc.nextInt(); String s = Integer.toString(num); list.add(s); System.out.println("updated list is: "); System.out.println(list); break; case 2: list.remove(); System.out.println("updated list is: "); System.out.println(list); break; case 3: System.out.println("The front element is " + list.peek()); break; case 4: System.out.println("The queue size is " + list.size()); break; case 5: if (list.isEmpty()) { System.out.println("The queue is empty"); } else { System.out.println("The queue is not empty"); } break; case 6: System.out.println("Program terminated"); break mainLoop; default: System.out.println("Wrong choice!!"); } } } }
Output
Enter the queue size : 4 Enter the element : 1 2 3 4 The queue contains: [1 , 2, 3, 4] ***Menu*** 1. Perform Enqueue operation 2. Perform Dequeue operation 3. Prints the front of the queue 4. Print the size of the queue 5. Check if the queue is empty 6. Terminate the program Enter action number (1-6): 1 Enter the element you want to enter in the queue : 5 updated list is: [1 , 2, 3, 4, 5] ***Menu*** 1. Perform Enqueue operation 2. Perform Dequeue operation 3. Prints the front of the queue 4. Print the size of the queue 5. Check if the queue is empty 6. Terminate the program Enter action number (1-6): 2 updated list is: [2, 3, 4, 5] ***Menu*** 1. Perform Enqueue operation 2. Perform Dequeue operation 3. Prints the front of the queue 4. Print the size of the queue 5. Check if the queue is empty 6. Terminate the program Enter action number (1-6): 3 The front element is 2 ***Menu*** 1. Perform Enqueue operation 2. Perform Dequeue operation 3. Prints the front of the queue 4. Print the size of the queue 5. Check if the queue is empty 6. Terminate the program Enter action number (1-6): 4 The queue size is 4 ***Menu*** 1. Perform Enqueue operation 2. Perform Dequeue operation 3. Prints the front of the queue 4. Print the size of the queue 5. Check if the queue is empty 6. Terminate the program Enter action number (1-6): 5 The queue is not empty ***Menu*** 1. Perform Enqueue operation 2. Perform Dequeue operation 3. Prints the front of the queue 4. Print the size of the queue 5. Check if the queue is empty 6. Terminate the program Enter action number (1-6): 6 Program terminated
In this article, we explored how to perform different queue operations in Java by using menu driven approach.
- Related Articles
- Java Menu Driven Program to Perform Array Operation
- Java Menu Driven Program to Perform Matrix Operation
- Java Menu Driven Program to Perform Basic String Operations
- Java Menu Driven Program to Check Type of a Number
- Java Menu Driven Program to Determine Cost of New Membership
- Java Program to perform XOR operation on BigInteger
- Java Program to perform AND operation on BigInteger
- Java Menu Driven Program to Check Positive Negative or Odd Even Number
- JAVA Menu Driven Program to Check Character is String, Number or Special Character
- Menu Driven C++ Program for a Simple Calculator
- Program to perform excel spreadsheet operation in Python?
- C++ Program to Perform Addition Operation Using Bitwise Operators
- C program to perform union operation on two arrays
- C program to perform intersection operation on two arrays
- Program to perform XOR operation in an array using Python
