Skip to content
Closed
Prev Previous commit
Next Next commit
style: apply clang-format to fix formatting issues
  • Loading branch information
prashantpiyush1111 committed Apr 15, 2026
commit db4e0c3944b228f5495fb24fb9e95e00d53ffc72
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
Expand Up @@ -6,66 +6,66 @@
/**
* 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