Skip to content

Commit 41020e5

Browse files
feat: add StackUsingLinkedList implementation with test cases
1 parent 56a087d commit 41020e5

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.thealgorithms.stacks;
2+
3+
/**
4+
* A class that implements a Stack using a singly linked list.
5+
* Supports basic operations like push, pop, peek, and isEmpty.
6+
*
7+
* Reference: https://www.geeksforgeeks.org/stack-using-linked-list/
8+
*/
9+
public class StackUsingLinkedList {
10+
11+
/**
12+
* Node class representing each element in the stack
13+
*/
14+
private static class Node {
15+
int data;
16+
Node next;
17+
18+
Node(int data) {
19+
this.data = data;
20+
}
21+
}
22+
23+
private Node top;
24+
25+
/**
26+
* Push an element onto the stack
27+
*
28+
* @param value the value to push
29+
*/
30+
public void push(int value) {
31+
Node newNode = new Node(value);
32+
newNode.next = top;
33+
top = newNode;
34+
}
35+
36+
/**
37+
* Remove and return the top element of the stack
38+
*
39+
* @return top element
40+
*/
41+
public int pop() {
42+
if (top == null) {
43+
throw new RuntimeException("Stack is empty");
44+
}
45+
int value = top.data;
46+
top = top.next;
47+
return value;
48+
}
49+
50+
/**
51+
* Return the top element without removing it
52+
*
53+
* @return top element
54+
*/
55+
public int peek() {
56+
if (top == null) {
57+
throw new RuntimeException("Stack is empty");
58+
}
59+
return top.data;
60+
}
61+
62+
/**
63+
* Check if the stack is empty
64+
*
65+
* @return true if empty, false otherwise
66+
*/
67+
public boolean isEmpty() {
68+
return top == null;
69+
}
70+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.thealgorithms.stacks;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
import org.junit.jupiter.api.Test;
5+
6+
/**
7+
* Test class for StackUsingLinkedList.
8+
*
9+
* This class contains unit tests to verify the correctness
10+
* of stack operations such as push, pop, peek, and isEmpty.
11+
*
12+
* Reference: https://www.geeksforgeeks.org/stack-using-linked-list/
13+
*/
14+
class StackUsingLinkedListTest {
15+
16+
/**
17+
* Test push and pop operations
18+
*/
19+
@Test
20+
void testPushAndPop() {
21+
StackUsingLinkedList stack = new StackUsingLinkedList();
22+
stack.push(10);
23+
stack.push(20);
24+
25+
assertEquals(20, stack.pop());
26+
assertEquals(10, stack.pop());
27+
}
28+
29+
/**
30+
* Test peek operation
31+
*/
32+
@Test
33+
void testPeek() {
34+
StackUsingLinkedList stack = new StackUsingLinkedList();
35+
stack.push(5);
36+
37+
assertEquals(5, stack.peek());
38+
}
39+
40+
/**
41+
* Test isEmpty method
42+
*/
43+
@Test
44+
void testIsEmpty() {
45+
StackUsingLinkedList stack = new StackUsingLinkedList();
46+
47+
assertTrue(stack.isEmpty());
48+
stack.push(1);
49+
assertFalse(stack.isEmpty());
50+
}
51+
52+
/**
53+
* Test pop on empty stack (edge case)
54+
*/
55+
@Test
56+
void testPopOnEmptyStack() {
57+
StackUsingLinkedList stack = new StackUsingLinkedList();
58+
59+
assertThrows(RuntimeException.class, stack::pop);
60+
}
61+
62+
/**
63+
* Test peek on empty stack (edge case)
64+
*/
65+
@Test
66+
void testPeekOnEmptyStack() {
67+
StackUsingLinkedList stack = new StackUsingLinkedList();
68+
69+
assertThrows(RuntimeException.class, stack::peek);
70+
}
71+
}

0 commit comments

Comments
 (0)