forked from Pranav-173/DSA
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCircularQueue.java
More file actions
157 lines (132 loc) · 4.27 KB
/
CircularQueue.java
File metadata and controls
157 lines (132 loc) · 4.27 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import java.util.Scanner;
class Queue {
private int[] arr;
private int front;
private int rear;
private int capacity;
private int count;
public Queue(int size) {
if (size <= 0) {
throw new IllegalArgumentException("Queue size must be greater than 0");
}
capacity = size;
arr = new int[capacity];
front = 0;
rear = -1;
count = 0;
}
public void enqueue(int item) {
if (isFull()) {
System.out.println("Queue is Full. Can't Enqueue " + item);
return;
}
rear = (rear + 1) % capacity;
arr[rear] = item;
count++;
System.out.println(item + " Enqueued to Queue.");
}
public void dequeue() {
if (isEmpty()) {
System.out.println("Queue is Empty. Can't Dequeue.");
return;
}
int item = arr[front];
front = (front + 1) % capacity;
count--;
if (count == 0) {
front = 0;
rear = -1;
}
System.out.println(item + " Dequeued from Queue.");
}
public void peek() {
if (isEmpty()) {
System.out.println("Queue is Empty.");
return;
}
System.out.println("Front element is: " + arr[front]);
}
public void display() {
if (isEmpty()) {
System.out.println("Queue is Empty.");
return;
}
System.out.print("Queue elements: ");
for (int i = 0; i < count; i++) {
int index = (front + i) % capacity;
System.out.print(arr[index] + " ");
}
System.out.println();
}
public boolean isEmpty() {
return count == 0;
}
public boolean isFull() {
return count == capacity;
}
}
public class CircularQueue {
private static int readInt(Scanner sc, String prompt) {
while (true) {
System.out.print(prompt);
if (sc.hasNextInt()) {
return sc.nextInt();
}
System.out.println("Invalid input. Please enter an integer.");
sc.next();
}
}
private static int readPositiveInt(Scanner sc, String prompt) {
while (true) {
int value = readInt(sc, prompt);
if (value > 0) {
return value;
}
System.out.println("Invalid input. Please enter an integer greater than 0.");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int capacity = readPositiveInt(sc, "Enter Queue size: ");
Queue queue = new Queue(capacity);
int choice;
do {
System.out.println("\n--- Circular Queue Operations ---");
System.out.println("1. Enqueue");
System.out.println("2. Dequeue");
System.out.println("3. Peek");
System.out.println("4. Display");
System.out.println("5. Is Full?");
System.out.println("6. Is Empty?");
System.out.println("7. Exit");
choice = readInt(sc, "Enter your choice: ");
switch (choice) {
case 1:
int element = readInt(sc, "Enter element to Enqueue: ");
queue.enqueue(element);
break;
case 2:
queue.dequeue();
break;
case 3:
queue.peek();
break;
case 4:
queue.display();
break;
case 5:
System.out.println(queue.isFull() ? "The Queue is Full." : "The Queue is NOT Full.");
break;
case 6:
System.out.println(queue.isEmpty() ? "The Queue is Empty." : "The Queue is NOT Empty.");
break;
case 7:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice. Please enter a valid menu option.");
}
} while (choice != 7);
sc.close();
}
}