Skip to content

LinkedQue: Features and Improvements #3909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 5, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<T> implements Iterable<T> {

class Node {
static class Node<T> {

int data;
Node next;
T data;
Node<T> 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<T> next) {
this.data = data;
this.next = next;
}
Expand All @@ -26,12 +28,12 @@ public Node(int data, Node next) {
/**
* Front of Queue
*/
private Node front;
private Node<T> front;

/**
* Rear of Queue
*/
private Node rear;
private Node<T> rear;

/**
* Size of Queue
Expand All @@ -42,7 +44,7 @@ public Node(int data, Node next) {
* Init LinkedQueue
*/
public LinkedQueue() {
front = rear = new Node();
front = rear = new Node<>();
}

/**
Expand All @@ -58,30 +60,28 @@ public boolean isEmpty() {
* 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<T> newNode = new Node<>(data);

rear.next = newNode;
rear = newNode;
/* make rear point at last node */
size++;
return true;
}

/**
* Remove element at the front of queue
*
* @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<T> destroy = front.next;
T retValue = destroy.data;
front.next = front.next.next;
destroy = null;
/* clear let GC do it's work */
size--;

Expand All @@ -97,7 +97,7 @@ public int dequeue() {
*
* @return element at the front
*/
public int peekFront() {
public T peekFront() {
if (isEmpty()) {
throw new NoSuchElementException("queue is empty");
}
Expand All @@ -109,13 +109,52 @@ public int peekFront() {
*
* @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<T> 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<T> iterator() {
return new Iterator<>() {

Node<T> node = front;

@Override
public boolean hasNext() {
return node.next != null;
}

@Override
public T next() {
return (node = node.next).data;
}
};
}

/**
* Return size of queue
*
Expand All @@ -129,30 +168,22 @@ public int size() {
* 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<T> 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<Integer> queue = new LinkedQueue<>();
assert queue.isEmpty();

queue.enqueue(1);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Integer> 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();
});
}
}