-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.java
More file actions
113 lines (97 loc) · 2.81 KB
/
Copy pathqueue.java
File metadata and controls
113 lines (97 loc) · 2.81 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
import java.util.Scanner;
class queue3 {
int size;
char st[];
int front;
int rear;
queue3(int size) {
this.size = size;
front = 0;
rear = -1;
st = new char[size];
}
boolean isEmpty() {
return front > rear;
}
boolean isFull() {
return rear == size - 1;
}
void nQueue(char ch) {
if (isFull()) {
System.out.println("Overflow. The Queue is full");
} else {
rear++;
st[rear] = ch;
}
}
void dQueue() {
if (isEmpty()) {
System.out.println("Underflow. The Queue is empty");
} else {
System.out.println("Dequeued: " + st[front]);
front++;
if (front > rear) {
front = 0;
rear = -1;
}
}
}
void size() {
if (isEmpty()) {
System.out.println("The size of the Queue is 0");
} else {
System.out.println("The size of the Queue is " + (rear - front + 1));
}
}
void display() {
if (isEmpty()) {
System.out.println("Queue is empty");
return;
}
System.out.println("The elements in the Queue are:");
for (int i = front; i <= rear; i++) {
System.out.println(st[i]);
}
}
public static void main(String[] args) {
int s;
System.out.println("Enter size of queue ");
Scanner sc = new Scanner(System.in);
s = sc.nextInt();
queue3 obj = new queue3(s);
while (true) {
System.out.println("\n==== Main Menu ====");
System.out.println("1. Insert Char to Queue");
System.out.println("2. Delete Char from Queue ");
System.out.println("3. Check size of Queue");
System.out.println("4. Display all elements");
System.out.println("5. Exit");
System.out.print("Enter your choice: \n");
int choice = sc.nextInt();
if (choice == 5) {
System.out.println("Exiting Queue Program!!");
break;
}
switch (choice) {
case 1:
System.out.println("Enter a char to nqueue into the queue");
String input = sc.next();
char ch = input.charAt(0);
obj.nQueue(ch);
;
break;
case 2:
obj.dQueue();
break;
case 3:
obj.size();
break;
case 4:
obj.display();
break;
default:
System.out.println("Enter a correct input");
}
}
}
}