Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
style: fix formatting to satisfy clang-format and checkstyle
  • Loading branch information
prashantpiyush1111 committed Apr 15, 2026
commit ca2cdfca83f4f674c1009a673312d98f4f105f68
110 changes: 55 additions & 55 deletions src/main/java/com/thealgorithms/stacks/StackUsingLinkedList.java
Original file line number Diff line number Diff line change
@@ -1,70 +1,70 @@
package com.thealgorithms.stacks;

/**
* A class that implements a Stack using a singly linked list. Supports basic
* operations like push, pop, peek, and isEmpty.
* A class that implements a Stack using a singly linked list.
* Supports basic operations like push, pop, peek, and isEmpty.
*
* Reference: https://www.geeksforgeeks.org/stack-using-linked-list/
*/
public class StackUsingLinkedList {

/**
* Node class representing each element in the stack
*/
private static class Node {
int data;
Node next;
/**
* Node class representing each element in the stack
*/
private static class Node {
int data;
Node next;

Node(int data) {
this.data = data;
}
}
Node(int data) {
this.data = data;
}
}

private Node top;
private Node top;

/**
* Push an element onto the stack
*
* @param value the value to push
*/
public void push(int value) {
Node newNode = new Node(value);
newNode.next = top;
top = newNode;
}
/**
* Push an element onto the stack
*
* @param value the value to push
*/
public void push(int value) {
Node newNode = new Node(value);
newNode.next = top;
top = newNode;
}

/**
* Remove and return the top element of the stack
*
* @return top element
*/
public int pop() {
if (top == null) {
throw new RuntimeException("Stack is empty");
}
int value = top.data;
top = top.next;
return value;
}
/**
* Remove and return the top element of the stack
*
* @return top element
*/
public int pop() {
if (top == null) {
throw new RuntimeException("Stack is empty");
}
int value = top.data;
top = top.next;
return value;
}

/**
* Return the top element without removing it
*
* @return top element
*/
public int peek() {
if (top == null) {
throw new RuntimeException("Stack is empty");
}
return top.data;
}
/**
* Return the top element without removing it
*
* @return top element
*/
public int peek() {
if (top == null) {
throw new RuntimeException("Stack is empty");
}
return top.data;
}

/**
* Check if the stack is empty
*
* @return true if empty, false otherwise
*/
public boolean isEmpty() {
return top == null;
}
/**
* Check if the stack is empty
*
* @return true if empty, false otherwise
*/
public boolean isEmpty() {
return top == null;
}
}
Original file line number Diff line number Diff line change
@@ -1,74 +1,75 @@
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.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;

import org.junit.jupiter.api.Test;

/**
* Test class for StackUsingLinkedList.
*
* This class contains unit tests to verify the correctness of stack operations
* such as push, pop, peek, and isEmpty.
* This class contains unit tests to verify the correctness
* of stack operations such as push, pop, peek, and isEmpty.
*
* Reference: https://www.geeksforgeeks.org/stack-using-linked-list/
*/
class StackUsingLinkedListTest {

/**
* Test push and pop operations
*/
@Test
void testPushAndPop() {
StackUsingLinkedList stack = new StackUsingLinkedList();
stack.push(10);
stack.push(20);
/**
* Test push and pop operations
*/
@Test
void testPushAndPop() {
StackUsingLinkedList stack = new StackUsingLinkedList();
stack.push(10);
stack.push(20);

assertEquals(20, stack.pop());
assertEquals(10, stack.pop());
}
assertEquals(20, stack.pop());
assertEquals(10, stack.pop());
}

/**
* Test peek operation
*/
@Test
void testPeek() {
StackUsingLinkedList stack = new StackUsingLinkedList();
stack.push(5);
/**
* Test peek operation
*/
@Test
void testPeek() {
StackUsingLinkedList stack = new StackUsingLinkedList();
stack.push(5);

assertEquals(5, stack.peek());
}
assertEquals(5, stack.peek());
}

/**
* Test isEmpty method
*/
@Test
void testIsEmpty() {
StackUsingLinkedList stack = new StackUsingLinkedList();
/**
* Test isEmpty method
*/
@Test
void testIsEmpty() {
StackUsingLinkedList stack = new StackUsingLinkedList();

assertTrue(stack.isEmpty());
stack.push(1);
assertFalse(stack.isEmpty());
}
assertTrue(stack.isEmpty());
stack.push(1);
assertFalse(stack.isEmpty());
}

/**
* Test pop on empty stack (edge case)
*/
@Test
void testPopOnEmptyStack() {
StackUsingLinkedList stack = new StackUsingLinkedList();
/**
* Test pop on empty stack (edge case)
*/
@Test
void testPopOnEmptyStack() {
StackUsingLinkedList stack = new StackUsingLinkedList();

assertThrows(RuntimeException.class, stack::pop);
}
assertThrows(RuntimeException.class, stack::pop);
}

/**
* Test peek on empty stack (edge case)
*/
@Test
void testPeekOnEmptyStack() {
StackUsingLinkedList stack = new StackUsingLinkedList();
/**
* Test peek on empty stack (edge case)
*/
@Test
void testPeekOnEmptyStack() {
StackUsingLinkedList stack = new StackUsingLinkedList();

assertThrows(RuntimeException.class, stack::peek);
}
assertThrows(RuntimeException.class, stack::peek);
}
}
Loading