-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularQueue.java
More file actions
116 lines (97 loc) · 2.8 KB
/
Copy pathCircularQueue.java
File metadata and controls
116 lines (97 loc) · 2.8 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
import java.util.Scanner;
public class CircularQueue {
int size, front, rear;
int[] cqueue;
//constructor
public CircularQueue(int capacity) {
size = capacity;
cqueue = new int[size];
front = -1;
rear = -1;
}
//Insert
void cqinsert(int val) {
if (front == (rear + 1) % size) {
System.out.println("Overflow");
}
else {
if (front == -1 && rear == -1) {
front = 0;
rear = 0;
cqueue[rear] = val;
}
else {
rear = (rear + 1) % size;
cqueue[rear] = val;
}
System.out.println("Inserted: " + val);
}
}
//Delete
void cqdelete() {
if (front == -1) {
System.out.println("Underflow");
}
else {
System.out.println("Deleted: " + cqueue[front]);
if (front == rear) {
front = -1;
rear = -1;
}
else {
front = (front + 1) % size;
}
}
}
//Display
void cqdisplay() {
if (front == -1) {
System.out.println("Queue is empty");
}
else {
System.out.println("Queue elements are:");
int i = front;
while (i != rear) {
System.out.print(cqueue[i] + " ");
i = (i + 1) % size;
}
System.out.println(cqueue[rear]);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter queue size: ");
int n = sc.nextInt();
CircularQueue q = new CircularQueue(n);
int ch, val;
System.out.println("\nSelect from the listed options-");
while(true){
System.out.println("\n--- MENU ---");
System.out.println("1. Insert");
System.out.println("2. Delete");
System.out.println("3. Display");
System.out.println("4. Exit");
System.out.println("Enter your choice:");
ch = sc.nextInt();
switch (ch) {
case 1:
System.out.println("Enter element to be inserted:");
val = sc.nextInt();
q.cqinsert(val);
break;
case 2:
q.cqdelete();
break;
case 3:
q.cqdisplay();
break;
case 4:
System.out.println("Program Terminated !!");
sc.close();
return;
default:
System.out.println("OUTSIDE CASE VALUE");
}
}
}
}