From fc886d6e43e2ae4e0a8f4f326812f89afeb4cd4f Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 14 Apr 2026 15:17:08 +0200 Subject: [PATCH 1/4] -Added com.thealgorithms.stacks.StackUsingLinkedList, implementation of a Stack using a singly linked list - Added tests for each method in the Stack. - Documented each member **Javadoc documentation changes** Changed documentation from javadoc to block comments because of build failures caused by dangling javadocs. Affected classes: - com.thealgorithms.conversions.AnyBaseToAnyBase - com.thealgorithms.searches.InterpolationSearch - com.thealhorithms.searches.LinearSearch --- .../conversions/AnyBaseToAnyBase.java | 2 +- .../searches/InterpolationSearch.java | 2 +- .../thealgorithms/searches/LinearSearch.java | 2 +- .../stacks/StackUsingLinkedList.java | 146 ++++++++++++++++++ .../stacks/StackUsingLinkedListTest.java | 79 ++++++++++ 5 files changed, 228 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/thealgorithms/stacks/StackUsingLinkedList.java create mode 100644 src/test/java/com/thealgorithms/stacks/StackUsingLinkedListTest.java diff --git a/src/main/java/com/thealgorithms/conversions/AnyBaseToAnyBase.java b/src/main/java/com/thealgorithms/conversions/AnyBaseToAnyBase.java index 7698cc832981..d80821a842f0 100644 --- a/src/main/java/com/thealgorithms/conversions/AnyBaseToAnyBase.java +++ b/src/main/java/com/thealgorithms/conversions/AnyBaseToAnyBase.java @@ -1,4 +1,4 @@ -/** +/* * [Brief description of what the algorithm does] *

* Time Complexity: O(n) [or appropriate complexity] diff --git a/src/main/java/com/thealgorithms/searches/InterpolationSearch.java b/src/main/java/com/thealgorithms/searches/InterpolationSearch.java index d24cc1c774bc..611b0f5259ed 100644 --- a/src/main/java/com/thealgorithms/searches/InterpolationSearch.java +++ b/src/main/java/com/thealgorithms/searches/InterpolationSearch.java @@ -1,4 +1,4 @@ -/** +/* * Interpolation Search estimates the position of the target value * based on the distribution of values. * diff --git a/src/main/java/com/thealgorithms/searches/LinearSearch.java b/src/main/java/com/thealgorithms/searches/LinearSearch.java index 3f273e167f0a..fc6ff7588908 100644 --- a/src/main/java/com/thealgorithms/searches/LinearSearch.java +++ b/src/main/java/com/thealgorithms/searches/LinearSearch.java @@ -1,4 +1,4 @@ -/** +/* * Performs Linear Search on an array. * * Linear search checks each element one by one until the target is found diff --git a/src/main/java/com/thealgorithms/stacks/StackUsingLinkedList.java b/src/main/java/com/thealgorithms/stacks/StackUsingLinkedList.java new file mode 100644 index 000000000000..d3e96d80bd46 --- /dev/null +++ b/src/main/java/com/thealgorithms/stacks/StackUsingLinkedList.java @@ -0,0 +1,146 @@ +package com.thealgorithms.stacks; + +import java.util.NoSuchElementException; + +/** + * Implementation of a {@link java.util.Stack Stack} that uses a "singly" linked list + * to mimic the Last In First Out (LIFO) behaviour of the Stack. + *

+ * Singly meaning the implementation only tracks the elements behind it and not both + * ways as the {@link java.util.LinkedList LinkedList} does. + *

+ *

+ * Supports {@link #peek()}, {@link #pop()}, {@link #push(Object) push()}, + * checking if the structure is {@link #isEmpty()} and getting the {@link #size()}. + * + * @author Andruid929 (Andrew Jones) + */ +public class StackUsingLinkedList { + + /** + * The node at the top of the Stack (last inserted). If not null, contains the last inserted element + * + */ + private Node top; + + /** + * The size of the LinkedStack + * + */ + private int size; + + /** + * Creates a new instance of the Stack. + * + */ + + public StackUsingLinkedList() { + } + + /** + * Check the element last inserted into the stack. + * + * @return the element last inserted into the Stack or {@code null} if the Stack is empty + * + */ + public T peek() { + if (isEmpty()) { + return null; + } + + return top.element; + } + + /** + * Removes an element from the Stack and then returns the removed element. + * + * @return the element last inserted into the Stack. + * @throws NoSuchElementException if the Stack is empty. + * + */ + public T pop() throws NoSuchElementException { + if (isEmpty()) { + throw new NoSuchElementException(); + } + + T removed = top.element; //Get the last inserted element before removing it + + top = top.next; //Set the second to last element at the top + + size--; + + return removed; + } + + /** + * Add a new element to the Stack. + * + * @param element the element to be added. + * + */ + public void push(T element) { + Node node = new Node(element); + + Node next; + + if (top != null) { + next = top; //Save the current top node + + top = node; //Update the newest node + + top.next = next; //Make the new node point to the old node + } else { + + top = node; + } + + size++; + } + + /** + * Check if the stack is empty. + * + * @return true if the Stack is empty; otherwise false. + * + */ + public boolean isEmpty() { + return top == null; + } + + /** + * Get the size stored within this Stack + * + * @return number of elements held in this Stack + * + */ + public int size() { + return size; + } + + /** + * A simple wrapper holding an element and the element that is next to it. + * + */ + private class Node { + + /** + * The element at this node. + * */ + T element; + + /** + * The node next to this one + * */ + Node next; + + /** + * Constructs a new node storing the given element and a null neighbour. + * + * @param element the element to be in this node. + * */ + Node(T element) { + this.element = element; + this.next = null; + } + } +} diff --git a/src/test/java/com/thealgorithms/stacks/StackUsingLinkedListTest.java b/src/test/java/com/thealgorithms/stacks/StackUsingLinkedListTest.java new file mode 100644 index 000000000000..cf77477256c2 --- /dev/null +++ b/src/test/java/com/thealgorithms/stacks/StackUsingLinkedListTest.java @@ -0,0 +1,79 @@ +package com.thealgorithms.stacks; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.NoSuchElementException; + +class StackUsingLinkedListTest { + + private StackUsingLinkedList linkedStack; + + @BeforeEach + public void instantiateLinkedStack() { + linkedStack = new StackUsingLinkedList<>(); + } + + @Test + void peek() { + assertNull(linkedStack.peek()); + + linkedStack.push(10); + linkedStack.push(20); + linkedStack.push(30); + + assertEquals(30, linkedStack.peek()); + } + + @Test + void pop() { + linkedStack.push(3); + linkedStack.push(6); + + assertEquals(6, linkedStack.pop()); + assertEquals(3, linkedStack.peek()); + + linkedStack.pop(); + + assertThrows(NoSuchElementException.class, () -> linkedStack.pop()); //Cannot pop from an empty stack + } + + @Test + void push() { + linkedStack.push(12); + + assertEquals(12, linkedStack.peek()); + + linkedStack.push(15); + linkedStack.push(17); + + assertEquals(17, linkedStack.peek()); + + } + + @Test + void isEmpty() { + assertTrue(linkedStack.isEmpty()); + + linkedStack.push(1); + + assertFalse(linkedStack.isEmpty()); + } + + @Test + void size() { + linkedStack.push(1); + linkedStack.push(2); + linkedStack.push(3); + linkedStack.push(4); + + assertEquals(4, linkedStack.size()); + + linkedStack.pop(); + linkedStack.pop(); + + assertEquals(2, linkedStack.size()); + } +} \ No newline at end of file From 650a622d0a39c45bacc7baf81efd194f33157d75 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 14 Apr 2026 15:46:37 +0200 Subject: [PATCH 2/4] Fixed wildcard imports --- .../thealgorithms/stacks/StackUsingLinkedListTest.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/stacks/StackUsingLinkedListTest.java b/src/test/java/com/thealgorithms/stacks/StackUsingLinkedListTest.java index cf77477256c2..86e95cfbfc2b 100644 --- a/src/test/java/com/thealgorithms/stacks/StackUsingLinkedListTest.java +++ b/src/test/java/com/thealgorithms/stacks/StackUsingLinkedListTest.java @@ -1,6 +1,10 @@ package com.thealgorithms.stacks; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -76,4 +80,4 @@ void size() { assertEquals(2, linkedStack.size()); } -} \ No newline at end of file +} From 259fbfa4c82fd261e549d2f05abffe9a347e1fc8 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 14 Apr 2026 15:57:14 +0200 Subject: [PATCH 3/4] Reduced static imports --- .../stacks/StackUsingLinkedListTest.java | 29 ++++++++----------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/src/test/java/com/thealgorithms/stacks/StackUsingLinkedListTest.java b/src/test/java/com/thealgorithms/stacks/StackUsingLinkedListTest.java index 86e95cfbfc2b..ed4db662500d 100644 --- a/src/test/java/com/thealgorithms/stacks/StackUsingLinkedListTest.java +++ b/src/test/java/com/thealgorithms/stacks/StackUsingLinkedListTest.java @@ -1,11 +1,6 @@ package com.thealgorithms.stacks; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; - +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -22,13 +17,13 @@ public void instantiateLinkedStack() { @Test void peek() { - assertNull(linkedStack.peek()); + Assertions.assertNull(linkedStack.peek()); linkedStack.push(10); linkedStack.push(20); linkedStack.push(30); - assertEquals(30, linkedStack.peek()); + Assertions.assertEquals(30, linkedStack.peek()); } @Test @@ -36,34 +31,34 @@ void pop() { linkedStack.push(3); linkedStack.push(6); - assertEquals(6, linkedStack.pop()); - assertEquals(3, linkedStack.peek()); + Assertions.assertEquals(6, linkedStack.pop()); + Assertions.assertEquals(3, linkedStack.peek()); linkedStack.pop(); - assertThrows(NoSuchElementException.class, () -> linkedStack.pop()); //Cannot pop from an empty stack + Assertions.assertThrows(NoSuchElementException.class, () -> linkedStack.pop()); //Cannot pop from an empty stack } @Test void push() { linkedStack.push(12); - assertEquals(12, linkedStack.peek()); + Assertions.assertEquals(12, linkedStack.peek()); linkedStack.push(15); linkedStack.push(17); - assertEquals(17, linkedStack.peek()); + Assertions.assertEquals(17, linkedStack.peek()); } @Test void isEmpty() { - assertTrue(linkedStack.isEmpty()); + Assertions.assertTrue(linkedStack.isEmpty()); linkedStack.push(1); - assertFalse(linkedStack.isEmpty()); + Assertions.assertFalse(linkedStack.isEmpty()); } @Test @@ -73,11 +68,11 @@ void size() { linkedStack.push(3); linkedStack.push(4); - assertEquals(4, linkedStack.size()); + Assertions.assertEquals(4, linkedStack.size()); linkedStack.pop(); linkedStack.pop(); - assertEquals(2, linkedStack.size()); + Assertions.assertEquals(2, linkedStack.size()); } } From 4acfdb19bfb9c2d37d0f6f8f715f4422127bdce2 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 14 Apr 2026 21:35:46 +0200 Subject: [PATCH 4/4] clang-format applied to pass clang-format job --- .../com/thealgorithms/stacks/StackUsingLinkedList.java | 10 +++++----- .../thealgorithms/stacks/StackUsingLinkedListTest.java | 6 ++---- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/thealgorithms/stacks/StackUsingLinkedList.java b/src/main/java/com/thealgorithms/stacks/StackUsingLinkedList.java index d3e96d80bd46..def51a63a078 100644 --- a/src/main/java/com/thealgorithms/stacks/StackUsingLinkedList.java +++ b/src/main/java/com/thealgorithms/stacks/StackUsingLinkedList.java @@ -63,9 +63,9 @@ public T pop() throws NoSuchElementException { throw new NoSuchElementException(); } - T removed = top.element; //Get the last inserted element before removing it + T removed = top.element; // Get the last inserted element before removing it - top = top.next; //Set the second to last element at the top + top = top.next; // Set the second to last element at the top size--; @@ -84,11 +84,11 @@ public void push(T element) { Node next; if (top != null) { - next = top; //Save the current top node + next = top; // Save the current top node - top = node; //Update the newest node + top = node; // Update the newest node - top.next = next; //Make the new node point to the old node + top.next = next; // Make the new node point to the old node } else { top = node; diff --git a/src/test/java/com/thealgorithms/stacks/StackUsingLinkedListTest.java b/src/test/java/com/thealgorithms/stacks/StackUsingLinkedListTest.java index ed4db662500d..f08902932454 100644 --- a/src/test/java/com/thealgorithms/stacks/StackUsingLinkedListTest.java +++ b/src/test/java/com/thealgorithms/stacks/StackUsingLinkedListTest.java @@ -1,11 +1,10 @@ package com.thealgorithms.stacks; +import java.util.NoSuchElementException; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import java.util.NoSuchElementException; - class StackUsingLinkedListTest { private StackUsingLinkedList linkedStack; @@ -36,7 +35,7 @@ void pop() { linkedStack.pop(); - Assertions.assertThrows(NoSuchElementException.class, () -> linkedStack.pop()); //Cannot pop from an empty stack + Assertions.assertThrows(NoSuchElementException.class, () -> linkedStack.pop()); // Cannot pop from an empty stack } @Test @@ -49,7 +48,6 @@ void push() { linkedStack.push(17); Assertions.assertEquals(17, linkedStack.peek()); - } @Test