From 2076a3ecc5e6859a9457b49e92399f5d252013f4 Mon Sep 17 00:00:00 2001 From: jrtechs Date: Fri, 24 Aug 2018 18:57:22 -0400 Subject: [PATCH] Implemented a basic linked list --- src/main/java/net/jrtechs/www/App.java | 13 +++ .../net/jrtechs/www/DataStructures/IList.java | 57 ++++++++++ .../DataStructures/LinkedList/LinkedList.java | 104 ++++++++++++++++++ .../net/jrtechs/www/DataStructures/Node.java | 44 ++++++++ 4 files changed, 218 insertions(+) create mode 100644 src/main/java/net/jrtechs/www/App.java create mode 100644 src/main/java/net/jrtechs/www/DataStructures/IList.java create mode 100644 src/main/java/net/jrtechs/www/DataStructures/LinkedList/LinkedList.java create mode 100644 src/main/java/net/jrtechs/www/DataStructures/Node.java diff --git a/src/main/java/net/jrtechs/www/App.java b/src/main/java/net/jrtechs/www/App.java new file mode 100644 index 0000000..6a6c180 --- /dev/null +++ b/src/main/java/net/jrtechs/www/App.java @@ -0,0 +1,13 @@ +package net.jrtechs.www; + +/** + * Hello world! + * + */ +public class App +{ + public static void main( String[] args ) + { + System.out.println( "Hello World!" ); + } +} diff --git a/src/main/java/net/jrtechs/www/DataStructures/IList.java b/src/main/java/net/jrtechs/www/DataStructures/IList.java new file mode 100644 index 0000000..31a93c3 --- /dev/null +++ b/src/main/java/net/jrtechs/www/DataStructures/IList.java @@ -0,0 +1,57 @@ +package net.jrtechs.www.DataStructures; + + +/** + * Generic interface to define the behavior of + * lists. + * + * @param generic for lists to store + * + * @author Jeffery Russell 8-24-18 + */ +public interface IList +{ + /** + * 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); +} diff --git a/src/main/java/net/jrtechs/www/DataStructures/LinkedList/LinkedList.java b/src/main/java/net/jrtechs/www/DataStructures/LinkedList/LinkedList.java new file mode 100644 index 0000000..fafab93 --- /dev/null +++ b/src/main/java/net/jrtechs/www/DataStructures/LinkedList/LinkedList.java @@ -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 implements IList +{ + private Node 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 current = this.head; + Node 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 current = this.head; + while(current != null) + { + if(count == index) + { + return current.getData(); + } + current = current.getNext(); + } + return null; + } +} diff --git a/src/main/java/net/jrtechs/www/DataStructures/Node.java b/src/main/java/net/jrtechs/www/DataStructures/Node.java new file mode 100644 index 0000000..acb33e2 --- /dev/null +++ b/src/main/java/net/jrtechs/www/DataStructures/Node.java @@ -0,0 +1,44 @@ +package net.jrtechs.www.DataStructures; + +/** + * @author Jeffery Russell 8-24-18 + */ + + +/** + * + * @param + */ +public class Node +{ + private E data; + + private Node next; + + public Node(E d) + { + this.data = d; + this.next = null; + } + + public Node(E d, Node newNext) + { + this.data = d; + this.next = newNext; + } + + public Node getNext() + { + return this.next; + } + + public E getData() + { + return data; + } + + public void setNext(Node newNext) + { + this.next = newNext; + } +}