Programa Java para implementar la lista enlazada desenrollada

Una lista enlazada desenrollada es un tipo especial de lista enlazada en la que cada Node almacena una array de elementos, a diferencia de una lista enlazada simple. Aquí usamos un ArrayList y un constructor que inicializa el tamaño de la Lista enlazada desenrollada. Se agregan elementos al primer Node hasta que se llena y luego se crea un nuevo Node con el mismo tamaño.

La clase Node tiene dos métodos, a saber 

  1. un constructor
  2. Un método de inserción que toma un número entero como parámetro.

Implementación:

  • Adición de elementos a la lista enlazada desenrollada
  • Eliminación de elementos de una lista enlazada desenrollada

Ejemplo 1: agregar elementos a la lista enlazada desenrollada

Java

// Java Program to implement Unrolled Linked List
 
// Importing all input output classes
import java.io.*;
// Importing all utility classes from
// java.util package
import java.util.*;
 
// Class 1
// Main class
class GFG {
 
    // Nested static class
    static class Node {
 
        // Member variables of static nested class
        int size;
        ArrayList<Integer> array;
 
        // Initializing next as NULL
        Node next = null;
 
        // Method 1
        // Of nested static class
        // To compute over size
        Node(int size)
        {
            // This keyword refers to current object itself
            this.size = size;
 
            // Storing ArrayList in an array
            array = new ArrayList<>();
        }
 
        // Method 2
        // To insert element in ana array
        void insert(int n)
        {
            // If array is having empty spaces
            if (array.size() < size) {
 
                // Add element from List to array
                array.add(n);
            }
 
            // If array is already full
            else {
 
                // Array size is zero
                if (next == null) {
 
                    // Create a new Node and start inserting
                    // elements
                    next = new Node(size);
                    next.insert(n);
                }
 
                // If array is defined
                else {
 
                    // Directly start inserting elements
                    next.insert(n);
                }
            }
        }
    }
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Creating an object of nested static class
        // inside the main() method
 
        // Maximum N elements will be printed in a row
        // N is passed as an parameter to the Node
        // N = 3 for illustration, do change and
        // observe how the output varies
        Node x = new Node(3);
 
        // Adding custom input elements
        // using the insert() method
        x.insert(10);
        x.insert(20);
        x.insert(30);
        x.insert(40);
        x.insert(50);
        x.insert(60);
        x.insert(70);
 
        // System.out.println(x.array);
        // System.out.println(x.next.array);
        // System.out.println(x.next.next.array);
 
        // If condition holds true
        // If there are elements left to be inserted
        while (x != null) {
 
            // Print and display the elements
            System.out.println(x.array);
 
            // Moving to next element
            x = x.next;
        }
    }
}
Producción

[10, 20, 30]
[40, 50, 60]
[70]

Ejemplo 2: Eliminación de elementos de una lista enlazada desenrollada

Java

// Java Program to implement Unrolled Linked List
 
// Importing all input output classes
import java.io.*;
// Importing all utility classes from
// java.util package
import java.util.*;
 
// Class 1
// Main class
class GFG {
 
    // Nested static class
    static class Node {
 
        // Member variables of nested class
        int size;
        ArrayList<Integer> array;
 
        // Initially next is pointing to head
        Node next = null;
 
        // Constructor of static nested class
        Node(int size)
        {
            // This keyword refers to current object itself
            this.size = size;
            // Assigning current List element
            // to an array
            array = new ArrayList<>();
        }
 
        // Method 1
        // Of static nested class
        void insert(int n)
        {
            // If there is space in an array
            if (array.size() < size) {
 
                // Add elements to an array
                array.add(n);
            }
 
            // Condition check over array
            else {
 
                // If no array exists, it means
                // head is still pointing to NULL
                if (next == null) {
 
                    // Create a new node
                    next = new Node(size);
 
                    // Now, start inserting elements
                    next.insert(n);
                }
 
                // If next is NOT-NULL
                else {
 
                    // Start filling array directly
                    next.insert(n);
                }
            }
        }
 
        // Method 2
        // Of static nested class
        boolean delete(int n)
        {
            if (array.contains(n)) {
                array.remove(array.indexOf(n));
                return true;
            }
            else {
                if (next == null) {
                    return false;
                }
                else {
                    next.delete(n);
                }
                return true;
            }
        }
    }
 
    // Mai driver method
    public static void main(String args[])
    {
 
        // Creating an object of Nodetype where
        // 3 as an argument represents maximum elements
        // to be displayed in aa row
        Node x = new Node(3);
 
        // Adding elements to the above object
        // Custom input entries
        x.insert(10);
        x.insert(20);
        x.insert(30);
        x.insert(40);
        x.insert(50);
        x.insert(60);
        x.insert(70);
 
        // System.out.println(x.array);
        // System.out.println(x.next.array);
        // System.out.println(x.next.next.array);
 
        // Display message
        System.out.println(
            "After Inserting all elements : ");
 
        // Creating an object of static nested class
        Node temp = x;
 
        while (temp != null) {
            System.out.println(temp.array);
            temp = temp.next;
        }
 
        // Display message
        System.out.println("Deleting 50 :");
 
        // Deleting a random element from above added
        // Say it be 50
        x.delete(50);
 
        // Using the temp variable so that
        // value is not lost
        temp = x;
 
        // Condition check
        // If temp vaible is NOT-NULL
        while (temp != null) {
 
            // Print and display the temp array
            // be it oe element or many
            System.out.println(temp.array);
 
            // Moving to the next node using
            // standard way 'next' in linkedList
            temp = temp.next;
        }
    }
}
Producción

After Inserting all elements : 
[10, 20, 30]
[40, 50, 60]
[70]
Deleting 50 :
[10, 20, 30]
[40, 60]
[70]

Publicación traducida automáticamente

Artículo escrito por varunkedia y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *