Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.07 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Queues {
  4.     private static Scanner sc = new Scanner(System.in);
  5.     private static int max = 5, size = 0, front = 0, rear = 0;
  6.     private static int[] arr = new int[max];
  7.  
  8.     public static void main(String[] args) {
  9.         Loop();
  10.     }
  11.  
  12.     private static void Enqueue() {
  13.         if (!isFull()) {
  14.             System.out.print("Enter a number to enqueue: ");
  15.             arr[rear] = sc.nextInt();
  16.             rear++;
  17.  
  18.             if (rear == max)
  19.                 rear = 0;
  20.  
  21.             size++;
  22.             System.out.println("Value enqueued");
  23.         }
  24.         else
  25.             System.out.println("Array is full");
  26.     }
  27.  
  28.     private static void Dequeue() {
  29.         if (!isEmpty()) {
  30.             arr[front] = -1;
  31.             front++;
  32.  
  33.             if (front == max)
  34.                 front = 0;
  35.             size--;
  36.             System.out.println("Value dequeued");
  37.         }
  38.         else
  39.             System.out.println("Array is empty");
  40.     }
  41.  
  42.     private static void Peek() {
  43.         if (!isEmpty()) {
  44.             System.out.println("============================");
  45.             for (int i = front; i < max; i++) {
  46.                 if (arr[i] == -1 || arr[i] == 0) continue;
  47.                 System.out.println("Number: " + arr[i]);
  48.             }
  49.  
  50.             for (int i = 0; i < front; i++) {
  51.                 if (arr[i] == -1 || arr[i] == 0) continue;
  52.                 System.out.println("Number: " + arr[i]);
  53.             }
  54.             System.out.println("============================");
  55.         }
  56.         else
  57.             System.out.println("Array is empty");
  58.     }
  59.  
  60.     private static void Clear() {
  61.         rear = size = front = 0;
  62.         arr = new int[max];
  63.     }
  64.  
  65.     private static boolean isFull() {
  66.         return max == size;
  67.     }
  68.  
  69.     private static boolean isEmpty() {
  70.         return size == 0;
  71.     }
  72.  
  73.     private static void Loop() {
  74.         while (true) {
  75.             System.out.println("What do you want to do?");
  76.             System.out.println("1 - Enqueue / Add");
  77.             System.out.println("2 - Dequeue / Remove");
  78.             System.out.println("3 - Peek / Show");
  79.             System.out.println("4 - Clear");
  80.             System.out.println("5 - Quit");
  81.             System.out.print("Choose one: ");
  82.             int choice = sc.nextInt();
  83.  
  84.             if (choice == 1)
  85.                 Enqueue();
  86.             else if (choice == 2)
  87.                 Dequeue();
  88.             else if (choice == 3)
  89.                 Peek();
  90.             else if (choice == 4)
  91.                 Clear();
  92.             else if (choice == 5)
  93.                 break;
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement