Skip to content

Commit c237423

Browse files
committed
Fix conflicts
1 parent 3cd1ce4 commit c237423

File tree

2 files changed

+125
-55
lines changed

2 files changed

+125
-55
lines changed
Lines changed: 73 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,109 @@
11
package com.thealgorithms.datastructures.stacks;
22

33
/**
4-
* Implementation of a stack using nodes. Unlimited size, no arraylist.
4+
* A stack implementation using linked nodes, supporting unlimited size without an ArrayList.
55
*
6+
* <p>Each node in the stack contains data of generic type {@code Item}, along with references
7+
* to the next and previous nodes, supporting typical stack operations.
8+
*
9+
* <p>The stack follows a Last-In-First-Out (LIFO) order where elements added last are
10+
* removed first. Supported operations include push, pop, and peek.
11+
*
12+
* @param <Item> the type of elements held in this stack
613
*/
714
public class NodeStack<Item> {
815

9-
private Item data;
10-
private NodeStack<Item> previous;
11-
private NodeStack<Item> head;
12-
private int size = 0;
16+
/**
17+
* Node class representing each element in the stack.
18+
*/
19+
private class Node {
20+
Item data;
21+
Node previous;
1322

14-
public NodeStack() {
23+
Node(Item data) {
24+
this.data = data;
25+
this.previous = null;
26+
}
1527
}
1628

17-
private NodeStack(Item item) {
18-
this.data = item;
29+
private Node head; // Top node in the stack
30+
private int size; // Number of elements in the stack
31+
32+
/**
33+
* Constructs an empty NodeStack.
34+
*/
35+
public NodeStack() {
36+
head = null;
37+
size = 0;
1938
}
2039

40+
/**
41+
* Pushes an item onto the stack.
42+
*
43+
* @param item the item to be pushed onto the stack
44+
*/
2145
public void push(Item item) {
22-
NodeStack<Item> newNode = new NodeStack<>(item);
23-
24-
if (isEmpty()) {
25-
head = newNode;
26-
} else {
27-
newNode.previous = head;
28-
head = newNode;
29-
}
46+
Node newNode = new Node(item);
47+
newNode.previous = head;
48+
head = newNode;
3049
size++;
3150
}
3251

52+
/**
53+
* Removes and returns the item at the top of the stack.
54+
*
55+
* @return the item at the top of the stack, or {@code null} if the stack is empty
56+
* @throws IllegalStateException if the stack is empty
57+
*/
3358
public Item pop() {
3459
if (isEmpty()) {
35-
throw new IllegalStateException("Stack is empty, cannot peek element");
60+
throw new IllegalStateException("Cannot pop from an empty stack.");
3661
}
37-
38-
Item item = head.data;
62+
Item data = head.data;
3963
head = head.previous;
4064
size--;
41-
return item;
65+
return data;
4266
}
4367

68+
/**
69+
* Returns the item at the top of the stack without removing it.
70+
*
71+
* @return the item at the top of the stack, or {@code null} if the stack is empty
72+
* @throws IllegalStateException if the stack is empty
73+
*/
4474
public Item peek() {
4575
if (isEmpty()) {
46-
throw new IllegalStateException("Stack is empty, cannot peek element");
76+
throw new IllegalStateException("Cannot peek from an empty stack.");
4777
}
4878
return head.data;
4979
}
5080

81+
/**
82+
* Checks whether the stack is empty.
83+
*
84+
* @return {@code true} if the stack has no elements, {@code false} otherwise
85+
*/
5186
public boolean isEmpty() {
52-
return size == 0;
87+
return head == null;
5388
}
5489

90+
/**
91+
* Returns the number of elements currently in the stack.
92+
*
93+
* @return the size of the stack
94+
*/
5595
public int size() {
5696
return size;
5797
}
98+
99+
/**
100+
* Prints the contents of the stack from top to bottom.
101+
*/
102+
public void print() {
103+
Node current = head;
104+
while (current != null) {
105+
System.out.println(current.data);
106+
current = current.previous;
107+
}
108+
}
58109
}

src/test/java/com/thealgorithms/datastructures/stacks/NodeStackTest.java

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,62 +5,81 @@
55
import static org.junit.jupiter.api.Assertions.assertThrows;
66
import static org.junit.jupiter.api.Assertions.assertTrue;
77

8-
import java.io.ByteArrayOutputStream;
9-
import java.io.PrintStream;
10-
import org.junit.jupiter.api.BeforeEach;
118
import org.junit.jupiter.api.Test;
129

1310
class NodeStackTest {
1411

15-
private NodeStack<Integer> stack;
16-
private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
12+
@Test
13+
void testPush() {
14+
NodeStack<Integer> stack = new NodeStack<>();
15+
stack.push(10);
16+
stack.push(20);
17+
assertEquals(20, stack.peek(), "Top element should be 20 after pushing 10 and 20.");
18+
}
1719

18-
@BeforeEach
19-
void setUp() {
20-
stack = new NodeStack<>();
21-
System.setOut(new PrintStream(outputStreamCaptor));
20+
@Test
21+
void testPop() {
22+
NodeStack<String> stack = new NodeStack<>();
23+
stack.push("First");
24+
stack.push("Second");
25+
assertEquals("Second", stack.pop(), "Pop should return 'Second', the last pushed element.");
26+
assertEquals("First", stack.pop(), "Pop should return 'First' after 'Second' is removed.");
2227
}
2328

2429
@Test
25-
void testPushAndPeek() {
26-
stack.push(3);
27-
assertEquals(3, stack.peek(), "Peek should return the last pushed item");
28-
stack.push(4);
29-
stack.push(5);
30-
assertEquals(5, stack.peek(), "Peek should return the last pushed item");
30+
void testPopOnEmptyStack() {
31+
NodeStack<Double> stack = new NodeStack<>();
32+
assertThrows(IllegalStateException.class, stack::pop, "Popping an empty stack should throw IllegalStateException.");
3133
}
3234

3335
@Test
34-
void testPop() {
36+
void testPeek() {
37+
NodeStack<Integer> stack = new NodeStack<>();
3538
stack.push(5);
36-
stack.push(7);
37-
assertEquals(7, stack.pop(), "Pop should return the last pushed item");
38-
assertEquals(5, stack.pop(), "Pop should return the next item in stack");
39-
assertTrue(stack.isEmpty(), "Stack should be empty after popping all elements");
39+
stack.push(15);
40+
assertEquals(15, stack.peek(), "Peek should return 15, the top element.");
41+
stack.pop();
42+
assertEquals(5, stack.peek(), "Peek should return 5 after 15 is popped.");
4043
}
4144

4245
@Test
43-
void testIsEmpty() {
44-
assertTrue(stack.isEmpty(), "New stack should be empty");
45-
stack.push(1);
46-
assertFalse(stack.isEmpty(), "Stack should not be empty after push");
46+
void testPeekOnEmptyStack() {
47+
NodeStack<String> stack = new NodeStack<>();
48+
assertThrows(IllegalStateException.class, stack::peek, "Peeking an empty stack should throw IllegalStateException.");
4749
}
4850

4951
@Test
50-
void testSize() {
51-
assertEquals(0, stack.size(), "New stack should have size 0");
52-
stack.push(10);
53-
stack.push(20);
54-
assertEquals(2, stack.size(), "Stack size should be 2 after two pushes");
52+
void testIsEmpty() {
53+
NodeStack<Character> stack = new NodeStack<>();
54+
assertTrue(stack.isEmpty(), "Newly initialized stack should be empty.");
55+
stack.push('A');
56+
assertFalse(stack.isEmpty(), "Stack should not be empty after a push operation.");
57+
stack.pop();
58+
assertTrue(stack.isEmpty(), "Stack should be empty after popping the only element.");
5559
}
5660

5761
@Test
58-
void testPopOnEmptyStack() {
59-
assertThrows(IllegalStateException.class, () -> { stack.pop(); });
62+
void testSize() {
63+
NodeStack<Integer> stack = new NodeStack<>();
64+
assertEquals(0, stack.size(), "Size of empty stack should be 0.");
65+
stack.push(3);
66+
stack.push(6);
67+
assertEquals(2, stack.size(), "Size should be 2 after pushing two elements.");
68+
stack.pop();
69+
assertEquals(1, stack.size(), "Size should be 1 after popping one element.");
70+
stack.pop();
71+
assertEquals(0, stack.size(), "Size should be 0 after popping all elements.");
6072
}
6173

6274
@Test
63-
void testPeekOnEmptyStack() {
64-
assertThrows(IllegalStateException.class, () -> { stack.peek(); });
75+
void testPrint() {
76+
NodeStack<Integer> stack = new NodeStack<>();
77+
stack.push(1);
78+
stack.push(2);
79+
stack.push(3);
80+
81+
// Output verification would ideally be handled through a different means
82+
// but you can print as a basic check to confirm method runs without errors.
83+
stack.print();
6584
}
6685
}

0 commit comments

Comments
 (0)