-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularQueue.java
More file actions
108 lines (94 loc) · 2.93 KB
/
Copy pathCircularQueue.java
File metadata and controls
108 lines (94 loc) · 2.93 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
package queque.dsa.lab;
public class CircularQueue {
private int capacity;
private int[] queueArray;
private int front;
private int rear;
private int size;
// Constructor to initialize the queue
public CircularQueue(int capacity) {
this.capacity = capacity;
queueArray = new int[capacity];
front = -1;
rear = -1;
size = 0;
}
// Method to check if the queue is empty
public boolean isEmpty() {
return size == 0;
}
// Method to check if the queue is full
public boolean isFull() {
return size == capacity;
}
// Method to add an element to the rear of the queue
public void enqueue(int item) {
if (isFull()) {
System.out.println("Queue is full. Cannot enqueue.");
return;
}
if (front == -1) {
front = 0;
}
rear = (rear + 1) % capacity;
queueArray[rear] = item;
size++;
System.out.println(item + " enqueued to the queue");
}
// Method to remove an element from the front of the queue
public int dequeue() {
if (isEmpty()) {
System.out.println("Queue is empty. Cannot dequeue.");
return -1;
}
int dequeuedItem = queueArray[front];
if (front == rear) {
front = -1;
rear = -1;
} else {
front = (front + 1) % capacity;
}
size--;
return dequeuedItem;
}
// Method to get the front element of the queue without removing it
public int peek() {
if (isEmpty()) {
System.out.println("Queue is empty.");
return -1;
}
return queueArray[front];
}
// Method to get the size of the queue
public int size() {
return size;
}
// Method to display the elements of the queue
public void display() {
if (isEmpty()) {
System.out.println("Queue is empty.");
return;
}
System.out.print("Queue: ");
int i = front;
do {
System.out.print(queueArray[i] + " ");
i = (i + 1) % capacity;
} while (i != (rear + 1) % capacity);
System.out.println();
}
public static void main(String[] args) {
CircularQueue queue = new CircularQueue(5);
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
queue.enqueue(40);
queue.enqueue(50);
queue.enqueue(60); // Queue is full, this will not be enqueued
queue.display(); // Display the queue
System.out.println("Dequeued element: " + queue.dequeue());
System.out.println("Front element: " + queue.peek());
System.out.println("Queue size: " + queue.size());
queue.display(); // Display the queue after dequeue
}
}