Skip to content
Open
Show file tree
Hide file tree
Changes from all 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,4 +1,4 @@
/**
/*
* [Brief description of what the algorithm does]
* <p>
* Time Complexity: O(n) [or appropriate complexity]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Interpolation Search estimates the position of the target value
* based on the distribution of values.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Performs Linear Search on an array.
*
* Linear search checks each element one by one until the target is found
Expand Down
146 changes: 146 additions & 0 deletions src/main/java/com/thealgorithms/stacks/StackUsingLinkedList.java
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* Singly meaning the implementation only tracks the elements behind it and not both
* ways as the {@link java.util.LinkedList LinkedList} does.
* <p>
* <p>
* 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<T> {

/**
* 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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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<Integer> 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());
}
}
Loading