I am brushing up my Data Structure knowledge (for preparation for internship interviews) and I started by implementing a very simple Linked List class. This is by no means intended to be able to replace the Java library just to be something robust enough for making me understand and remember how to implement a Linked List.
(Newer code bellow!)LinkedList
class with private Node
class:
public class LinkedList<T> {
private Node _first;
private int _size;
public LinkedList() {
_first = null;
_size = 0;
}
public int getSize() {
return _size;
}
public void add(T data) {
Node current = _first;
if (current == null) {
_first = new Node(data);
_size++;
return;
}
while (current.getNext() != null) {
current = current.getNext();
}
current.setNext(new Node(data));
_size++;
}
public void add(T[] array) {
for (T data : array) {
add(data);
}
}
public void remove(T data) {
Node current = _first;
Node next = current.getNext();
if (_first.getData().equals(data)) {
if (_size == 1) {
_first.setData(null);
_size--;
return;
}
_first.setData(null);
_first = _first.getNext();
_size--;
return;
}
while (next != null) {
if (next.getData().equals(data)) {
current.setNext(next.getNext());
next = null;
_size--;
return;
}
current = next;
next = current.getNext();
}
}
private class Node<T> {
private T _data;
private Node _next;
public Node(T data) {
_data = data;
_next = null;
}
public void setData(T data) {
_data = data;
}
public T getData() {
return _data;
}
public void setNext(Node next) {
_next = next;
}
public Node getNext() {
return _next;
}
}
}
Updated after recommendations:
public class LinkedList<T> {
private Node<T> first;
private int size;
public LinkedList() {
this.first = null;
this.size = 0;
}
public int size() {
return this.size;
}
public void add(T data) {
Node<T> current = this.first;
if (current == null) {
this.first = new Node(data);
this.size++;
return;
}
while (current.getNext() != null) {
current = current.getNext();
}
current.setNext(new Node(data));
this.size++;
}
public void addAll(T[] array) {
for (T data : array) {
add(data);
}
}
public void remove(T data) {
if (this.first == null) {
throw new NoSuchElementException();
} else if (this.first.getData().equals(data)) {
this.first = this.first.getNext();
this.size--;
return;
}
Node<T> current = this.first;
Node<T> next = current.getNext();
while (next != null) {
if (next.getData().equals(data)) {
current.setNext(next.getNext());
this.size--;
return;
}
current = next;
next = current.getNext();
}
throw new NoSuchElementException();
}
public boolean contains(T data) {
Node<T> current = this.first;
while (current != null) {
if (current.getData().equals(data)) {
return true;
}
current = current.getNext();
}
return false;
}
@Override
public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append("{");
Node<T> current = this.first;
while (current != null) {
buffer.append(current.getData());
if (current.getNext() != null) {
buffer.append(", ");
}
current = current.getNext();
}
buffer.append("}");
return buffer.toString();
}
private class Node<T> {
private T data;
private Node<T> next;
public Node(T data) {
this.data = data;
this.next = null;
}
public void setData(T data) {
this.data = data;
}
public T getData() {
return this.data;
}
public void setNext(Node next) {
this.next = next;
}
public Node getNext() {
return this.next;
}
}
}
Anything I could do to make it better?