File clean-up

This commit is contained in:
KylerSmith
2017-07-09 16:29:00 -07:00
parent 5fec65b247
commit ae7aba0b44
21 changed files with 14 additions and 14 deletions

View File

@@ -0,0 +1,42 @@
public class CircleLinkedList<E>{
private static class Node<E>{
Node<E> next;
E value;
private Node(E value, Node<E> next){
this.value = value;
this.next = next;
}
}
private int size; //For better O.O design this should be private allows for better black box design
private Node<E> head; //this will point to dummy node;
public CircleLinkedList(){ //constructer for class.. here we will make a dummy node for circly linked list implementation with reduced error catching as our list will never be empty;
head = new Node<E>(null,head); //creation of the dummy node
size = 0;
}
public int getSize(){ return size;} // getter for the size... needed because size is private.
public void append(E value){ // for the sake of simplistiy this class will only contain the append function or addLast other add functions can be implemented however this is the basses of them all really.
if(value == null){
throw new NullPointerException("Cannot add null element to the list"); // we do not want to add null elements to the list.
}
head.next = new Node<E>(value,head); //head.next points to the last element;
size++;}
public E remove(int pos){
if(pos>size || pos< 0){
throw new IndexOutOfBoundsException("position cannot be greater than size or negative"); //catching errors
}
Node<E> iterator = head.next;
Node<E> before = head; //we need to keep track of the element before the element we want to remove we can see why bellow.
for(int i = 1; i<=pos; i++){
iterator = iterator.next;
before = before.next;
}
E saved = iterator.value;
before.next = iterator.next; // assigning the next referance to the the element following the element we want to remove... the last element will be assigned to the head.
iterator.next = null; // scrubbing
iterator.value = null;
return saved;
}
}

View File

@@ -0,0 +1,214 @@
/**
* This class implements a DoublyLinkedList. This is done using the classes
* LinkedList and Link.
*
* A linked list is simplar to an array, it holds values. However,
* links in a linked list do not have indees. With a linked list
* you do not need to predetermine it's size as it grows and shrinks
* as it is edited. This is an example of a double ended, doubly
* linked list. Each link references the next link and the previous
* one.
*
* @author Unknown
*
*/
class DoublyLinkedList{
/** Head refers to the front of the list */
private Link head;
/** Tail refers to the back of the list */
private Link tail;
/**
* Constructor
*/
public DoublyLinkedList(){
head = null;
tail = null;
}
/**
* Insert an element at the head
*
* @param x Element to be inserted
*/
public void insertHead(int x){
Link newLink = new Link(x); //Create a new link with a value attached to it
if(isEmpty()) //Set the first element added to be the tail
tail = newLink;
else
head.previous = newLink; // newLink <-- currenthead(head)
newLink.next = head; // newLink <--> currenthead(head)
head = newLink; // newLink(head) <--> oldhead
}
/**
* Insert an element at the tail
*
* @param x Element to be inserted
*/
public void insertTail(int x){
Link newLink = new Link(x);
newLink.next = null; // currentTail(tail) newlink -->
tail.next = newLink; // currentTail(tail) --> newLink -->
newLink.previous = tail; // currentTail(tail) <--> newLink -->
tail = newLink; // oldTail <--> newLink(tail) -->
}
/**
* Delete the element at the head
*
* @return The new head
*/
public Link deleteHead(){
Link temp = head;
head = head.next; // oldHead <--> 2ndElement(head)
head.previous = null; // oldHead --> 2ndElement(head) nothing pointing at old head so will be removed
if(head == null)
tail = null;
return temp;
}
/**
* Delete the element at the tail
*
* @return The new tail
*/
public Link deleteTail(){
Link temp = tail;
tail = tail.previous; // 2ndLast(tail) <--> oldTail --> null
tail.next = null; // 2ndLast(tail) --> null
return temp;
}
/**
* Delete the element from somewhere in the list
*
* @param x element to be deleted
* @return Link deleted
*/
public Link delete(int x){
Link current = head;
while(current.value != x) //Find the position to delete
current = current.next;
if(current == head)
deleteHead();
else if(current == tail)
deleteTail();
else{ //Before: 1 <--> 2(current) <--> 3
current.previous.next = current.next; // 1 --> 3
current.next.previous = current.previous; // 1 <--> 3
}
return current;
}
/**
* Inserts element and reorders
*
* @param x Element to be added
*/
public void insertOrdered(int x){
Link newLink = new Link(x);
Link current = head;
while(current != null && x > current.value) //Find the position to insert
current = current.next;
if(current == head)
insertHead(x);
else if(current == null)
insertTail(x);
else{ //Before: 1 <--> 2(current) <--> 3
newLink.previous = current.previous; // 1 <-- newLink
current.previous.next = newLink; // 1 <--> newLink
newLink.next = current; // 1 <--> newLink --> 2(current) <--> 3
current.previous = newLink; // 1 <--> newLink <--> 2(current) <--> 3
}
}
/**
* Returns true if list is empty
*
* @return true if list is empty
*/
public boolean isEmpty(){
return(head == null);
}
/**
* Prints contents of the list
*/
public void display(){ //Prints contents of the list
Link current = head;
while(current!=null){
current.displayLink();
current = current.next;
}
System.out.println();
}
}
/**
* This class is used to implement the nodes of the
* linked list.
*
* @author Unknown
*
*/
class Link{
/** Value of node */
public int value;
/** This points to the link in front of the new link */
public Link next;
/** This points to the link behind the new link */
public Link previous;
/**
* Constructor
*
* @param value Value of node
*/
public Link(int value){
this.value = value;
}
/**
* Displays the node
*/
public void displayLink(){
System.out.print(value+" ");
}
/**
* Main Method
*
* @param args Command line arguments
*/
public static void main(String args[]){
DoublyLinkedList myList = new DoublyLinkedList();
myList.insertHead(13);
myList.insertHead(7);
myList.insertHead(10);
myList.display(); // <-- 10(head) <--> 7 <--> 13(tail) -->
myList.insertTail(11);
myList.display(); // <-- 10(head) <--> 7 <--> 13 <--> 11(tail) -->
myList.deleteTail();
myList.display(); // <-- 10(head) <--> 7 <--> 13(tail) -->
myList.delete(7);
myList.display(); // <-- 10(head) <--> 13(tail) -->
myList.insertOrdered(23);
myList.insertOrdered(67);
myList.insertOrdered(3);
myList.display(); // <-- 3(head) <--> 10 <--> 13 <--> 23 <--> 67(tail) -->
}
}

View File

@@ -0,0 +1,121 @@
/**
* This class implements a SinglyLinked List. This is done
* using SinglyLinkedList class and a LinkForLinkedList Class.
*
* A linked list is implar to an array, it hold values.
* However, links in a linked list do not have indexes. With
* a linked list you do not need to predetermine it's size as
* it gorws and shrinks as it is edited. This is an example of
* a singly linked list. Elements can only be added/removed
* at the head/front of the list.
*
* @author Unknown
*
*/
class SinglyLinkedList{
/**Head refered to the front of the list */
private LinkForLinkedList head;
/**
* Constructor of SinglyLinkedList
*/
public SinglyLinkedList(){
head = null;
}
/**
* This method inserts an element at the head
*
* @param x Element to be added
*/
public void insertHead(int x){
LinkForLinkedList newLink = new LinkForLinkedList(x); //Create a new link with a value attached to it
newLink.next = head; //Set the new link to point to the current head
head = newLink; //Now set the new link to be the head
}
/**
* This method deletes an element at the head
*
* @return The element deleted
*/
public LinkForLinkedList deleteHead(){
LinkForLinkedList temp = head;
head = head.next; //Make the second element in the list the new head, the Java garbage collector will later remove the old head
return temp;
}
/**
* Checks if the list is empty
*
* @return true is list is empty
*/
public boolean isEmpty(){
return(head == null);
}
/**
* Prints contents of the list
*/
public void display(){
LinkForLinkedList current = head;
while(current!=null){
current.displayLink();
current = current.next;
}
System.out.println();
}
/**
* Main method
*
* @param args Command line arguments
*/
public static void main(String args[]){
SinglyLinkedList myList = new SinglyLinkedList();
System.out.println(myList.isEmpty()); //Will print true
myList.insertHead(5);
myList.insertHead(7);
myList.insertHead(10);
myList.display(); // 10(head) --> 7 --> 5
myList.deleteHead();
myList.display(); // 7(head) --> 5
}
}
/**
* This class is the nodes of the SinglyLinked List.
* They consist of a vlue and a pointer to the node
* after them.
*
* @author Unknown
*
*/
class LinkForLinkedList{
/** The value of the node */
public int value;
/** Point to the next node */
public LinkForLinkedList next; //This is what the link will point to
/**
* Constructor
*
* @param valuein Value to be put in the node
*/
public LinkForLinkedList(int valuein){
value = valuein;
}
/**
* Prints out the value of the node
*/
public void displayLink(){
System.out.print(value+" ");
}
}