
Queue is a linear data structure, which in Java is regarded as a collection that works according to the FIFO (first in, first out) principle.
In this article, we will see how to perform different queue operations such as enqueue, dequeue, queue front, queue size, and whether the queue is empty or not using Java programming language. We will use switch case to implement this application.
Show you some examples
Example 1
1 2 | 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]
|
Copy after login
Example-2
1 2 | In the same queue we perform Dequeue operation and remove element 2. Then the
updated list is - [6, 5, 8, 7, 3, 0]
|
Copy after login
Example 3
1 | Now we find the front of the queue. The front element is 6.
|
Copy after login
The Chinese translation of
Instance-4
is:
Instance-4
1 2 | 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
|
Copy after login
The Chinese translation of
Instance-5
is:
Instance-5
1 | Now we find if the queue is empty or not. And the result is “The queue is not empty ”.
|
Copy after login
grammar
To queue elements, we use the add() method
The following is the syntax of "add()"
In order to dequeue the elements in the queue, we use the remove() method
The following is the syntax of "remove()"
To view the previous element in the queue, we use the peek() method
The following is the syntax of "peek()"
To check whether the queue is empty, we use the isEmpty() method
The following is the syntax of "isEmpty()":
algorithm
Step-1 - Ask the user to enter the desired queue.
Step-2 − Display the menu.
Step 3 - Ask the user to enter their selections.
Step-4 - Use the switch box to go to the selection and perform the action.
Step 5 - Print the results.
Let us see the program to understand it clearly.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | import java.util.*;
public class Main{
public static void main(String args[]){
LinkedList<String> list = new LinkedList<>();
Scanner sc = new Scanner(System.in);
System.out. print ( "Enter the queue size : " );
int nbr = sc.nextInt();
System.out.println( "Enter the element : " );
sc.nextLine();
do {
list.add(sc.nextLine());
nbr--;
}
while (nbr > 0);
System.out.println( "The queue contains: " );
System.out.println(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!!" );
}
}
}
}
|
Copy after login
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | 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
|
Copy after login
In this article, we explored how to perform different queue operations in Java by using a menu-driven approach.
The above is the detailed content of Menu driver program written in Java for performing queue operations. For more information, please follow other related articles on the PHP Chinese website!