0% found this document useful (0 votes)
7 views6 pages

Package Linkedlistdouble

Uploaded by

Adolfo Mena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

Package Linkedlistdouble

Uploaded by

Adolfo Mena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

package linkedlistdouble;

public class DoublyLinkedList {

public DoublyNode head;

public DoublyNode tail;

public int size;

public DoublyNode createDLL(int nodeValue) {

head = new DoublyNode();

DoublyNode newNode = new DoublyNode();

newNode.value = nodeValue;

newNode.next = null;

newNode.prev = null;

head = newNode;

tail = newNode;

size = 1;

return head;

public void deleteNode(int nodeValue) {

if (head == null) {

System.out.println("La lista está vacía.");

return;

} else if (head.value == nodeValue) { // Si el nodo a eliminar es el primero

if (size == 1) { // Si la lista solo tiene un nodo

head = null;

tail = null;

} else {

head = head.next;

head.prev = null;

size--;
} else { // Para el resto de los casos

DoublyNode tempNode = head;

while (tempNode != null) {

if (tempNode.value == nodeValue) {

if (tempNode == tail) { // Si el nodo es el último

tail = tail.prev;

tail.next = null;

} else { // Nodo en el medio

tempNode.prev.next = tempNode.next;

tempNode.next.prev = tempNode.prev;

size--;

System.out.println("Nodo con valor " + nodeValue + " eliminado.");

return;

tempNode = tempNode.next;

System.out.println("Nodo con valor " + nodeValue + " no encontrado.");

public void traverseDLL() {

if (head != null) {

DoublyNode tempNode = head;

while (tempNode != null) {

System.out.print(tempNode.value);

if (tempNode.next != null) {

System.out.print(" <-> ");

tempNode = tempNode.next;

System.out.println();
} else {

System.out.println("La lista doble no existe.");

public void reverseTraverseDLL() {

if (tail != null) {

DoublyNode tempNode = tail;

while (tempNode != null) {

System.out.print(tempNode.value);

if (tempNode.prev != null) {

System.out.print(" <-> ");

tempNode = tempNode.prev;

System.out.println();

} else {

System.out.println("La lista doble no existe.");

public void insertDLL(int nodeValue, int location) {

DoublyNode newNode = new DoublyNode();

newNode.value = nodeValue;

if (head == null) {

createDLL(nodeValue);

return;

} else if (location == 0) { // Insertar al inicio

newNode.next = head;

newNode.prev = null;

head.prev = newNode;

head = newNode;
} else if (location >= size) { // Insertar al final

newNode.next = null;

tail.next = newNode;

newNode.prev = tail;

tail = newNode;

} else { // Insertar en el medio

DoublyNode tempNode = head;

int index = 0;

while (index < location - 1) {

tempNode = tempNode.next;

index++;

newNode.prev = tempNode;

newNode.next = tempNode.next;

tempNode.next.prev = newNode;

tempNode.next = newNode;

size++;

public boolean searchNode(int nodeValue) {

if (head != null) {

DoublyNode tempNode = head;

int index = 0;

while (tempNode != null) {

if (tempNode.value == nodeValue) {

System.out.println("Nodo encontrado en la posición " + index);

return true;

tempNode = tempNode.next;

index++;
}

System.out.println("Nodo no encontrado.");

return false;

public void eliminarDuplicados() {

if (head != null) {

DoublyNode current = head;

while (current != null) {

DoublyNode tempNode = current.next;

while (tempNode != null) {

if (current.value == tempNode.value) {

if (tempNode == tail) {

tail = tempNode.prev;

tail.next = null;

} else {

tempNode.prev.next = tempNode.next;

tempNode.next.prev = tempNode.prev;

size--;

tempNode = tempNode.next;

current = current.next;

System.out.println("Se han eliminado los nodos duplicados.");

} else {

System.out.println("La lista doble no existe.");

}
public void deleteDLL() {

head = null;

tail = null;

size = 0;

System.out.println("La lista ha sido eliminada.");

Metodo Main.-

package linkedlistdouble;

public class LinkedListDouble {

public static void main(String[] args) {

DoublyLinkedList dll = new DoublyLinkedList();

// Creando la lista doblemente enlazada

dll.createDLL(10);

dll.insertDLL(20, 1);

dll.insertDLL(30, 2);

dll.insertDLL(40, 3);

dll.insertDLL(20, 4); // Nodo duplicado

// Recorrer la lista en orden

System.out.println("Recorrido de la lista:");

dll.traverseDLL();

LISTA NODE:

package linkedlistdouble;

public class DoublyNode {

public int value;

public DoublyNode next;

public DoublyNode prev;

You might also like