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..def51a63a078 --- /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..f08902932454 --- /dev/null +++ b/src/test/java/com/thealgorithms/stacks/StackUsingLinkedListTest.java @@ -0,0 +1,76 @@ +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; + +class StackUsingLinkedListTest { + + private StackUsingLinkedList linkedStack; + + @BeforeEach + public void instantiateLinkedStack() { + linkedStack = new StackUsingLinkedList<>(); + } + + @Test + void peek() { + Assertions.assertNull(linkedStack.peek()); + + linkedStack.push(10); + linkedStack.push(20); + linkedStack.push(30); + + Assertions.assertEquals(30, linkedStack.peek()); + } + + @Test + void pop() { + linkedStack.push(3); + linkedStack.push(6); + + Assertions.assertEquals(6, linkedStack.pop()); + Assertions.assertEquals(3, linkedStack.peek()); + + linkedStack.pop(); + + Assertions.assertThrows(NoSuchElementException.class, () -> linkedStack.pop()); // Cannot pop from an empty stack + } + + @Test + void push() { + linkedStack.push(12); + + Assertions.assertEquals(12, linkedStack.peek()); + + linkedStack.push(15); + linkedStack.push(17); + + Assertions.assertEquals(17, linkedStack.peek()); + } + + @Test + void isEmpty() { + Assertions.assertTrue(linkedStack.isEmpty()); + + linkedStack.push(1); + + Assertions.assertFalse(linkedStack.isEmpty()); + } + + @Test + void size() { + linkedStack.push(1); + linkedStack.push(2); + linkedStack.push(3); + linkedStack.push(4); + + Assertions.assertEquals(4, linkedStack.size()); + + linkedStack.pop(); + linkedStack.pop(); + + Assertions.assertEquals(2, linkedStack.size()); + } +}