* 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
- Freelance Writers Can Cash in on the List Article MarketTop 3 reasons to write list articles: you'll make money writing, they're easy to sell, and they spark ideas.
Blog Spotlight: A-List Blogger Nick Douglas of ValleywagValleywag is Silicon Valley's tech gossip rag. And who is the author of this A-List blog? His name is Nick Douglas and he was plucked prematurely from undergraduate English.
How to Build an Email List for Your BusinessEvery online business should have a good email list that is profitable for them. Of course there are certain tips that you should follow for creating this list and making it pr...- How to Build Your Leads ListIf you work at home you are going to need to have a list of leads. These are people who have shown that they are interested in what you have to offer. It is important to understand that this leads list is your busin...
- Easy Java - Linked List Implementation
- Example Intermediate Level C++ Program; A Simple Card Game; Classes, Randomization...
- The Horror Movie List
- Gift Buying Guide for the Skiers and Snowboarders on Your List
- How to Build an Opt-In List - the Tools You'll Need - Part 2
- Craig's List - It's Not EBay But it Works
- The Web Smiley Turns 25 - Plus a Full List of Emoticons



