Skip to content

Commit d4dc3da

Browse files
author
Kay Hudson
committed
Add linkedlist example.
1 parent 7ab3111 commit d4dc3da

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.galvanize;
2+
3+
public class LinkedList {
4+
private Node head;
5+
private int size;
6+
7+
public LinkedList() {
8+
this.size = 0;
9+
}
10+
11+
public boolean isEmpty() {
12+
return head == null;
13+
}
14+
public int count() {
15+
return this.size;
16+
}
17+
18+
public void addNode(Node node) {
19+
if (head == null) {
20+
head = node;
21+
} else {
22+
Node current = head;
23+
while (current.getNext() != null) {
24+
current = current.getNext();
25+
}
26+
current.setNext(node);
27+
}
28+
this.size++;
29+
}
30+
31+
public Node getHead() {
32+
return head;
33+
}
34+
35+
public void setHead(Node head) {
36+
this.head = head;
37+
}
38+
39+
public Node find(Node node) {
40+
if (head == node) {
41+
return head;
42+
}
43+
Node current = head.getNext();
44+
while (current != null) {
45+
if (current == node) return current;
46+
current = current.getNext();
47+
}
48+
return null;
49+
}
50+
51+
public void removeNode(Node node) {
52+
if (head != null && head == node) {
53+
head = node;
54+
this.size--;
55+
} else {
56+
Node previous = head;
57+
Node current = head.getNext();
58+
while (current != null) {
59+
if (current == node) {
60+
previous.setNext(current.getNext());
61+
this.size--;
62+
break;
63+
} else {
64+
previous = current;
65+
current = current.getNext();
66+
}
67+
}
68+
}
69+
}
70+
}

src/test/java/LinkedListTestCase.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import com.galvanize.LinkedList;
2+
import com.galvanize.Node;
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
public class LinkedListTestCase {
8+
LinkedList linkedList;
9+
10+
@BeforeEach
11+
void setUp() {
12+
linkedList = new LinkedList();
13+
}
14+
15+
@Test
16+
void testListEmpty() {
17+
assertTrue(linkedList.isEmpty());
18+
}
19+
20+
@Test
21+
void testAddNode() {
22+
Node node = new Node("Sword");
23+
linkedList.addNode(node);
24+
assertEquals(node, linkedList.getHead());
25+
}
26+
27+
@Test
28+
void testAddManyNodesChangesSize() {
29+
linkedList.addNode(new Node("A"));
30+
linkedList.addNode(new Node("B"));
31+
linkedList.addNode(new Node("C"));
32+
assertEquals(3, linkedList.count());
33+
}
34+
35+
@Test
36+
void testFindNode() {
37+
Node aNode = new Node("A");
38+
Node bNode = new Node("B");
39+
linkedList.addNode(aNode);
40+
linkedList.addNode(bNode);
41+
Node result = linkedList.find(aNode);
42+
assertEquals(aNode, result);
43+
}
44+
45+
@Test
46+
void testRemoveNodeSuccess() {
47+
Node aNode = new Node("A");
48+
Node bNode = new Node("B");
49+
Node cNode = new Node("C");
50+
Node dNode = new Node("D");
51+
linkedList.addNode(aNode);
52+
linkedList.addNode(bNode);
53+
linkedList.addNode(cNode);
54+
linkedList.addNode(dNode);
55+
linkedList.removeNode(cNode);
56+
assertEquals(3, linkedList.count());
57+
assertEquals(bNode.getNext(), dNode);
58+
}
59+
}

0 commit comments

Comments
 (0)