Browse Source

Implemented a basic linked list

master
jrtechs 6 years ago
parent
commit
2076a3ecc5
4 changed files with 218 additions and 0 deletions
  1. +13
    -0
      src/main/java/net/jrtechs/www/App.java
  2. +57
    -0
      src/main/java/net/jrtechs/www/DataStructures/IList.java
  3. +104
    -0
      src/main/java/net/jrtechs/www/DataStructures/LinkedList/LinkedList.java
  4. +44
    -0
      src/main/java/net/jrtechs/www/DataStructures/Node.java

+ 13
- 0
src/main/java/net/jrtechs/www/App.java View File

@ -0,0 +1,13 @@
package net.jrtechs.www;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}

+ 57
- 0
src/main/java/net/jrtechs/www/DataStructures/IList.java View File

@ -0,0 +1,57 @@
package net.jrtechs.www.DataStructures;
/**
* Generic interface to define the behavior of
* lists.
*
* @param <E> generic for lists to store
*
* @author Jeffery Russell 8-24-18
*/
public interface IList<E>
{
/**
* Adds an element to the list
*
* @param o element to get added
* @return whether element was edded
*/
public boolean add(E o);
/**
* Determines if the list contains
* a certain element.
*
* @param o element to see if exists
* @return if element o is in the list
*/
public boolean contains(E o);
/**
* Returns the size of the list.
*
* @return size of list
*/
public int size();
/**
* Removes an element from the list
*
* @param index of element to remove
* @return element which is removed
*/
public E remove(int index);
/**
* Returns element at a particular index
*
* @param index of desired element
* @return element at a certain index
*/
public E get(int index);
}

+ 104
- 0
src/main/java/net/jrtechs/www/DataStructures/LinkedList/LinkedList.java View File

@ -0,0 +1,104 @@
package net.jrtechs.www.DataStructures.LinkedList;
import net.jrtechs.www.DataStructures.IList;
import net.jrtechs.www.DataStructures.Node;
/**
* @author Jeffery Russell
*/
public class LinkedList<E> implements IList<E>
{
private Node<E> head;
private int size;
public LinkedList(E o)
{
this.head = new Node<>(o);
this.size = 1;
}
public LinkedList()
{
this.head = null;
this.size = 0;
}
public boolean add(E o)
{
Node current = this.head;
while(current != null)
{
current = current.getNext();
}
this.size++;
return false;
}
public boolean contains(E o)
{
Node current = this.head;
while(current != null)
{
if(current.equals(o))
{
return true;
}
current = current.getNext();
}
return false;
}
public int size()
{
return this.size;
}
public E remove(int index)
{
int count = 0;
Node<E> current = this.head;
Node<E> previous = null;
while(current != null)
{
if(count == index)
{
if(previous == null)
this.head = current;
previous.setNext(current.getNext());
this.size--;
return current.getData();
}
previous = current;
current = current.getNext();
}
return null;
}
public E get(int index)
{
int count = 0;
Node<E> current = this.head;
while(current != null)
{
if(count == index)
{
return current.getData();
}
current = current.getNext();
}
return null;
}
}

+ 44
- 0
src/main/java/net/jrtechs/www/DataStructures/Node.java View File

@ -0,0 +1,44 @@
package net.jrtechs.www.DataStructures;
/**
* @author Jeffery Russell 8-24-18
*/
/**
*
* @param <E>
*/
public class Node<E>
{
private E data;
private Node<E> next;
public Node(E d)
{
this.data = d;
this.next = null;
}
public Node(E d, Node<E> newNext)
{
this.data = d;
this.next = newNext;
}
public Node<E> getNext()
{
return this.next;
}
public E getData()
{
return data;
}
public void setNext(Node<E> newNext)
{
this.next = newNext;
}
}

Loading…
Cancel
Save