Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- //==============================QUEUE================================\\
- int maxi = 1000;
- int[] a = new int[maxi];
- int f, r;
- void initialize() {
- f = r = -1;
- }
- void enqueue(int val) {
- if (r == a[maxi] - 1) {
- System.out.println("Queue Overflow!");
- } else if (f == -1 && r == -1) {
- f = r = 1;
- a[r] = val;
- } else {
- r++;
- a[r] = val;
- }
- }
- void dequeue() {
- if (f == -1 && r == -1) {
- System.out.println("Queue is already empty!\n\n");
- } else if (f == r) {
- System.out.println("The dequeued element is: \n" + a[f]);
- f = r = -1;
- } else {
- System.out.println("The dequeued element is: " + a[f]);
- System.out.println("\n\n");
- f++;
- }
- }
- void call() {
- int x;
- if (f == -1 && r == -1) {
- System.out.println("The Queue is empty!\n\n");
- return;
- }
- System.out.println("The Contents of the Queue is: ");
- System.out.println("\n\n");
- for (x = f; x <= r; x++) {
- System.out.println(a[x] + " ");
- }
- }
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- Main obj = new Main();
- obj.initialize();
- char qw = 0;
- int value;
- System.out.println("\t------------------Queue------------------\n");
- System.out.println("Select below:\n");
- System.out.print("[a] - Enqueue\n[b] - Dequeue\n[c] - Exit\n");
- System.out.print("Select your choice: ");
- qw = sc.nextLine().charAt(0);
- switch (qw) {
- case 'a':
- System.out.print("Enter the element to be enqueued: ");
- value = sc.nextInt();
- sc.nextLine();
- obj.enqueue(value);
- break;
- case 'b':
- obj.dequeue();
- break;
- case 'c':
- obj.call();
- break;
- default:
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement