//points to first node of list
DNode first;
//points to last node of list
DNode last;
public void insertBeginning(List<DNode> list, DNode newNode) {
if (first == null) {
first = newNode;
last = newNode;
newNode.previous = null;
newNode.next = null;
} else {
insertBefore(list, last, newNode);
}
}
public void insertEnd(List<DNode> list, DNode newNode) {
if (last == null) {
insertBeginning(list, newNode);
} else {
insertAfter(list, last, newNode);
}
}
public void remove(DNode node) {
if (node.previous == null) {
first = node.next;
} else if (node.next == null) {
last = node.previous;
} else {
node.previous.next = node.next;
node.next.previous = node.previous;
}
Tres métodos básicos para una lista doblemente enlazada en Java: añadir nodo al inicio/fin, y eliminar un nodo. También es necesario crear dos variables generales.
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.