Skip to content

Commit fc886d6

Browse files
committed
-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
1 parent 79bc620 commit fc886d6

File tree

5 files changed

+228
-3
lines changed

5 files changed

+228
-3
lines changed

src/main/java/com/thealgorithms/conversions/AnyBaseToAnyBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/*
22
* [Brief description of what the algorithm does]
33
* <p>
44
* Time Complexity: O(n) [or appropriate complexity]

src/main/java/com/thealgorithms/searches/InterpolationSearch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/*
22
* Interpolation Search estimates the position of the target value
33
* based on the distribution of values.
44
*

src/main/java/com/thealgorithms/searches/LinearSearch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/*
22
* Performs Linear Search on an array.
33
*
44
* Linear search checks each element one by one until the target is found
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package com.thealgorithms.stacks;
2+
3+
import java.util.NoSuchElementException;
4+
5+
/**
6+
* Implementation of a {@link java.util.Stack Stack} that uses a "singly" linked list
7+
* to mimic the Last In First Out (LIFO) behaviour of the Stack.
8+
* <p>
9+
* Singly meaning the implementation only tracks the elements behind it and not both
10+
* ways as the {@link java.util.LinkedList LinkedList} does.
11+
* <p>
12+
* <p>
13+
* Supports {@link #peek()}, {@link #pop()}, {@link #push(Object) push()},
14+
* checking if the structure is {@link #isEmpty()} and getting the {@link #size()}.
15+
*
16+
* @author Andruid929 (Andrew Jones)
17+
*/
18+
public class StackUsingLinkedList<T> {
19+
20+
/**
21+
* The node at the top of the Stack (last inserted). If not null, contains the last inserted element
22+
*
23+
*/
24+
private Node top;
25+
26+
/**
27+
* The size of the LinkedStack
28+
*
29+
*/
30+
private int size;
31+
32+
/**
33+
* Creates a new instance of the Stack.
34+
*
35+
*/
36+
37+
public StackUsingLinkedList() {
38+
}
39+
40+
/**
41+
* Check the element last inserted into the stack.
42+
*
43+
* @return the element last inserted into the Stack or {@code null} if the Stack is empty
44+
*
45+
*/
46+
public T peek() {
47+
if (isEmpty()) {
48+
return null;
49+
}
50+
51+
return top.element;
52+
}
53+
54+
/**
55+
* Removes an element from the Stack and then returns the removed element.
56+
*
57+
* @return the element last inserted into the Stack.
58+
* @throws NoSuchElementException if the Stack is empty.
59+
*
60+
*/
61+
public T pop() throws NoSuchElementException {
62+
if (isEmpty()) {
63+
throw new NoSuchElementException();
64+
}
65+
66+
T removed = top.element; //Get the last inserted element before removing it
67+
68+
top = top.next; //Set the second to last element at the top
69+
70+
size--;
71+
72+
return removed;
73+
}
74+
75+
/**
76+
* Add a new element to the Stack.
77+
*
78+
* @param element the element to be added.
79+
*
80+
*/
81+
public void push(T element) {
82+
Node node = new Node(element);
83+
84+
Node next;
85+
86+
if (top != null) {
87+
next = top; //Save the current top node
88+
89+
top = node; //Update the newest node
90+
91+
top.next = next; //Make the new node point to the old node
92+
} else {
93+
94+
top = node;
95+
}
96+
97+
size++;
98+
}
99+
100+
/**
101+
* Check if the stack is empty.
102+
*
103+
* @return true if the Stack is empty; otherwise false.
104+
*
105+
*/
106+
public boolean isEmpty() {
107+
return top == null;
108+
}
109+
110+
/**
111+
* Get the size stored within this Stack
112+
*
113+
* @return number of elements held in this Stack
114+
*
115+
*/
116+
public int size() {
117+
return size;
118+
}
119+
120+
/**
121+
* A simple wrapper holding an element and the element that is next to it.
122+
*
123+
*/
124+
private class Node {
125+
126+
/**
127+
* The element at this node.
128+
* */
129+
T element;
130+
131+
/**
132+
* The node next to this one
133+
* */
134+
Node next;
135+
136+
/**
137+
* Constructs a new node storing the given element and a null neighbour.
138+
*
139+
* @param element the element to be in this node.
140+
* */
141+
Node(T element) {
142+
this.element = element;
143+
this.next = null;
144+
}
145+
}
146+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.thealgorithms.stacks;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.util.NoSuchElementException;
9+
10+
class StackUsingLinkedListTest {
11+
12+
private StackUsingLinkedList<Integer> linkedStack;
13+
14+
@BeforeEach
15+
public void instantiateLinkedStack() {
16+
linkedStack = new StackUsingLinkedList<>();
17+
}
18+
19+
@Test
20+
void peek() {
21+
assertNull(linkedStack.peek());
22+
23+
linkedStack.push(10);
24+
linkedStack.push(20);
25+
linkedStack.push(30);
26+
27+
assertEquals(30, linkedStack.peek());
28+
}
29+
30+
@Test
31+
void pop() {
32+
linkedStack.push(3);
33+
linkedStack.push(6);
34+
35+
assertEquals(6, linkedStack.pop());
36+
assertEquals(3, linkedStack.peek());
37+
38+
linkedStack.pop();
39+
40+
assertThrows(NoSuchElementException.class, () -> linkedStack.pop()); //Cannot pop from an empty stack
41+
}
42+
43+
@Test
44+
void push() {
45+
linkedStack.push(12);
46+
47+
assertEquals(12, linkedStack.peek());
48+
49+
linkedStack.push(15);
50+
linkedStack.push(17);
51+
52+
assertEquals(17, linkedStack.peek());
53+
54+
}
55+
56+
@Test
57+
void isEmpty() {
58+
assertTrue(linkedStack.isEmpty());
59+
60+
linkedStack.push(1);
61+
62+
assertFalse(linkedStack.isEmpty());
63+
}
64+
65+
@Test
66+
void size() {
67+
linkedStack.push(1);
68+
linkedStack.push(2);
69+
linkedStack.push(3);
70+
linkedStack.push(4);
71+
72+
assertEquals(4, linkedStack.size());
73+
74+
linkedStack.pop();
75+
linkedStack.pop();
76+
77+
assertEquals(2, linkedStack.size());
78+
}
79+
}

0 commit comments

Comments
 (0)