Tue Oct 19 2021
Queue
Java Programming1098 views
File Name: queue.java
import java.io.*;
import java.util.*;
class dataqueue {
public static void main(String args[]) {
Queue queue = new LinkedList();
Scanner scan = new Scanner(System.in);
int opt = 0;
while(opt != 4) {
System.out.println("1. Enqueue 2. Dequeue 3. Display 4. Exit");
System.out.println("Enter your choice:");
opt = Integer.parseInt(scan.nextLine());
switch(opt) {
case 1:
System.out.println("Please Enter Value in Queue:");
/* Enqueue data in Queue */
if(queue.offer(scan.nextLine()))
System.out.println("Data enqueued successfully!");
break;
case 2:
/* Dequeue data in Queue */
System.out.println(queue.poll()+" - dequeued successfully!");
break;
case 3:
System.out.println("Queue:");
System.out.println(queue);
break;
case 4:
System.out.println("Bye Bye!");
System.exit(0);
break;
default:
System.out.println("Invalid choice!");
}
}
}
}
/* Output */
1. Enqueue 2. Dequeue 3. Display 4. Exit
Enter your choice:
1
Please Enter Value in Queue:
5
Data enqueued successfully!
1. Enqueue 2. Dequeue 3. Display 4. Exit
Enter your choice:
1
Please Enter Value in Queue:
6
Data enqueued successfully!
1. Enqueue 2. Dequeue 3. Display 4. Exit
Enter your choice:
1
Please Enter Value in Queue:
7
Data enqueued successfully!
1. Enqueue 2. Dequeue 3. Display 4. Exit
Enter your choice:
1
Please Enter Value in Queue:
8
Data enqueued successfully!
1. Enqueue 2. Dequeue 3. Display 4. Exit
Enter your choice:
3
Queue:
[5, 6, 7, 8]
1. Enqueue 2. Dequeue 3. Display 4. Exit
Enter your choice:
2
5 - dequeued successfully!
1. Enqueue 2. Dequeue 3. Display 4. Exit
Enter your choice:
4
Bye Bye!
Reference:
Author:Geekboots