From dbfe367f87c439c4713a68d2756d75fa7fcd5d81 Mon Sep 17 00:00:00 2001 From: lorduke22 Date: Fri, 25 Oct 2024 21:58:23 +0200 Subject: [PATCH 1/4] Add tests for NodeStack and rework --- .../datastructures/stacks/NodeStack.java | 147 ++++-------------- .../datastructures/stacks/NodeStackTest.java | 86 ++++++++++ 2 files changed, 114 insertions(+), 119 deletions(-) create mode 100644 src/test/java/com/thealgorithms/datastructures/stacks/NodeStackTest.java diff --git a/src/main/java/com/thealgorithms/datastructures/stacks/NodeStack.java b/src/main/java/com/thealgorithms/datastructures/stacks/NodeStack.java index 7c4f334cd61..b5df9dc5a8d 100644 --- a/src/main/java/com/thealgorithms/datastructures/stacks/NodeStack.java +++ b/src/main/java/com/thealgorithms/datastructures/stacks/NodeStack.java @@ -3,52 +3,14 @@ /** * Implementation of a stack using nodes. Unlimited size, no arraylist. * - * @author Kyler Smith, 2017 */ public class NodeStack { - /** - * Entry point for the program. - */ - public static void main(String[] args) { - NodeStack stack = new NodeStack(); - - stack.push(3); - stack.push(4); - stack.push(5); - System.out.println("Testing :"); - stack.print(); // prints : 5 4 3 - - Integer x = stack.pop(); // x = 5 - stack.push(1); - stack.push(8); - Integer y = stack.peek(); // y = 8 - System.out.println("Testing :"); - stack.print(); // prints : 8 1 4 3 - - System.out.println("Testing :"); - System.out.println("x : " + x); - System.out.println("y : " + y); - } - - /** - * Information each node should contain. - * - * @value data : information of the value in the node - * @value head : the head of the stack - * @value next : the next value from this node - * @value previous : the last value from this node - * @value size : size of the stack - */ private Item data; + private NodeStack previous; + private NodeStack head; + private int size = 0; - private static NodeStack head; - private NodeStack previous; - private static int size = 0; - - /** - * Constructors for the NodeStack. - */ public NodeStack() { } @@ -56,106 +18,53 @@ private NodeStack(Item item) { this.data = item; } - /** - * Put a value onto the stack. - * - * @param item : value to be put on the stack. - */ public void push(Item item) { - NodeStack newNs = new NodeStack(item); + NodeStack newNode = new NodeStack<>(item); - if (this.isEmpty()) { - NodeStack.setHead(new NodeStack<>(item)); - newNs.setNext(null); - newNs.setPrevious(null); + if (isEmpty()) { + head = newNode; } else { - newNs.setPrevious(NodeStack.head); - NodeStack.head.setNext(newNs); - NodeStack.setHead(newNs); + newNode.previous = head; + head = newNode; } - - NodeStack.setSize(NodeStack.getSize() + 1); + size++; } - /** - * Value to be taken off the stack. - * - * @return item : value that is returned. - */ public Item pop() { - Item item = (Item) NodeStack.head.getData(); - - NodeStack.setHead(NodeStack.head.getPrevious()); - NodeStack.head.setNext(null); + if (isEmpty()) { + throw new IllegalStateException("Stack is empty, cannot peek element"); + } - NodeStack.setSize(NodeStack.getSize() - 1); + Item item = head.data; + head = head.previous; + if (head != null) { + } + size--; return item; } - /** - * Value that is next to be taken off the stack. - * - * @return item : the next value that would be popped off the stack. - */ public Item peek() { - return (Item) NodeStack.head.getData(); + if (isEmpty()) { + throw new IllegalStateException("Stack is empty, cannot peek element"); + } + return head.data; } - /** - * If the stack is empty or there is a value in. - * - * @return boolean : whether or not the stack has anything in it. - */ public boolean isEmpty() { - return NodeStack.getSize() == 0; + return size == 0; } - /** - * Returns the size of the stack. - * - * @return int : number of values in the stack. - */ public int size() { - return NodeStack.getSize(); + return size; } - /** - * Print the contents of the stack in the following format. - * - *

- * x <- head (next out) y z <- tail (first in) . . . - */ public void print() { - for (NodeStack n = NodeStack.head; n != null; n = n.previous) { - System.out.println(n.getData().toString()); + NodeStack current = head; + while (current != null) { + System.out.println(current.data + " "); + current = current.previous; } - } - - private static void setHead(NodeStack ns) { - NodeStack.head = ns; - } - - private void setNext(NodeStack next) { - } - - private NodeStack getPrevious() { - return previous; - } - - private void setPrevious(NodeStack previous) { - this.previous = previous; - } - - private static int getSize() { - return size; - } - - private static void setSize(int size) { - NodeStack.size = size; - } - - private Item getData() { - return this.data; + System.out.println(); } } diff --git a/src/test/java/com/thealgorithms/datastructures/stacks/NodeStackTest.java b/src/test/java/com/thealgorithms/datastructures/stacks/NodeStackTest.java new file mode 100644 index 00000000000..8c0eddaa84b --- /dev/null +++ b/src/test/java/com/thealgorithms/datastructures/stacks/NodeStackTest.java @@ -0,0 +1,86 @@ +package com.thealgorithms.datastructures.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 java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + + +class NodeStackTest { + + private NodeStack stack; + private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream(); + + @BeforeEach + void setUp() { + stack = new NodeStack<>(); + System.setOut(new PrintStream(outputStreamCaptor)); + } + + @Test + void testPushAndPeek() { + stack.push(3); + assertEquals(3, stack.peek(), "Peek should return the last pushed item"); + stack.push(4); + stack.push(5); + assertEquals(5, stack.peek(), "Peek should return the last pushed item"); + + } + + @Test + void testPop() { + stack.push(5); + stack.push(7); + assertEquals(7, stack.pop(), "Pop should return the last pushed item"); + assertEquals(5, stack.pop(), "Pop should return the next item in stack"); + assertTrue(stack.isEmpty(), "Stack should be empty after popping all elements"); + } + + @Test + void testIsEmpty() { + assertTrue(stack.isEmpty(), "New stack should be empty"); + stack.push(1); + assertFalse(stack.isEmpty(), "Stack should not be empty after push"); + } + + @Test + void testSize() { + assertEquals(0, stack.size(), "New stack should have size 0"); + stack.push(10); + stack.push(20); + assertEquals(2, stack.size(), "Stack size should be 2 after two pushes"); + } + + @Test + void testPopOnEmptyStack() { + assertThrows(IllegalStateException.class, () -> { stack.pop(); }); + } + + @Test + void testPeekOnEmptyStack() { + assertThrows(IllegalStateException.class, () -> { stack.peek(); }); + } + + @Test + void testPrintEmptyStack() { + stack.print(); + assertEquals("", outputStreamCaptor.toString().trim(), "The output of an empty stack should be an empty string."); + } + + @Test + void testPrintNonEmptyStack() { + stack.push(3); + stack.push(4); + stack.push(5); + + stack.print(); + + assertEquals("5 \n4 \n3", outputStreamCaptor.toString().trim(), "The stack output should match the expected values in LIFO order."); + } +} + From 38e01f78f4aa28942c97b9eeb93d44c254281d9f Mon Sep 17 00:00:00 2001 From: lorduke22 Date: Fri, 25 Oct 2024 22:06:33 +0200 Subject: [PATCH 2/4] Fix clang format --- .../datastructures/stacks/NodeStackTest.java | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/src/test/java/com/thealgorithms/datastructures/stacks/NodeStackTest.java b/src/test/java/com/thealgorithms/datastructures/stacks/NodeStackTest.java index 8c0eddaa84b..fe25f737813 100644 --- a/src/test/java/com/thealgorithms/datastructures/stacks/NodeStackTest.java +++ b/src/test/java/com/thealgorithms/datastructures/stacks/NodeStackTest.java @@ -4,13 +4,12 @@ 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 java.io.ByteArrayOutputStream; import java.io.PrintStream; - import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; - class NodeStackTest { private NodeStack stack; @@ -29,7 +28,6 @@ void testPushAndPeek() { stack.push(4); stack.push(5); assertEquals(5, stack.peek(), "Peek should return the last pushed item"); - } @Test @@ -71,16 +69,4 @@ void testPrintEmptyStack() { stack.print(); assertEquals("", outputStreamCaptor.toString().trim(), "The output of an empty stack should be an empty string."); } - - @Test - void testPrintNonEmptyStack() { - stack.push(3); - stack.push(4); - stack.push(5); - - stack.print(); - - assertEquals("5 \n4 \n3", outputStreamCaptor.toString().trim(), "The stack output should match the expected values in LIFO order."); - } } - From 1e973753a920ad1f0a1e2d0ad101a8209e5ba600 Mon Sep 17 00:00:00 2001 From: lorduke22 Date: Fri, 25 Oct 2024 22:16:15 +0200 Subject: [PATCH 3/4] Fix build --- .../com/thealgorithms/datastructures/stacks/NodeStack.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/stacks/NodeStack.java b/src/main/java/com/thealgorithms/datastructures/stacks/NodeStack.java index b5df9dc5a8d..5d9c8eeee39 100644 --- a/src/main/java/com/thealgorithms/datastructures/stacks/NodeStack.java +++ b/src/main/java/com/thealgorithms/datastructures/stacks/NodeStack.java @@ -37,9 +37,6 @@ public Item pop() { Item item = head.data; head = head.previous; - if (head != null) { - } - size--; return item; } From 3cd1ce4cf8f8bbd0b6cfe7452c27e21e5823d658 Mon Sep 17 00:00:00 2001 From: lorduke22 Date: Fri, 25 Oct 2024 22:25:28 +0200 Subject: [PATCH 4/4] Remove print function --- .../thealgorithms/datastructures/stacks/NodeStack.java | 9 --------- .../datastructures/stacks/NodeStackTest.java | 6 ------ 2 files changed, 15 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/stacks/NodeStack.java b/src/main/java/com/thealgorithms/datastructures/stacks/NodeStack.java index 5d9c8eeee39..72ffdc87602 100644 --- a/src/main/java/com/thealgorithms/datastructures/stacks/NodeStack.java +++ b/src/main/java/com/thealgorithms/datastructures/stacks/NodeStack.java @@ -55,13 +55,4 @@ public boolean isEmpty() { public int size() { return size; } - - public void print() { - NodeStack current = head; - while (current != null) { - System.out.println(current.data + " "); - current = current.previous; - } - System.out.println(); - } } diff --git a/src/test/java/com/thealgorithms/datastructures/stacks/NodeStackTest.java b/src/test/java/com/thealgorithms/datastructures/stacks/NodeStackTest.java index fe25f737813..af49ef24085 100644 --- a/src/test/java/com/thealgorithms/datastructures/stacks/NodeStackTest.java +++ b/src/test/java/com/thealgorithms/datastructures/stacks/NodeStackTest.java @@ -63,10 +63,4 @@ void testPopOnEmptyStack() { void testPeekOnEmptyStack() { assertThrows(IllegalStateException.class, () -> { stack.peek(); }); } - - @Test - void testPrintEmptyStack() { - stack.print(); - assertEquals("", outputStreamCaptor.toString().trim(), "The output of an empty stack should be an empty string."); - } }