Skip to content

Commit dbfe367

Browse files
committed
Add tests for NodeStack and rework
1 parent 90d20b3 commit dbfe367

File tree

2 files changed

+114
-119
lines changed

2 files changed

+114
-119
lines changed

src/main/java/com/thealgorithms/datastructures/stacks/NodeStack.java

Lines changed: 28 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -3,159 +3,68 @@
33
/**
44
* Implementation of a stack using nodes. Unlimited size, no arraylist.
55
*
6-
* @author Kyler Smith, 2017
76
*/
87
public class NodeStack<Item> {
98

10-
/**
11-
* Entry point for the program.
12-
*/
13-
public static void main(String[] args) {
14-
NodeStack<Integer> stack = new NodeStack<Integer>();
15-
16-
stack.push(3);
17-
stack.push(4);
18-
stack.push(5);
19-
System.out.println("Testing :");
20-
stack.print(); // prints : 5 4 3
21-
22-
Integer x = stack.pop(); // x = 5
23-
stack.push(1);
24-
stack.push(8);
25-
Integer y = stack.peek(); // y = 8
26-
System.out.println("Testing :");
27-
stack.print(); // prints : 8 1 4 3
28-
29-
System.out.println("Testing :");
30-
System.out.println("x : " + x);
31-
System.out.println("y : " + y);
32-
}
33-
34-
/**
35-
* Information each node should contain.
36-
*
37-
* @value data : information of the value in the node
38-
* @value head : the head of the stack
39-
* @value next : the next value from this node
40-
* @value previous : the last value from this node
41-
* @value size : size of the stack
42-
*/
439
private Item data;
10+
private NodeStack<Item> previous;
11+
private NodeStack<Item> head;
12+
private int size = 0;
4413

45-
private static NodeStack<?> head;
46-
private NodeStack<?> previous;
47-
private static int size = 0;
48-
49-
/**
50-
* Constructors for the NodeStack.
51-
*/
5214
public NodeStack() {
5315
}
5416

5517
private NodeStack(Item item) {
5618
this.data = item;
5719
}
5820

59-
/**
60-
* Put a value onto the stack.
61-
*
62-
* @param item : value to be put on the stack.
63-
*/
6421
public void push(Item item) {
65-
NodeStack<Item> newNs = new NodeStack<Item>(item);
22+
NodeStack<Item> newNode = new NodeStack<>(item);
6623

67-
if (this.isEmpty()) {
68-
NodeStack.setHead(new NodeStack<>(item));
69-
newNs.setNext(null);
70-
newNs.setPrevious(null);
24+
if (isEmpty()) {
25+
head = newNode;
7126
} else {
72-
newNs.setPrevious(NodeStack.head);
73-
NodeStack.head.setNext(newNs);
74-
NodeStack.setHead(newNs);
27+
newNode.previous = head;
28+
head = newNode;
7529
}
76-
77-
NodeStack.setSize(NodeStack.getSize() + 1);
30+
size++;
7831
}
7932

80-
/**
81-
* Value to be taken off the stack.
82-
*
83-
* @return item : value that is returned.
84-
*/
8533
public Item pop() {
86-
Item item = (Item) NodeStack.head.getData();
87-
88-
NodeStack.setHead(NodeStack.head.getPrevious());
89-
NodeStack.head.setNext(null);
34+
if (isEmpty()) {
35+
throw new IllegalStateException("Stack is empty, cannot peek element");
36+
}
9037

91-
NodeStack.setSize(NodeStack.getSize() - 1);
38+
Item item = head.data;
39+
head = head.previous;
40+
if (head != null) {
41+
}
9242

43+
size--;
9344
return item;
9445
}
9546

96-
/**
97-
* Value that is next to be taken off the stack.
98-
*
99-
* @return item : the next value that would be popped off the stack.
100-
*/
10147
public Item peek() {
102-
return (Item) NodeStack.head.getData();
48+
if (isEmpty()) {
49+
throw new IllegalStateException("Stack is empty, cannot peek element");
50+
}
51+
return head.data;
10352
}
10453

105-
/**
106-
* If the stack is empty or there is a value in.
107-
*
108-
* @return boolean : whether or not the stack has anything in it.
109-
*/
11054
public boolean isEmpty() {
111-
return NodeStack.getSize() == 0;
55+
return size == 0;
11256
}
11357

114-
/**
115-
* Returns the size of the stack.
116-
*
117-
* @return int : number of values in the stack.
118-
*/
11958
public int size() {
120-
return NodeStack.getSize();
59+
return size;
12160
}
12261

123-
/**
124-
* Print the contents of the stack in the following format.
125-
*
126-
* <p>
127-
* x <- head (next out) y z <- tail (first in) . . .
128-
*/
12962
public void print() {
130-
for (NodeStack<?> n = NodeStack.head; n != null; n = n.previous) {
131-
System.out.println(n.getData().toString());
63+
NodeStack<Item> current = head;
64+
while (current != null) {
65+
System.out.println(current.data + " ");
66+
current = current.previous;
13267
}
133-
}
134-
135-
private static void setHead(NodeStack<?> ns) {
136-
NodeStack.head = ns;
137-
}
138-
139-
private void setNext(NodeStack<?> next) {
140-
}
141-
142-
private NodeStack<?> getPrevious() {
143-
return previous;
144-
}
145-
146-
private void setPrevious(NodeStack<?> previous) {
147-
this.previous = previous;
148-
}
149-
150-
private static int getSize() {
151-
return size;
152-
}
153-
154-
private static void setSize(int size) {
155-
NodeStack.size = size;
156-
}
157-
158-
private Item getData() {
159-
return this.data;
68+
System.out.println();
16069
}
16170
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.thealgorithms.datastructures.stacks;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
import java.io.ByteArrayOutputStream;
8+
import java.io.PrintStream;
9+
10+
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.Test;
12+
13+
14+
class NodeStackTest {
15+
16+
private NodeStack<Integer> stack;
17+
private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
18+
19+
@BeforeEach
20+
void setUp() {
21+
stack = new NodeStack<>();
22+
System.setOut(new PrintStream(outputStreamCaptor));
23+
}
24+
25+
@Test
26+
void testPushAndPeek() {
27+
stack.push(3);
28+
assertEquals(3, stack.peek(), "Peek should return the last pushed item");
29+
stack.push(4);
30+
stack.push(5);
31+
assertEquals(5, stack.peek(), "Peek should return the last pushed item");
32+
33+
}
34+
35+
@Test
36+
void testPop() {
37+
stack.push(5);
38+
stack.push(7);
39+
assertEquals(7, stack.pop(), "Pop should return the last pushed item");
40+
assertEquals(5, stack.pop(), "Pop should return the next item in stack");
41+
assertTrue(stack.isEmpty(), "Stack should be empty after popping all elements");
42+
}
43+
44+
@Test
45+
void testIsEmpty() {
46+
assertTrue(stack.isEmpty(), "New stack should be empty");
47+
stack.push(1);
48+
assertFalse(stack.isEmpty(), "Stack should not be empty after push");
49+
}
50+
51+
@Test
52+
void testSize() {
53+
assertEquals(0, stack.size(), "New stack should have size 0");
54+
stack.push(10);
55+
stack.push(20);
56+
assertEquals(2, stack.size(), "Stack size should be 2 after two pushes");
57+
}
58+
59+
@Test
60+
void testPopOnEmptyStack() {
61+
assertThrows(IllegalStateException.class, () -> { stack.pop(); });
62+
}
63+
64+
@Test
65+
void testPeekOnEmptyStack() {
66+
assertThrows(IllegalStateException.class, () -> { stack.peek(); });
67+
}
68+
69+
@Test
70+
void testPrintEmptyStack() {
71+
stack.print();
72+
assertEquals("", outputStreamCaptor.toString().trim(), "The output of an empty stack should be an empty string.");
73+
}
74+
75+
@Test
76+
void testPrintNonEmptyStack() {
77+
stack.push(3);
78+
stack.push(4);
79+
stack.push(5);
80+
81+
stack.print();
82+
83+
assertEquals("5 \n4 \n3", outputStreamCaptor.toString().trim(), "The stack output should match the expected values in LIFO order.");
84+
}
85+
}
86+

0 commit comments

Comments
 (0)