Skip to content

Commit ca482c6

Browse files
add linked queue
1 parent 208962e commit ca482c6

2 files changed

Lines changed: 138 additions & 0 deletions

File tree

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.examplehub.datastructures.queue;
2+
3+
import com.examplehub.datastructures.linkedlist.Node;
4+
5+
import java.util.StringJoiner;
6+
7+
public class LinkedQueue<E> {
8+
9+
/**
10+
* The front pointer of LinkedQueue.
11+
*/
12+
private Node<E> front;
13+
14+
/**
15+
* The rear pointer of LinkedQueue.
16+
*/
17+
private Node<E> rear;
18+
19+
/**
20+
* The number of elements in LinkedQueue.
21+
*/
22+
private int size;
23+
24+
public LinkedQueue() {
25+
front = rear = null;
26+
}
27+
28+
/**
29+
* Remove item from the front of LinkedQueue.
30+
*
31+
* @return the item at the front of LinkedQueue.
32+
* @throws IllegalAccessError if LinkedQueue is empty.
33+
*/
34+
public E dequeue() throws IllegalAccessException {
35+
if (size == 0) {
36+
throw new IllegalAccessException();
37+
}
38+
E retValue = front.data;
39+
if ((front = front.next) == null) {
40+
rear = null;
41+
}
42+
size--;
43+
return retValue;
44+
}
45+
46+
/**
47+
* Added item to the rear of LinkedQueue.
48+
*
49+
* @param item the item to be added.
50+
* @return {@code true} if add successfully.
51+
*/
52+
public boolean enqueue(E item) {
53+
Node<E> newNode = new Node<>(item);
54+
if (size == 0) {
55+
front = rear = newNode;
56+
} else {
57+
rear.next = newNode;
58+
rear = newNode;
59+
}
60+
size++;
61+
return true;
62+
}
63+
64+
/**
65+
* Test if this queue is empty or not.
66+
*
67+
* @return {@code true} if
68+
*/
69+
public boolean empty() {
70+
return size == 0;
71+
}
72+
73+
@Override
74+
public String toString() {
75+
StringJoiner joiner = new StringJoiner(", ", "[", "]");
76+
Node<E> temp = front;
77+
while (temp != null) {
78+
joiner.add(temp.data.toString());
79+
System.out.println(temp.data);
80+
temp = temp.next;
81+
}
82+
return joiner.toString();
83+
}
84+
85+
/**
86+
* Return the number of elements in this LinkedQueue.
87+
*
88+
* @return the number of elements in this LinkedQueue.
89+
*/
90+
public int size() {
91+
return size;
92+
}
93+
94+
/**
95+
* Clear all elements in the LinkedQueue.
96+
*/
97+
public void clear() {
98+
front = rear = null;
99+
size = 0;
100+
}
101+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.examplehub.datastructures.queue;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class LinkedQueueTest {
8+
@Test
9+
void testQueue() throws IllegalAccessException {
10+
LinkedQueue<String> queue = new LinkedQueue<>();
11+
assertTrue(queue.empty());
12+
assertEquals(0, queue.size());
13+
assertEquals("[]", queue.toString());
14+
15+
for (int i = 1; i <= 5; ++i) {
16+
queue.enqueue(i + "");
17+
}
18+
assertEquals(5, queue.size());
19+
assertEquals("[1, 2, 3, 4, 5]", queue.toString());
20+
21+
assertEquals("1", queue.dequeue());
22+
assertEquals("2", queue.dequeue());
23+
assertEquals("[3, 4, 5]", queue.toString());
24+
25+
assertTrue(queue.enqueue("1"));
26+
assertTrue(queue.enqueue("2"));
27+
assertEquals("[3, 4, 5, 1, 2]", queue.toString());
28+
29+
assertEquals("3", queue.dequeue());
30+
assertEquals("4", queue.dequeue());
31+
32+
queue.clear();
33+
assertEquals(queue.size(), 0);
34+
assertTrue(queue.empty());
35+
assertEquals("[]", queue.toString());
36+
}
37+
}

0 commit comments

Comments
 (0)