-
Notifications
You must be signed in to change notification settings - Fork 0
/
Circular_Queue1.java
103 lines (86 loc) · 2.37 KB
/
Circular_Queue1.java
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
// Circular-Queue Program
import java.util.*;
class cqueue {
int r;
int f;
int capacity;
int[] q;
cqueue(int size) {
capacity = size;
r = -1;
f = -1;
q = new int[capacity];
}
void enqueue(int a) {
if ((f == r + 1) || (f == 0 && r == capacity - 1)) {
System.out.println("Queue Overflow !!");
} else {
if (f == -1) {
f++;
}
r = (r + 1) % capacity;
q[r] = a;
}
}
int dequeue() {
if (f == -1) {
System.out.println("Queue Underflow !!");
return -1;
} else {
int y = q[f];
if (f == r) {
f = -1;
r = -1;
return y;
} else {
f = (f + 1) % capacity;
return y;
}
}
}
void display() {
if (f == -1) {
System.out.println("Queue is Empty..");
} else {
for (int i = f; i != r; i = (i + 1) % capacity) {
System.out.print(q[i] + " ");
}
System.out.print(q[r]);
}
}
}
class Main1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Size of Queue: ");
int size = sc.nextInt();
cqueue cq = new cqueue(size);
while (true) {
System.out.println("\nPress\n 1)Enqueue \n 2)Dequeue \n 3)Display \n 4) Exit");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("Enter Element to Enqueue: ");
int a = sc.nextInt();
cq.enqueue(a);
break;
case 2:
int y = cq.dequeue();
if (y != -1) {
System.out.println("Element Removed is: " + y);
}
break;
case 3:
System.out.println("Elements in Quueue are: ");
cq.display();
break;
case 4:
System.exit(0);
break;
default:
System.out.println("Invalid Input !!");
break;
}
}
}
}