From 3987255e928077b5a00293fefd9f83d30e95083a Mon Sep 17 00:00:00 2001 From: jrtechs Date: Sat, 25 Aug 2018 08:57:24 -0400 Subject: [PATCH] Refactored and finalized the linked list method to include a remove by element method. --- .../{ => Lists/LinkedList}/IList.java | 11 +- .../{ => Lists}/LinkedList/LinkedList.java | 104 ++++++++++++++++-- .../{LinkedList => Lists}/LinkedListTest.java | 33 +++++- .../jrtechs/www/DataStructures/NodeTest.java | 1 - 4 files changed, 131 insertions(+), 18 deletions(-) rename src/main/java/net/jrtechs/www/DataStructures/{ => Lists/LinkedList}/IList.java (82%) rename src/main/java/net/jrtechs/www/DataStructures/{ => Lists}/LinkedList/LinkedList.java (51%) rename src/test/java/net/jrtechs/www/DataStructures/{LinkedList => Lists}/LinkedListTest.java (70%) diff --git a/src/main/java/net/jrtechs/www/DataStructures/IList.java b/src/main/java/net/jrtechs/www/DataStructures/Lists/LinkedList/IList.java similarity index 82% rename from src/main/java/net/jrtechs/www/DataStructures/IList.java rename to src/main/java/net/jrtechs/www/DataStructures/Lists/LinkedList/IList.java index 31a93c3..d66f5e6 100644 --- a/src/main/java/net/jrtechs/www/DataStructures/IList.java +++ b/src/main/java/net/jrtechs/www/DataStructures/Lists/LinkedList/IList.java @@ -1,4 +1,4 @@ -package net.jrtechs.www.DataStructures; +package net.jrtechs.www.DataStructures.Lists.LinkedList; /** @@ -47,6 +47,15 @@ public interface IList public E remove(int index); + /** + * Removes an element from the list + * + * @param o element to remove + * @return element which is removed + */ + public E remove(E o); + + /** * Returns element at a particular index * diff --git a/src/main/java/net/jrtechs/www/DataStructures/LinkedList/LinkedList.java b/src/main/java/net/jrtechs/www/DataStructures/Lists/LinkedList/LinkedList.java similarity index 51% rename from src/main/java/net/jrtechs/www/DataStructures/LinkedList/LinkedList.java rename to src/main/java/net/jrtechs/www/DataStructures/Lists/LinkedList/LinkedList.java index e0d1731..12b6982 100644 --- a/src/main/java/net/jrtechs/www/DataStructures/LinkedList/LinkedList.java +++ b/src/main/java/net/jrtechs/www/DataStructures/Lists/LinkedList/LinkedList.java @@ -1,30 +1,29 @@ -package net.jrtechs.www.DataStructures.LinkedList; +package net.jrtechs.www.DataStructures.Lists.LinkedList; -import net.jrtechs.www.DataStructures.IList; import net.jrtechs.www.DataStructures.Node; /** + * Implementation of a linked list. + * * @author Jeffery Russell */ public class LinkedList implements IList { + /** Head node */ private Node head; + /**Stored to make insertions o(1) */ private Node tail; - + /** Keeps track of size so it doesn't + * have to be re-calculated each time it's used */ private int size; - public LinkedList(E o) - { - this.head = new Node<>(o); - this.tail = head; - this.size = 1; - } - - + /** + * Creates a new empty linked list + */ public LinkedList() { this.head = null; @@ -33,6 +32,27 @@ public class LinkedList implements IList } + /** + * Creates a new linked list with an + * initial element stored inside of it + * + * @param o initial element + */ + public LinkedList(E o) + { + this.head = new Node<>(o); + this.tail = head; + this.size = 1; + } + + + /** + * Adds a data element to the linked list. + * + * @param o element to get added + * @return + */ + @Override public boolean add(E o) { this.size++; @@ -51,6 +71,13 @@ public class LinkedList implements IList } + /** + * Checks to see if list contains the element + * + * @param o element to see if exists + * @return + */ + @Override public boolean contains(E o) { Node current = this.head; @@ -67,16 +94,28 @@ public class LinkedList implements IList } + /** + * Returns the size of the linked list + * + * @return + */ public int size() { return this.size; } + /** + * Removes an element at a specific index in the + * linked list + * + * @param index of element to remove + * @return element removed, or null if the + * element is not found + */ public E remove(int index) { int count = 0; - Node current = this.head; Node previous = null; while(current != null) @@ -102,6 +141,47 @@ public class LinkedList implements IList } + /** + * Removes a specific element from the linked list + * + * @param o element to remove + * @return element removed or null if element is not + * in the list + */ + @Override + public E remove(E o) + { + Node current = this.head; + Node previous = null; + while(current != null) + { + if(current.getData().equals(o)) + { + if(previous == null) + { + this.head = current.getNext(); + } + else + { + previous.setNext(current.getNext()); + } + this.size--; + return current.getData(); + } + previous = current; + current = current.getNext(); + } + return null; + } + + + /** + * Returns the element at a specific index + * of the linked list. + * + * @param index of desired element + * @return + */ public E get(int index) { int count = 0; diff --git a/src/test/java/net/jrtechs/www/DataStructures/LinkedList/LinkedListTest.java b/src/test/java/net/jrtechs/www/DataStructures/Lists/LinkedListTest.java similarity index 70% rename from src/test/java/net/jrtechs/www/DataStructures/LinkedList/LinkedListTest.java rename to src/test/java/net/jrtechs/www/DataStructures/Lists/LinkedListTest.java index 7581981..d2f00d1 100644 --- a/src/test/java/net/jrtechs/www/DataStructures/LinkedList/LinkedListTest.java +++ b/src/test/java/net/jrtechs/www/DataStructures/Lists/LinkedListTest.java @@ -1,6 +1,7 @@ -package net.jrtechs.www.DataStructures.LinkedList; +package net.jrtechs.www.DataStructures.Lists; +import net.jrtechs.www.DataStructures.Lists.LinkedList.LinkedList; import org.junit.Test; import static org.junit.Assert.assertFalse; @@ -66,10 +67,10 @@ public class LinkedListTest /** - * Testing addition of data elements + * Testing deletion of data elements */ @Test - public void testDeletion() + public void testDeletionByIndex() { LinkedList list = new LinkedList<>(); list.add(12.0); @@ -86,11 +87,35 @@ public class LinkedListTest list.remove(0); list.remove(1); - assertFalse(list.contains(12.0)); assertFalse(list.contains(44.9)); assertTrue(list.contains(13.0)); } + /** + * Tests linked deletion based on element + */ + @Test + public void testDeletionByElement() + { + 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(12.0); + list.remove(44.9); + + 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 index c77a0b1..bad566b 100644 --- a/src/test/java/net/jrtechs/www/DataStructures/NodeTest.java +++ b/src/test/java/net/jrtechs/www/DataStructures/NodeTest.java @@ -4,7 +4,6 @@ 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;