diff --git a/src/main/java/net/jrtechs/www/DataStructures/LinkedList/LinkedList.java b/src/main/java/net/jrtechs/www/DataStructures/LinkedList/LinkedList.java index fafab93..e0d1731 100644 --- a/src/main/java/net/jrtechs/www/DataStructures/LinkedList/LinkedList.java +++ b/src/main/java/net/jrtechs/www/DataStructures/LinkedList/LinkedList.java @@ -11,6 +11,8 @@ public class LinkedList implements IList { private Node head; + private Node tail; + private int size; @@ -18,6 +20,7 @@ public class LinkedList implements IList public LinkedList(E o) { this.head = new Node<>(o); + this.tail = head; this.size = 1; } @@ -25,20 +28,26 @@ public class LinkedList implements IList public LinkedList() { this.head = null; + this.tail = null; this.size = 0; } public boolean add(E o) { - Node current = this.head; - while(current != null) - { - current = current.getNext(); - } this.size++; - return false; + if(this.head == null) + { + this.head = new Node(o); + this.tail = this.head; + } + else + { + this.tail.setNext(new Node(o)); + this.tail = this.tail.getNext(); + } + return true; } @@ -47,8 +56,9 @@ public class LinkedList implements IList Node current = this.head; while(current != null) { - if(current.equals(o)) + if(current.getData().equals(o)) { + return true; } current = current.getNext(); @@ -74,13 +84,19 @@ public class LinkedList implements IList if(count == index) { if(previous == null) - this.head = current; - previous.setNext(current.getNext()); + { + this.head = current.getNext(); + } + else + { + previous.setNext(current.getNext()); + } this.size--; return current.getData(); } previous = current; current = current.getNext(); + count++; } return null; } diff --git a/src/test/java/net/jrtechs/www/AppTest.java b/src/test/java/net/jrtechs/www/AppTest.java new file mode 100644 index 0000000..13ed9ed --- /dev/null +++ b/src/test/java/net/jrtechs/www/AppTest.java @@ -0,0 +1,20 @@ +package net.jrtechs.www; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +/** + * Unit test for simple App. + */ +public class AppTest +{ + /** + * Rigorous Test :-) + */ + @Test + public void shouldAnswerWithTrue() + { + assertTrue( true ); + } +} diff --git a/src/test/java/net/jrtechs/www/DataStructures/LinkedList/LinkedListTest.java b/src/test/java/net/jrtechs/www/DataStructures/LinkedList/LinkedListTest.java new file mode 100644 index 0000000..7581981 --- /dev/null +++ b/src/test/java/net/jrtechs/www/DataStructures/LinkedList/LinkedListTest.java @@ -0,0 +1,96 @@ +package net.jrtechs.www.DataStructures.LinkedList; + + +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +/** + * @author Jeffery Russell 8-24-18 + */ +public class LinkedListTest +{ + /** + * Makes sure that instantiations work + */ + @Test + public void testCreation() + { + assertNotNull(new LinkedList()); + + assertNotNull(new LinkedList(12.2)); + } + + + /** + * Test Size of linked list + */ + @Test + public void testSize() + { + LinkedList list = new LinkedList<>(); + list.add(12.0); + assertTrue(list.size() == 1); + list.add(13.0); + assertTrue(list.size() == 2); + } + + + /** + * Testing addition of data elements + */ + @Test + public void testAddition() + { + LinkedList list = new LinkedList<>(); + list.add(12.0); + list.add(13.0); + list.add(44.9); + + assertTrue(list.contains(44.9)); + assertTrue(list.contains(12.0)); + assertTrue(list.contains(13.0)); + + assertTrue(list.size() == 3); + + + LinkedList list2 = new LinkedList<>(16.0); + list2.add(18.0); + + assertTrue(list2.contains(16.0)); + assertTrue(list2.contains(18.0)); + assertTrue(list2.size() == 2); + } + + + /** + * Testing addition of data elements + */ + @Test + public void testDeletion() + { + LinkedList list = new LinkedList<>(); + list.add(12.0); + list.add(13.0); + list.add(44.9); + + assertTrue(list.contains(44.9)); + assertTrue(list.contains(12.0)); + assertTrue(list.contains(13.0)); + + assertTrue(list.size() == 3); + + + list.remove(0); + list.remove(1); + + + assertFalse(list.contains(12.0)); + assertFalse(list.contains(44.9)); + assertTrue(list.contains(13.0)); + } + + +} diff --git a/src/test/java/net/jrtechs/www/DataStructures/NodeTest.java b/src/test/java/net/jrtechs/www/DataStructures/NodeTest.java new file mode 100644 index 0000000..c77a0b1 --- /dev/null +++ b/src/test/java/net/jrtechs/www/DataStructures/NodeTest.java @@ -0,0 +1,38 @@ +package net.jrtechs.www.DataStructures; + + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import net.jrtechs.www.DataStructures.LinkedList.LinkedList; +import org.junit.Test; + + + +public class NodeTest +{ + + /** + * Rigorous Test :-) + */ + @Test + public void testCreation() + { + assertNotNull(new Node(12.0)); + + assertNotNull(new Node(12.2, new Node(13.0))); + } + + @Test + public void testSetNext() + { + Node node = new Node<>(13.0); + + Node node2 = new Node<>(14.5); + + node.setNext(node2); + + assertTrue(node.getNext().getData().equals(14.5)); + } + +}