forked from Pranav-173/DSA
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQueue.java
More file actions
155 lines (129 loc) · 4.17 KB
/
Queue.java
File metadata and controls
155 lines (129 loc) · 4.17 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
import java.util.Scanner;
class Queue {
private int[] arr;
private int front;
private int rear;
private int capacity;
public Queue(int size) {
capacity = size;
arr = new int[capacity];
front = 0;
rear = -1;
}
public void enqueue(int item) {
if (isFull()) {
if (front > 0) {
int j = 0;
for (int i = front; i <= rear; i++) {
arr[j++] = arr[i];
}
rear = j - 1;
front = 0;
}
}
if (isFull()) {
System.out.println("Queue is Full. Can't Enqueue " + item);
return;
}
arr[++rear] = item;
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++];
System.out.println(item + " Dequeued from Queue.");
if (front > rear) {
front = 0;
rear = -1;
}
}
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 = front; i <= rear; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
public boolean isEmpty() {
return rear == -1;
}
public boolean isFull() {
return rear == capacity - 1;
}
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);
while (true) {
System.out.println("\n--- 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");
int 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...");
sc.close();
return;
default:
System.out.println("Invalid choice. Please enter a valid menu option.");
}
}
}
}