Skip to content

Commit 5a28d67

Browse files
author
Kay Hudson
committed
Solution with nodes.
1 parent e709ab1 commit 5a28d67

File tree

3 files changed

+73
-42
lines changed

3 files changed

+73
-42
lines changed

src/main/java/com/galvanize/Node.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.galvanize;
2+
3+
public class Node {
4+
public Object value;
5+
private Node next;
6+
7+
public Node(Object value) {
8+
this.value = value;
9+
}
10+
11+
public Node getNext() {
12+
return next;
13+
}
14+
15+
public void setNext(Node next) {
16+
this.next = next;
17+
}
18+
}
Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,39 @@
11
package com.galvanize;
22

3-
import java.util.ArrayList;
4-
53
public class Stack {
64
public boolean empty;
7-
private ArrayList items;
5+
public Node top;
6+
private int size;
87

98
public Stack() {
109
this.empty = true;
11-
this.items = new ArrayList();
10+
this.size = 0;
1211
}
1312

1413
public int count() {
15-
return this.items.size();
14+
return size;
1615
}
1716

18-
public void push(Object item) {
19-
this.items.add(item);
20-
this.empty = false;
17+
public void push(Node item) {
18+
if (top == null) {
19+
top = item;
20+
this.empty = false;
21+
} else {
22+
item.setNext(top);
23+
top = item;
24+
}
25+
this.size++;
2126
}
2227

23-
public Object pop() {
24-
int lastIndex = this.count() -1;
25-
Object lastItem = this.items.get(lastIndex);
26-
this.items.remove(lastIndex);
27-
return lastItem;
28+
public Node pop() {
29+
Node current = top;
30+
top = current.getNext();
31+
current.setNext(new Node(null));
32+
this.size--;
33+
return current;
2834
}
2935

30-
public Object peek() {
31-
int lastIndex = this.count() -1;
32-
return this.items.get(lastIndex);
36+
public Node peek() {
37+
return top;
3338
}
3439
}

src/test/java/StackTestCase.java

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,49 @@
1-
import org.junit.jupiter.api.DisplayName;
1+
import org.junit.jupiter.api.BeforeEach;
22
import org.junit.jupiter.api.Test;
33
import static org.junit.jupiter.api.Assertions.*;
44

55
import com.galvanize.Stack;
6+
import com.galvanize.Node;
67

78
class StackTestCase {
9+
Stack stack;
810

9-
@Test()
10-
@DisplayName("Stack.push() adds a new object")
11-
void testStackPush() {
12-
Stack newStack = new Stack();
13-
assertEquals(0, newStack.count(),"Stack should be empty on initialization");
14-
newStack.push(100);
15-
assertEquals(1, newStack.count(), "Stack should contain only 1 object.");
16-
assertFalse(newStack.empty, "Stack should not be empty.");
11+
@BeforeEach
12+
void setUp() {
13+
stack = new Stack();
1714
}
1815

1916
@Test
20-
@DisplayName("Stack.pop() removes and returns last item added.")
21-
void testStackPopRemovesAndReturnsLastItemIn() {
22-
Stack newStack = new Stack();
23-
newStack.push(1);
24-
newStack.push(2);
25-
newStack.push(3);
26-
assertEquals(3, newStack.count(), "Stack should contain 3 objects.");
27-
Object item = newStack.pop();
28-
assertEquals(2, newStack.count(), "Stack should contain only 2 items after .pop()");
29-
assertEquals(3, item, "The removed object should have a value of 3");
17+
void testStackPushNode () {
18+
stack.push(new Node(100));
19+
assertEquals(1, stack.count());
3020
}
3121

3222
@Test
33-
@DisplayName("Stack.peek() returns the last item added without removing it.")
34-
void testStackPeek() {
35-
Stack newStack = new Stack();
36-
newStack.push("sock");
37-
newStack.push("shoe");
38-
assertEquals("shoe", newStack.peek(), "The last object should be 'shoe'.");
39-
assertEquals(2, newStack.count(), "The stack size should be unchanged after peek().");
23+
void testStackPopRemovesNode_LIFO() {
24+
stack.push(new Node(200));
25+
stack.push(new Node(300));
26+
Node lastIn = new Node(400);
27+
stack.push(lastIn);
28+
assertEquals(3, stack.count());
29+
30+
Node firstOut = stack.pop();
31+
assertEquals(2, stack.count());
32+
assertEquals(lastIn, firstOut);
33+
}
34+
35+
@Test
36+
void testStackPeekReturnsTopNode() {
37+
Node item = new Node(200);
38+
Node topItem = new Node(300);
39+
stack.push(item);
40+
stack.push(topItem);
41+
Node peekItem = stack.peek();
42+
assertEquals(topItem, peekItem);
43+
}
44+
45+
@Test
46+
void testStackEmptyTrue() {
47+
assertTrue(stack.empty);
4048
}
4149
}

0 commit comments

Comments
 (0)