Java Singly Linked List Code Example
This document provides a Java code example for implementing a basic singly linked list data structure. The class is named TrainList
and supports common list operations.
Java Linked List Code
The TrainList Class Structure
public class TrainList<X>
{
Node<X> first;
Node<X> last;
Adding Elements
The add Method
Adds an element to the end of the list.
public void add(X value)
{
addLast(value);
}
The addFirst Method
Adds an element to the beginning of the list.
public void addFirst(X value)
{
Node<X> x = new Node<X>(value);
if(first == null)
{
last = x;
}
else
{
x.next = first;
}
first = x;
}
The addLast Method
Adds an element to the end of the list.
public void addLast(X value)
{
Node<X> x = new Node<X>(value);
if(first == null)
{
first = x;
}
else
{
last.next = x;
}
last = x;
}
List Information
The size Method
Returns the number of elements in the list.
public int size()
{
int count = 0;
Node<X> x = first;
while (x != null)
{
count++;
x = x.next;
}
return count;
}
The indexOf Method
Returns the index of the first occurrence of the specified element, or -1 if not found.
public int indexOf(X target)
{
Node<X> x = first;
int index = 0;
while(x != null)
{
if(x.value.equals(target))
{
return index;
}
index++;
x = x.next;
}
return -1;
}
}
Accessing and Modifying Elements by Index
The get Method
Returns the element at the specified index.
public X get(int index)
{
if(index < 0 || index >= size())
{
throw new Pie(); // Placeholder exception
}
int count = 0;
Node<X> x = first;
while (x != null)
{
if (count == index)
{
return x.value;
}
count++;
x = x.next;
}
return null; // Should not be reached if index is valid
}
The set Method
Replaces the element at the specified index with the new value.
public void set(int index, X value)
{
if(index < 0 || index >= size())
{
throw new Pie(); // Placeholder exception
}
int count = 0;
Node<X> x = first;
while(x != null)
{
if(count == index)
{
x.value = value;
return; // Added return to stop iteration after setting
}
count++;
x = x.next;
}
}
The insert Method
Inserts an element at the specified index.
public void insert(int index, X value)
{
if(index < 0 || index >= size())
{
throw new Pie(); // Placeholder exception
}
if (index == 0)
{
addFirst(value);
}
else
{
int count = 0;
Node<X> x = first;
while(count < index - 1)
{
count ++;
x = x.next;
}
Node<X> a = new Node<X>(value);
a.next = x.next;
x.next = a;
}
}
The remove Method
Removes the element at the specified index.
public void remove(int index)
{
if (index < 0 || index >= size())
{
throw new Pie(); // Placeholder exception
}
if(index == 0)
{
first = first.next;
if (first == null) { // Update last if list becomes empty
last = null;
}
}
else
{
Node<X> x = first;
int count = 0;
while (count < index - 1)
{
count++;
x = x.next;
}
x.next = x.next.next;
if(x.next == null)
{
last = x;
}
}
}
Utility Methods
The clear Method
Removes all elements from the list.
public void clear()
{
first = null;
last = null; // Also clear last
}
The toString Method
Returns a string representation of the list.
public String toString()
{
String str = "[";
Node<X> x = first;
if(first == null) {
return "[]";
}
while(x.next != null)
{
str += x.value + ", ";
x = x.next;
}
return str += x.value + "]";
}
Important Notes
- The exception
throw new Pie();
is used as a placeholder for error handling. In a real application, you would typically throw a standard exception likeIndexOutOfBoundsException
. - A
Node
class is assumed to exist with fieldsvalue
andnext
. - Added a
return
statement in theset
method after updating the value for efficiency. - Added logic in
remove(0)
andclear()
to correctly update thelast
reference.
This code provides a foundational example for working with linked lists in Java.