I've been working on implementing a doubly linked list from scratch in Java, and if anyone has time, could you critique it?
class Node {
Node prev;
Node next;
int data;
public Node(int d) {
data = d;
prev = null;
next = null;
}
}
class LinkedList {
Node head;
public LinkedList() {
head = null;
}
public void insert(int d) {
if (head == null) {
head = new Node(d);
return;
}
if (head.data > d) {
Node holder = head;
Node newNode = new Node(d);
head = newNode;
head.next = holder;
holder.prev = newNode;
return;
}
Node tmpNode = head;
while (tmpNode.next != null && tmpNode.next.data < d) {
tmpNode = tmpNode.next;
}
Node prevTmp = tmpNode;
Node insertedNode = new Node(d);
if (tmpNode.next != null) {
Node nextTmp = tmpNode.next;
insertedNode.next = nextTmp;
nextTmp.prev = insertedNode;
}
prevTmp.next = insertedNode;
insertedNode.prev = prevTmp;
}
public void delete(int d) {
if (head == null) {
System.out.println("The list is empty.");
return;
}
if (head.data == d) {
head = head.next;
if (head != null) {
head.prev = null;
}
return;
}
Node tmpNode = head;
while (tmpNode != null && tmpNode.data != d) {
tmpNode = tmpNode.next;
}
if (tmpNode == null) {
System.out.println("That node does not exist in the list");
return;
}
if (tmpNode.data == d) {
tmpNode.prev.next = tmpNode.next;
if (tmpNode.next != null) {
tmpNode.next.prev = tmpNode.prev;
}
}
}
public void print() {
Node tmpNode = head;
while (tmpNode != null) {
System.out.print(tmpNode.data + " -> ");
tmpNode = tmpNode.next;
}
System.out.print("null");
}