-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCircularQueue.txt
More file actions
82 lines (76 loc) · 1.28 KB
/
Copy pathCircularQueue.txt
File metadata and controls
82 lines (76 loc) · 1.28 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
//**CIRCULAR QUEUE USING JAVA**//
public class CircularQueue
{
public static void main(String args[])
{
Queue b = new Queue();
b.enqueue(10);
b.enqueue(20);
b.enqueue(30);
b.enqueue(40);
b.enqueue(50);
b.dequeue();
b.dequeue();
b.enqueue(70);
b.enqueue(80);
b.display();
b.peak();
}
}
class Queue
{
int front = -1;
int rear = -1;
int size = 5;
int q[] = new int[size];
public void enqueue(int x)
{
if(front==-1 && rear==-1)
{
front = 0;
rear = 0;
q[rear] = x;
}
else if((rear+1)%size == front)
{
System.out.println("Queue is full");
}
else
{
rear = (rear+1)%size;
q[rear] = x;
}
}
public void dequeue()
{
if(front==-1 && rear==-1)
{
System.out.println("Queue is empty");
}
else if(front == rear)
{
System.out.println("Dequeue at "+front+" "+q[front]);
front = -1;
rear = -1;
}
else
{
System.out.println("Dequeue at "+front+" "+q[front]);
front = (front+1)%size;
}
}
public void display()
{
int i=front;
while(i!=rear)
{
System.out.println(q[i]);
i=(i+1)%size;
}
System.out.println(q[i]);
}
public void peak()
{
System.out.println("Peak "+q[front]);
}
}