Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Queues {
- private static Scanner sc = new Scanner(System.in);
- private static int max = 5, size = 0, front = 0, rear = 0;
- private static int[] arr = new int[max];
- public static void main(String[] args) {
- Loop();
- }
- private static void Enqueue() {
- if (!isFull()) {
- System.out.print("Enter a number to enqueue: ");
- arr[rear] = sc.nextInt();
- rear++;
- if (rear == max)
- rear = 0;
- size++;
- System.out.println("Value enqueued");
- }
- else
- System.out.println("Array is full");
- }
- private static void Dequeue() {
- if (!isEmpty()) {
- arr[front] = -1;
- front++;
- if (front == max)
- front = 0;
- size--;
- System.out.println("Value dequeued");
- }
- else
- System.out.println("Array is empty");
- }
- private static void Peek() {
- if (!isEmpty()) {
- System.out.println("============================");
- for (int i = front; i < max; i++) {
- if (arr[i] == -1 || arr[i] == 0) continue;
- System.out.println("Number: " + arr[i]);
- }
- for (int i = 0; i < front; i++) {
- if (arr[i] == -1 || arr[i] == 0) continue;
- System.out.println("Number: " + arr[i]);
- }
- System.out.println("============================");
- }
- else
- System.out.println("Array is empty");
- }
- private static void Clear() {
- rear = size = front = 0;
- arr = new int[max];
- }
- private static boolean isFull() {
- return max == size;
- }
- private static boolean isEmpty() {
- return size == 0;
- }
- private static void Loop() {
- while (true) {
- System.out.println("What do you want to do?");
- System.out.println("1 - Enqueue / Add");
- System.out.println("2 - Dequeue / Remove");
- System.out.println("3 - Peek / Show");
- System.out.println("4 - Clear");
- System.out.println("5 - Quit");
- System.out.print("Choose one: ");
- int choice = sc.nextInt();
- if (choice == 1)
- Enqueue();
- else if (choice == 2)
- Dequeue();
- else if (choice == 3)
- Peek();
- else if (choice == 4)
- Clear();
- else if (choice == 5)
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement