Browse Source

Refactored and finalized the linked list method to include a remove by element method.

master
jrtechs 6 years ago
parent
commit
3987255e92
4 changed files with 131 additions and 18 deletions
  1. +10
    -1
      src/main/java/net/jrtechs/www/DataStructures/Lists/LinkedList/IList.java
  2. +92
    -12
      src/main/java/net/jrtechs/www/DataStructures/Lists/LinkedList/LinkedList.java
  3. +29
    -4
      src/test/java/net/jrtechs/www/DataStructures/Lists/LinkedListTest.java
  4. +0
    -1
      src/test/java/net/jrtechs/www/DataStructures/NodeTest.java

src/main/java/net/jrtechs/www/DataStructures/IList.java → src/main/java/net/jrtechs/www/DataStructures/Lists/LinkedList/IList.java View File

@ -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
*

src/main/java/net/jrtechs/www/DataStructures/LinkedList/LinkedList.java → src/main/java/net/jrtechs/www/DataStructures/Lists/LinkedList/LinkedList.java View File

@ -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<E> implements IList<E>
{
/** Head node */
private Node<E> head;
/**Stored to make insertions o(1) */
private Node<E> 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<E> current = this.head;
Node<E> 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<E> current = this.head;
Node<E> 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;

src/test/java/net/jrtechs/www/DataStructures/LinkedList/LinkedListTest.java → src/test/java/net/jrtechs/www/DataStructures/Lists/LinkedListTest.java View File

@ -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<Double> 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<Double> 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));
}
}

+ 0
- 1
src/test/java/net/jrtechs/www/DataStructures/NodeTest.java View File

@ -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;

Loading…
Cancel
Save