Skip to content

Instantly share code, notes, and snippets.

@iagox86
Created June 9, 2014 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iagox86/0fbe782b945a5eb80bb4 to your computer and use it in GitHub Desktop.
Save iagox86/0fbe782b945a5eb80bb4 to your computer and use it in GitHub Desktop.
/**
* MyList
*
* ASSIGNMENT Assignment 2, Question 1
* @author Brittany Postninkoff, 07757816
* @version 2014 - 06 - 07
*
* PURPOSE: This is a class that builds a linked list.
*/
import java.util.*;
public class MyList
{
private Link first;
private Link temp;
/**
* This is a constructor for MyList.
*/
public MyList()
{
first = null;
temp = null;
}
/**
* Adds the new link to the front of the linked list.
*
* @param Object data The value to be used in creating a new
* link for the linked list.
*/
public void addFirst(Object data)
{
Link newFirst = new Link(data);
//Sets the current first link, to the new link's next link, and sets
// the new link to the first position
newFirst.setNext(first);
first = newFirst;
}
/**
* Adds a link to the end of the linked list.
*
* @param Object data The value to be used in creating a new
* link to add onto the end of the list.
*/
public void addLast(Object data)
{
Link newLink = new Link(data);
//Checks to see if the first link is null. If so, the first link is
// the new link and requires no shifting.
if(first == null)
{
first = newLink;
first.setNext(null);
}
else
{
temp = first;
//iterates through the linked list until reaching the point just
// before the end of the list
while(temp.getNext() != null)
{
temp = temp.getNext();
}
//Instead of being null, the last link is now the value of the
// new link that was just created.
temp.setNext(newLink);
}
}
/**
* Removes the first item in the linked list.
*/
public void removeFirst()
{
//If there is an item in the list.
if(first != null)
{
//If there is only one item in the list.
if (first.getNext() == null)
{
first = null;
}
else
{
//First is now adjusted to be the second item in the list.
first = first.getNext();
}
}
}
/**
* Removes the last item in the list.
*/
public void removeLast()
{
//If first and last are the same item, reset linked list.
if (first.getNext() == null)
{
first = null;
}
else
{
//Set to the first link so we can loop through.
temp = first;
//Loop through to the next to last link.
while(temp.getNext() != null)
{
temp = temp.getNext();
}
temp.setNext(null);
}
}
/**
* Returns the data inside a link. Loops through the list, until a certain
* position in the list, and returns the link data.
*
* @param int index The position of the link to be printed.
*
* @return Object data The data inside the link in question.
*/
public Object get(int index)
{
temp = first;
Object data = "";
//Loops through until the correct link
for(int i = 0; i < index; i++)
{
temp = temp.getNext();
}
return temp.getData();
}
/**
* Checks to verify whether the list is empty or not.
*
* @return boolean empty If list is empty, this value is true.
*/
public boolean isEmpty()
{
boolean empty = true;
if(first.getNext() != null)
{
empty = false;
}
return empty;
}
public String toString()
{
String values = "";
Link current = first;
while(current != null)
{
values = current.getData() + "\n";
current = current.getNext();
}
return values;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment