import java.io.*;
class Cqueue {
int item[], f, r, size;
public Cqueue(int x) {
item = new int[x];
f = r = -1;
size = x;
}
public boolean isFull() {
if ((f == 0 && r == size - 1) || (r == f - 1)) {
return true;
} else {
return false;
}
}
public boolean isEmpty() {
if ((f == -1 && r == -1) || (r < f)) {
return true;
} else {
return false;
}
}
public void enqueue(int ele) {
if (isFull()) {
System.out.println("Queue is Full");
} else {
if (r == -1 && f == -1)
f = r = 0;
else
r++;
if (r == size)
r = 0;
item[r] = ele;
}
}
public int dequeue() {
if (isEmpty()) {
System.out.println("Queue is empty");
return (-1);
} else {
int x = item[f];
f++;
if (f == r + 1)
f = r = -1;
if (f == size) {
f = 0;
}
return x;
}
}
public int peek() {
if (isEmpty()) {
System.out.println("Queue is empty");
return (-1);
} else {
return (item[f]);
}
}
public void display() {
if (isEmpty()) {
System.out.println("Queue is empty");
} else {
int i = f, j = r;
{
while (i != j) {
System.out.print(item[i] + " ");
i++;
if (i == j) {
System.out.print(item[i]);
}
if (i == size) {
i = 0;
}
}
}
}
}
}
class Cqdemo {
public static void main(String args[]) throws IOException {
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
int ch, ele, size;
System.out.println("Enter size of queue");
size = Integer.parseInt(obj.readLine());
Cqueue c = new Cqueue(size);
do {
System.out.println("Menu");
System.out.println("1.Add element in queue");
System.out.println("2.Remove element from queue");
System.out.println("3.Display element in queue");
System.out.println("4.Peek element from queue");
System.out.println("5.Exit");
System.out.println("Enter choice");
ch = Integer.parseInt(obj.readLine());
switch (ch) {
case 1:
{
System.out.println("Enter element :");
int x1 = Integer.parseInt(obj.readLine());
c.enqueue(x1);
break;
}
case 2:
{
int x2 = c.dequeue();
System.out.println("Removed Element " + x2);
break;
}
case 3:
{
c.display();
break;
}
case 4:
{
int x3 = c.peek();
System.out.println("Peeked element" + x3);
break;
}
}
} while (ch != 5);
}
}
/*
Enter size of queue
5
Menu
1.Add element in queue
2.Remove element from queue
3.Display element in queue
4.Peek element from queue
5.Exit
Enter choice
1
Enter element :
10
Menu
1.Add element in queue
2.Remove element from queue
3.Display element in queue
4.Peek element from queue
5.Exit
Enter choice
1
Enter element :
20
Menu
1.Add element in queue
2.Remove element from queue
3.Display element in queue
4.Peek element from queue
5.Exit
Enter choice
1
Enter element :
30
Menu
1.Add element in queue
2.Remove element from queue
3.Display element in queue
4.Peek element from queue
5.Exit
Enter choice
1
Enter element :
40
Menu
1.Add element in queue
2.Remove element from queue
3.Display element in queue
4.Peek element from queue
5.Exit
Enter choice
1
Enter element :
50
Menu
1.Add element in queue
2.Remove element from queue
3.Display element in queue
4.Peek element from queue
5.Exit
Enter choice
1
Enter element :
60
Queue is Full
Menu
1.Add element in queue
2.Remove element from queue
3.Display element in queue
4.Peek element from queue
5.Exit
Enter choice
2
Removed Element 10
Menu
1.Add element in queue
2.Remove element from queue
3.Display element in queue
4.Peek element from queue
5.Exit
Enter choice
2
Removed Element 20
Menu
1.Add element in queue
2.Remove element from queue
3.Display element in queue
4.Peek element from queue
5.Exit
Enter choice
3
30 40 50Menu
1.Add element in queue
2.Remove element from queue
3.Display element in queue
4.Peek element from queue
5.Exit
Enter choice
4
Peeked element30
Menu
1.Add element in queue
2.Remove element from queue
3.Display element in queue
4.Peek element from queue
5.Exit
Enter choice
5
Process Exit...
*/