-
Notifications
You must be signed in to change notification settings - Fork 0
/
circularQueue.java
67 lines (60 loc) · 1.08 KB
/
circularQueue.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
public class circularQueue
{
int n=10;
Object A[]=new Object[n];
int front=-1;
int rear=-1;
public boolean isEmpty()
{
if (front==-1 && rear==-1) {
return true;
}
else return false;
}
public void enQueue(Object o)
{
if((rear+1)%n==front)
{
System.out.println("Queue is full");
return;
}
else if(isEmpty())
{
front=rear=0;
}
else rear++;
A[rear]=o;
}
public void deQueue()
{
if(isEmpty())
{ System.out.println("Queue is Empty");
}
else if(front==rear) // Only one element is present
{
front=-1;
rear=-1;
}
else front=(front+1)%n;
}
public Object Front()
{
if(isEmpty())
{
System.out.println("Queue is Empty");
return -1;
}
else return A[front];
}
public static void main(String [] args)
{
circularQueue qe=new circularQueue();
qe.enQueue(5);
qe.enQueue(4);
qe.enQueue(3);qe.enQueue(2);
qe.enQueue(1);
qe.deQueue();
qe.deQueue();
System.out.println(qe.Front());
}
}