Karla News

Easy Java – Linked List Implementation

This is the Linked List class. I use generic typing (). If you don’t know what it means or how to use generic typing, then just replace the T’s with a variable type (String, char, int, double…) and get rid of the on the first line. Name the file “LL.java”.

public class LL T
{
private Node firstNode;

public void add(T b) // add a new value (b) to the end of the list
{
if(firstNode == null) // if the list is empty, then assign the first position to the new value (b)
firstNode = new Node(b);
else // if the list is not empty, assign the new value to the end
{
Node temp = firstNode; // create a temperary Node to traverse the list
while(temp.next != null) // while the next Node doesn’t equals null, assign the next Node to temperary
temp = temp.next;
Node newNode = new Node(b); // one you’ve reached the end, create a new Node with the new value (b)
newNode.next = temp.next; // the new Node’s next Node equals the temperary one’s next Node (null)
temp.next = newNode;
}// end else
}// end add

public void add(T b, int a) // add a new value (b) to a specified position (a) in the list
{
if(a == 1)
firstNode = new Node(b, firstNode);
else
{
Node temp = firstNode;
int count = 2;
while(temp.next != null && count < a)
{
temp = temp.next;
count++;
}// end while
Node newNode = new Node(b);
newNode.next = temp.next;
temp.next = newNode;
}
}// end add

public int getLength() // find the length of the list
{
Node temp = firstNode; // create a temperary Node equal to the first Node in your list
int count = 0; // instantiate an integer (count) and make it equal to 0
while(temp != null) // while the Node doesn’t equal null, assign temperary to the next Node and increment
// count
{
temp = temp.next;
count++;
}
return count; // return the integer (count)
}

See also  Oakley Sunglasses You Can Listen To: THUMP, THUMP 2 & RAZRWIRE

public int place(T test) // find what position a certain value is in the linked list
{
Node temp = firstNode;
int count = 0;
boolean found = false;
while(temp != null && !found) // run the while loop until the list has been completely traversed or the value
// is found
{
found = temp.data == test;
temp = temp.next;
count++;
}// end while
return count;
}

public void display() // print out the linked list by traversing the linked list and printing each value one at a time
{
Node temp = firstNode;
while(temp != null)
{
System.out.println(temp.data);
temp = temp.next;
}// end while
}// end display

public void move(int a, int b) // move the value from one position (a) to a new position (b)
{
T temp = getEntry(a);
remove(a);
add(temp, b);
}// end move

public void remove(int a) // remove the value at the specified position (a) and move the lower values up
{
if(a == 1)
firstNode = firstNode.next;
else
{
Node temp = firstNode;
Node temp2 = firstNode.next;
int count = 2;
while(temp2.next != null && count < a)
{
temp = temp.next;
temp2 = temp2.next;
count++;
}// end while
temp.next = temp2.next;
}
}// end remove

public T getEntry(int a) // find the value at the specified position (a)
{
Node temp = firstNode;
int count = 1;
while(temp.next != null && count < a)
{
temp = temp.next;
count++;
}// end while
return temp.data;
}// end getEntry

private class Node // Node object
{
private T data;
private Node next;
public Node(T b)
{
data = b;
next = null;
}
public Node(T b, Node nextNode)
{
data = b;
next = nextNode;
}
}// end private class
}// end class

Reference: