File tree Expand file tree Collapse file tree
src/main/java/com/thealgorithms/datastructures/queues Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22
33public class ThreadSafeQueue <T > {
44
5- private static class Node <T > {
6- T data ;
7- Node <T > next ;
5+ private static class Node <T > {
6+ T data ;
7+ Node <T > next ;
88
9- Node (T data ) {
10- this .data = data ;
11- }
12- }
9+ Node (T data ) {
10+ this .data = data ;
11+ }
12+ }
1313
14- private Node <T > head ;
15- private Node <T > tail ;
14+ private Node <T > head ;
15+ private Node <T > tail ;
1616
17- public synchronized void enqueue (T data ) {
18- Node <T > newNode = new Node <>(data );
17+ public synchronized void enqueue (T data ) {
18+ Node <T > newNode = new Node <>(data );
1919
20- if (tail == null ) {
21- head = tail = newNode ;
22- return ;
23- }
20+ if (tail == null ) {
21+ head = newNode ;
22+ tail = newNode ;
23+ return ;
24+ }
2425
25- tail .next = newNode ;
26- tail = newNode ;
27- }
26+ tail .next = newNode ;
27+ tail = newNode ;
28+ }
2829
29- public synchronized T dequeue () {
30- if (head == null ) {
31- return null ;
32- }
30+ public synchronized T dequeue () {
31+ if (head == null ) {
32+ return null ;
33+ }
3334
34- T data = head .data ;
35- head = head .next ;
35+ T data = head .data ;
36+ head = head .next ;
3637
37- if (head == null ) {
38- tail = null ;
39- }
38+ if (head == null ) {
39+ tail = null ;
40+ }
4041
41- return data ;
42- }
42+ return data ;
43+ }
4344
44- public synchronized boolean isEmpty () {
45- return head == null ;
46- }
45+ public synchronized boolean isEmpty () {
46+ return head == null ;
47+ }
4748}
You can’t perform that action at this time.
0 commit comments