Skip to content

Commit 6284d72

Browse files
committed
Added Circular Queue data structure
1 parent 883a050 commit 6284d72

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package com.thealgorithms.datastructures.queues;
2+
3+
/**
4+
* Implementation of a Double Ended Queue (Deque) using Array
5+
* A deque allows insertion and deletion from both front and rear ends.
6+
* Author: Sonu Dodwariya (Hacktoberfest 2025)
7+
*/
8+
9+
public class DequeUsingArray {
10+
11+
private int[] arr;
12+
private int front;
13+
private int rear;
14+
private int size;
15+
16+
// Constructor
17+
public DequeUsingArray(int size) {
18+
arr = new int[size];
19+
front = -1;
20+
rear = 0;
21+
this.size = size;
22+
}
23+
24+
// Check if deque is full
25+
public boolean isFull() {
26+
return (front == 0 && rear == size - 1) || (front == rear + 1);
27+
}
28+
29+
// Check if deque is empty
30+
public boolean isEmpty() {
31+
return (front == -1);
32+
}
33+
34+
// Insert element at front
35+
public void insertFront(int key) {
36+
if (isFull()) {
37+
System.out.println("Deque is full!");
38+
return;
39+
}
40+
41+
// First element
42+
if (front == -1) {
43+
front = 0;
44+
rear = 0;
45+
} else if (front == 0)
46+
front = size - 1;
47+
else
48+
front = front - 1;
49+
50+
arr[front] = key;
51+
}
52+
53+
// Insert element at rear
54+
public void insertRear(int key) {
55+
if (isFull()) {
56+
System.out.println("Deque is full!");
57+
return;
58+
}
59+
60+
// First element
61+
if (front == -1) {
62+
front = 0;
63+
rear = 0;
64+
} else if (rear == size - 1)
65+
rear = 0;
66+
else
67+
rear = rear + 1;
68+
69+
arr[rear] = key;
70+
}
71+
72+
// Delete element at front
73+
public void deleteFront() {
74+
if (isEmpty()) {
75+
System.out.println("Deque is empty!");
76+
return;
77+
}
78+
79+
// Single element
80+
if (front == rear) {
81+
front = -1;
82+
rear = -1;
83+
} else if (front == size - 1)
84+
front = 0;
85+
else
86+
front = front + 1;
87+
}
88+
89+
// Delete element at rear
90+
public void deleteRear() {
91+
if (isEmpty()) {
92+
System.out.println("Deque is empty!");
93+
return;
94+
}
95+
96+
// Single element
97+
if (front == rear) {
98+
front = -1;
99+
rear = -1;
100+
} else if (rear == 0)
101+
rear = size - 1;
102+
else
103+
rear = rear - 1;
104+
}
105+
106+
// Get front element
107+
public int getFront() {
108+
if (isEmpty()) {
109+
System.out.println("Deque is empty!");
110+
return -1;
111+
}
112+
return arr[front];
113+
}
114+
115+
// Get rear element
116+
public int getRear() {
117+
if (isEmpty() || rear < 0) {
118+
System.out.println("Deque is empty!");
119+
return -1;
120+
}
121+
return arr[rear];
122+
}
123+
124+
// Display all elements
125+
public void display() {
126+
if (isEmpty()) {
127+
System.out.println("Deque is empty!");
128+
return;
129+
}
130+
131+
System.out.print("Deque elements: ");
132+
int i = front;
133+
while (true) {
134+
System.out.print(arr[i] + " ");
135+
if (i == rear)
136+
break;
137+
i = (i + 1) % size;
138+
}
139+
System.out.println();
140+
}
141+
142+
// Main method for testing
143+
public static void main(String[] args) {
144+
DequeUsingArray dq = new DequeUsingArray(5);
145+
146+
dq.insertRear(10);
147+
dq.insertRear(20);
148+
dq.insertFront(5);
149+
dq.insertFront(2);
150+
dq.display();
151+
152+
System.out.println("Front element: " + dq.getFront());
153+
System.out.println("Rear element: " + dq.getRear());
154+
155+
dq.deleteFront();
156+
dq.deleteRear();
157+
dq.display();
158+
}
159+
}

0 commit comments

Comments
 (0)