From 3e9dd776e5cc91c84e8206779ace3874c8d7491f Mon Sep 17 00:00:00 2001 From: Kumaraswamy B G <71964026+XomaDev@users.noreply.github.com> Date: Mon, 6 Mar 2023 00:38:42 +0530 Subject: [PATCH] Make LinkedQueue generic (#3909) --- .../datastructures/queues/LinkedQueue.java | 101 ++++++++++++------ .../queues/LinkedQueueTest.java | 29 +++++ 2 files changed, 95 insertions(+), 35 deletions(-) create mode 100644 src/test/java/com/thealgorithms/datastructures/queues/LinkedQueueTest.java diff --git a/src/main/java/com/thealgorithms/datastructures/queues/LinkedQueue.java b/src/main/java/com/thealgorithms/datastructures/queues/LinkedQueue.java index 770582b7..79fddb0e 100644 --- a/src/main/java/com/thealgorithms/datastructures/queues/LinkedQueue.java +++ b/src/main/java/com/thealgorithms/datastructures/queues/LinkedQueue.java @@ -1,23 +1,25 @@ package com.thealgorithms.datastructures.queues; +import java.util.Iterator; import java.util.NoSuchElementException; +import java.util.StringJoiner; -public class LinkedQueue { +public class LinkedQueue implements Iterable { - class Node { + static class Node { - int data; - Node next; + T data; + Node next; public Node() { - this(0); + this(null); } - public Node(int data) { + public Node(T data) { this(data, null); } - public Node(int data, Node next) { + public Node(T data, Node next) { this.data = data; this.next = next; } @@ -26,12 +28,12 @@ public class LinkedQueue { /** * Front of Queue */ - private Node front; + private Node front; /** * Rear of Queue */ - private Node rear; + private Node rear; /** * Size of Queue @@ -42,7 +44,7 @@ public class LinkedQueue { * Init LinkedQueue */ public LinkedQueue() { - front = rear = new Node(); + front = rear = new Node<>(); } /** @@ -58,15 +60,14 @@ public class LinkedQueue { * Add element to rear of queue * * @param data insert value - * @return true if add successfully */ - public boolean enqueue(int data) { - Node newNode = new Node(data); + public void enqueue(T data) { + Node newNode = new Node<>(data); + rear.next = newNode; rear = newNode; /* make rear point at last node */ size++; - return true; } /** @@ -74,14 +75,13 @@ public class LinkedQueue { * * @return element at the front of queue */ - public int dequeue() { + public T dequeue() { if (isEmpty()) { throw new NoSuchElementException("queue is empty"); } - Node destroy = front.next; - int retValue = destroy.data; + Node destroy = front.next; + T retValue = destroy.data; front.next = front.next.next; - destroy = null; /* clear let GC do it's work */ size--; @@ -97,7 +97,7 @@ public class LinkedQueue { * * @return element at the front */ - public int peekFront() { + public T peekFront() { if (isEmpty()) { throw new NoSuchElementException("queue is empty"); } @@ -109,13 +109,52 @@ public class LinkedQueue { * * @return element at the front */ - public int peekRear() { + public T peekRear() { if (isEmpty()) { throw new NoSuchElementException("queue is empty"); } return rear.data; } + /** + * Peeks the element at the index and + * returns the value + * @param pos at which to peek + */ + + public T peek(int pos) { + if (pos > size) + throw new IndexOutOfBoundsException( + "Position %s out of range!".formatted(pos)); + Node node = front; + while (pos-- > 0) + node = node.next; + return node.data; + } + + /** + * Node iterator, allows to travel through + * the nodes using for() loop or forEach(Consumer) + */ + + @Override + public Iterator iterator() { + return new Iterator<>() { + + Node node = front; + + @Override + public boolean hasNext() { + return node.next != null; + } + + @Override + public T next() { + return (node = node.next).data; + } + }; + } + /** * Return size of queue * @@ -129,30 +168,22 @@ public class LinkedQueue { * Clear all nodes in queue */ public void clear() { - while (!isEmpty()) { + while (size > 0) dequeue(); - } } @Override public String toString() { - if (isEmpty()) { - return "[]"; - } - StringBuilder builder = new StringBuilder(); - Node cur = front.next; - builder.append("["); - while (cur != null) { - builder.append(cur.data).append(", "); - cur = cur.next; - } - builder.replace(builder.length() - 2, builder.length(), "]"); - return builder.toString(); + StringJoiner join = new StringJoiner(", "); // separator of ', ' + Node travel = front; + while ((travel = travel.next) != null) + join.add(String.valueOf(travel.data)); + return '[' + join.toString() + ']'; } /* Driver Code */ public static void main(String[] args) { - LinkedQueue queue = new LinkedQueue(); + LinkedQueue queue = new LinkedQueue<>(); assert queue.isEmpty(); queue.enqueue(1); diff --git a/src/test/java/com/thealgorithms/datastructures/queues/LinkedQueueTest.java b/src/test/java/com/thealgorithms/datastructures/queues/LinkedQueueTest.java new file mode 100644 index 00000000..e153890e --- /dev/null +++ b/src/test/java/com/thealgorithms/datastructures/queues/LinkedQueueTest.java @@ -0,0 +1,29 @@ +package com.thealgorithms.datastructures.queues; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + + +class LinkedQueueTest { + @Test + public void testQue() { + LinkedQueue queue = new LinkedQueue<>(); + for (int i = 1; i < 5; i++) + queue.enqueue(i); + + assertEquals(queue.peekRear(), 4); + assertEquals(queue.peek(2), 2); + + assertEquals(queue.peek(4), 4); + + final int[] element = { 1 }; + + // iterates over all the elements present + // as in the form of nodes + queue.forEach(integer -> { + if (element[0]++ != integer) + throw new AssertionError(); + }); + } +} \ No newline at end of file