Linked List Implementation - Java Pt. 1

Chris Chen
/*
* Purpose: to create a linked list class
*/

public class MyLinkedList {

private ListNode front; //stores the list node at the front of the LinkedList

//Default constructor for creating an object of MyLinkedList
public MyLinkedList(){

front = null;

}//end default constructor

//determines if the LinkedList is empty
public boolean isEmpty(){

return(front == null);

}//end isEmpty

//adds a ListNode to the beginning of the LinkedList
public void addFirst(Object o){

front = new ListNode(o, front);

}//end addFirst

//adds a ListNode to the end of the LinkedList
public void addLast(Object o){

//checks if the list is empty
if(isEmpty()){

this.addFirst(o);
return;

}//end if

ListNode temp = front;

//loops though LinkedList
while(temp.getNext() != null)
temp = temp.getNext();

temp.setNext(new ListNode(o, null));
temp = null;

}//end addLast

//adds a ListNode to the LinkedList after the first appearance of the specified object
public boolean addAfter(Object target, Object o){

//determines if the LinkedList contains the target
if(isEmpty())
return(false);

ListNode temp = front;

//loops through the LinkedList
while(!(temp == null || temp.getValue().equals(target)))
temp = temp.getNext();

//checks if the temp ListNode is at the end of the LinkedList
if(temp == null)
return(false);

temp.setNext(new ListNode(o, temp.getNext()));
temp = null;
return(true);

}//end addAfter

//removes the specified object from the ListNode (returns false if object isn't in LinkedList)
public boolean remove(Object o){

//checks if the target is in the LinkedList
if(!isEmpty()){

//checks if the first element is the target
if(front.getValue().equals(o))
return(removeFirst());

ListNode temp = front;

//loops through the Linked List
while(!(temp.getNext() == null || temp.getNext().getValue().equals(o)))
temp = temp.getNext();

//checks if the marker isn't at the end of the LinkedList
if(temp.getNext() != null){

ListNode temp2 = temp.getNext();
temp.setNext(temp.getNext().getNext());

temp2.setNext(null);
temp = null;
temp2 = null;

return(true);

}//end if

}//end if

return(false);

}//end remove

//removes the first element in the LinkedList
public boolean removeFirst(){

//checks if the list is empty
if(isEmpty())
return(false);

ListNode temp = front;
front = front.getNext();

temp.setNext(null);
temp = null;
return(true);

}//end removeFirst

//returns the value of the ListNode of a specified location
public Object get(int i){

if(i < 0 || i >= size())
throw new IndexOutOfBoundsException("Index passed in isn't contained in LinkedList.");

ListNode temp = front;

//loops through the LinkedList
for(int x = 0; x < i; x++)
temp = temp.getNext();

Object ret = temp.getValue();
temp = null;

return(ret);

}//end get

//checks if the LinkedList contains the specified object
public boolean contains(Object o){

//checks if the LinkedList is empty
if(!isEmpty()){

ListNode temp = front;

//Loops through the LinkedList
while(temp != null){

//checks if the target is found
if(temp.getValue().equals(o))
return(true);

temp = temp.getNext();

}//end while

}//end if

return(false);

}//end contains

//returns the number of ListNodes in the LinkedList
public int size(){

int i = 0; //stores the number of nodes in the LinkedList

ListNode temp = front;

//loops through the LinkedList
while(temp != null){

temp = temp.getNext();
i++;

}//end while

return(i);

}//end size

//returns a string representation of the LinkedList
public String toString(){

ListNode temp = front;
String ret = "";

//loops through the LinkedList
while(temp!=null){

ret += temp.getValue().toString();
ret += "\n";
temp = temp.getNext();

}//end while

return(ret);

}//end toString

}//end MyLinkedList

Published by Chris Chen

Chris is currently attending the University of California, Berkeley seeking an undergraduate's degree in Electrical Engineering Computer Science. He enjoys playing basketball, practicing kendo, hanging out w...  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.