Skip to content

Commit 84ca022

Browse files
author
Lord-of-Algorithms
committed
Add Queue implementations
1 parent 665809f commit 84ca022

File tree

5 files changed

+343
-0
lines changed

5 files changed

+343
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package lineards.queue;
2+
3+
import java.util.NoSuchElementException;
4+
5+
/**
6+
* Implements a queue that automatically adjusts its capacity based on the number of elements.
7+
*/
8+
public class DynamicArrayQueue implements Queue {
9+
private char[] data;
10+
private int front;
11+
private int rear;
12+
private int size;
13+
14+
public DynamicArrayQueue(int capacity) {
15+
if (capacity < 1) {
16+
throw new IllegalArgumentException("Capacity must be at least 1");
17+
}
18+
data = new char[capacity];
19+
front = 0;
20+
rear = -1;
21+
size = 0;
22+
}
23+
24+
/**
25+
* Checks if the queue is empty.
26+
*
27+
* @return true if the queue has no elements, false otherwise.
28+
*/
29+
public boolean isEmpty() {
30+
return size == 0;
31+
}
32+
33+
/**
34+
* Adds a character to the rear of the queue. Resizes if full.
35+
*
36+
* @param value The character to add.
37+
*/
38+
public void enqueue(char value) {
39+
if (size == data.length) {
40+
// Double the size of the array when
41+
// the queue is full
42+
resize(2 * data.length);
43+
}
44+
rear = (rear + 1) % data.length;
45+
data[rear] = value;
46+
size++;
47+
}
48+
49+
/**
50+
* Removes and returns the character at the front of the queue.
51+
*
52+
* @return The character at the front of the queue.
53+
* @throws NoSuchElementException if the queue is empty.
54+
*/
55+
public char dequeue() {
56+
if (isEmpty()) {
57+
throw new NoSuchElementException("Queue is empty");
58+
}
59+
char value = data[front];
60+
front = (front + 1) % data.length;
61+
size--;
62+
63+
// Reduce the size of the array when the queue is 1/4 full
64+
if (size > 0 && size == data.length / 4) {
65+
resize(Math.max(data.length / 2, 10)); // Ensure the capacity doesn't get too small
66+
}
67+
68+
return value;
69+
}
70+
71+
/**
72+
* Returns the character at the front of the queue without removing it.
73+
*/
74+
public char peek() {
75+
if (isEmpty()) {
76+
throw new NoSuchElementException("Queue is empty");
77+
}
78+
return data[front];
79+
}
80+
81+
/**
82+
* Resizes the underlying array holding the elements of the queue.
83+
*
84+
* @param newCapacity The new capacity for the queue.
85+
*/
86+
private void resize(int newCapacity) {
87+
char[] newData = new char[newCapacity];
88+
for (int i = 0; i < size; i++) {
89+
newData[i] = data[(front + i) % data.length];
90+
}
91+
data = newData;
92+
front = 0;
93+
rear = size - 1;
94+
}
95+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package lineards.queue;
2+
3+
import java.util.NoSuchElementException;
4+
5+
/**
6+
* A queue implementation using a singly linked list. This implementation allows for dynamic
7+
* memory allocation, growing as needed based on the queue's usage. It does not impose a fixed capacity.
8+
*/
9+
public class LinkedListQueue implements Queue {
10+
11+
/**
12+
* Node in a linked list, holding a character and a reference to the next node.
13+
*/
14+
private static class Node {
15+
final char data;
16+
Node next;
17+
18+
Node(char data) {
19+
this.data = data;
20+
}
21+
}
22+
23+
private Node front;
24+
private Node rear;
25+
26+
/**
27+
* Checks if the queue is empty.
28+
*
29+
* @return true if the queue has no elements, false otherwise.
30+
*/
31+
public boolean isEmpty() {
32+
return front == null;
33+
}
34+
35+
/**
36+
* Adds a character to the rear of the queue.
37+
*
38+
* @param value The character to add.
39+
*/
40+
public void enqueue(char value) {
41+
Node newNode = new Node(value);
42+
if (isEmpty()) {
43+
front = newNode;
44+
} else {
45+
rear.next = newNode;
46+
}
47+
rear = newNode;
48+
}
49+
50+
/**
51+
* Removes and returns the character at the front of the queue.
52+
*
53+
* @return The character at the front of the queue.
54+
* @throws NoSuchElementException if the queue is empty.
55+
*/
56+
public char dequeue() {
57+
if (isEmpty()) {
58+
throw new NoSuchElementException("Queue is empty");
59+
}
60+
char data = front.data;
61+
front = front.next;
62+
if (front == null) {
63+
rear = null;
64+
}
65+
return data;
66+
}
67+
68+
/**
69+
* Returns the character at the front of the queue without removing it.
70+
*/
71+
public char peek() {
72+
if (isEmpty()) {
73+
throw new NoSuchElementException("Queue is empty");
74+
}
75+
return front.data;
76+
}
77+
}

src/lineards/queue/Queue.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package lineards.queue;
2+
3+
import java.util.NoSuchElementException;
4+
5+
/**
6+
* Defines the interface for a queue.
7+
*/
8+
interface Queue {
9+
/**
10+
* Checks if the queue is empty.
11+
*
12+
* @return true if the queue has no elements, false otherwise.
13+
*/
14+
boolean isEmpty();
15+
16+
/**
17+
* Adds a character to the rear of the queue.
18+
*
19+
* @param value The character to add.
20+
*/
21+
void enqueue(char value);
22+
23+
/**
24+
* Removes and returns the character at the front of the queue.
25+
*
26+
* @return The character at the front of the queue.
27+
* @throws NoSuchElementException if the queue is empty.
28+
*/
29+
char dequeue();
30+
31+
/**
32+
* Returns the character at the front of the queue without removing it.
33+
*/
34+
char peek();
35+
}

src/lineards/queue/QueueMain.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package lineards.queue;
2+
3+
/**
4+
* Demonstrates the functionalities of different queue implementations.
5+
*/
6+
public class QueueMain {
7+
8+
public static void main(String[] args) {
9+
// Demo for StaticArrayQueue
10+
System.out.println("Static Array Queue Demo:");
11+
StaticArrayQueue staticQueue = new StaticArrayQueue(5);
12+
demoQueue(staticQueue);
13+
14+
// Demo for DynamicArrayQueue
15+
System.out.println("\nDynamic Array Queue Demo:");
16+
DynamicArrayQueue dynamicQueue = new DynamicArrayQueue(5);
17+
demoQueue(dynamicQueue);
18+
19+
// Demo for LinkedListQueue
20+
System.out.println("\nLinked List Queue Demo:");
21+
LinkedListQueue linkedListQueue = new LinkedListQueue();
22+
demoQueue(linkedListQueue);
23+
}
24+
25+
/**
26+
* Demonstrates basic queue operations like enqueue, dequeue, and peek on a queue.
27+
*
28+
* @param queue The queue to demonstrate.
29+
*/
30+
private static void demoQueue(Queue queue) {
31+
try {
32+
queue.enqueue('A');
33+
queue.enqueue('B');
34+
queue.enqueue('C');
35+
System.out.println("Enqueuing A, B, and C...");
36+
System.out.println("Peek: " + queue.peek());
37+
System.out.println("Dequeue: " + queue.dequeue());
38+
System.out.println("Peek: " + queue.peek());
39+
System.out.println("Dequeue: " + queue.dequeue());
40+
System.out.println("Peek: " + queue.peek());
41+
System.out.println("Empty: " + queue.isEmpty());
42+
System.out.println("Dequeue: " + queue.dequeue());
43+
System.out.println("Empty: " + queue.isEmpty());
44+
45+
} catch (Exception e) {
46+
System.out.println(e.getMessage());
47+
}
48+
}
49+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package lineards.queue;
2+
3+
import java.util.NoSuchElementException;
4+
5+
/**
6+
* Represents a fixed-size queue implemented using a static array.
7+
* The queue uses a circular array mechanism to manage elements without
8+
* shifting them physically in the array. This allows for O(1) complexity for enqueue
9+
* and dequeue operations.
10+
*/
11+
public class StaticArrayQueue implements Queue {
12+
private final char[] data; // Array to store queue elements.
13+
private int front;
14+
private int rear;
15+
private int size;
16+
17+
/**
18+
* Constructs a new Queue with a specified capacity.
19+
*/
20+
public StaticArrayQueue(int capacity) {
21+
if (capacity < 1) {
22+
throw new IllegalArgumentException("Capacity must be at least 1");
23+
}
24+
data = new char[capacity];
25+
front = 0;
26+
rear = -1;
27+
size = 0;
28+
}
29+
30+
/**
31+
* Checks if the queue is empty.
32+
*
33+
* @return true if the queue has no elements, false otherwise.
34+
*/
35+
public boolean isEmpty() {
36+
return size == 0;
37+
}
38+
39+
/**
40+
* Checks if the queue is full.
41+
*
42+
* @return true if the queue has reached its current capacity, false otherwise.
43+
*/
44+
public boolean isFull() {
45+
return size == data.length;
46+
}
47+
48+
/**
49+
* Adds a character to the rear of the queue.
50+
*
51+
* @param value The character to add.
52+
*/
53+
public void enqueue(char value) {
54+
if (isFull()) {
55+
throw new IllegalStateException("Queue is full");
56+
}
57+
rear = (rear + 1) % data.length;
58+
data[rear] = value;
59+
size++;
60+
}
61+
62+
/**
63+
* Removes and returns the character at the front of the queue.
64+
*
65+
* @return The character at the front of the queue.
66+
* @throws NoSuchElementException if the queue is empty.
67+
*/
68+
public char dequeue() {
69+
if (isEmpty()) {
70+
throw new NoSuchElementException("Queue is empty");
71+
}
72+
char value = data[front];
73+
front = (front + 1) % data.length;
74+
size--;
75+
return value;
76+
}
77+
78+
/**
79+
* Returns the character at the front of the queue without removing it.
80+
*/
81+
public char peek() {
82+
if (isEmpty()) {
83+
throw new NoSuchElementException("Queue is empty");
84+
}
85+
return data[front];
86+
}
87+
}

0 commit comments

Comments
 (0)