Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Challenge: Implementing size and delete Methods in LinkedList | Fundamental Data Structures in Java
Java Data Structures
course content

Kursinhalt

Java Data Structures

Java Data Structures

1. Fundamental Data Structures in Java
2. Advanced Data Structures in Java
3. Mastering Map in Java
4. Advanced Java Features and Techniques

book
Challenge: Implementing size and delete Methods in LinkedList

Task

You implemented three out of four CRUD operations together. In this task, you need to implement two methods:

  • public int size();
  • public void delete(int index).

The assignment is on GitHub, so you need to implement it locally in your IDE.

You'll see the solution to this task with comments on the implementation.

A brief guide on how to complete this task:

  • I suggest starting with the size() method. First, you need to declare a variable to keep track of the number of elements in the list, and then simply return this variable from the method;
  • In the size() method, follow the same steps as in the display() method. However, instead of printing the elements to the screen, increment the variable you created;
  • In the delete() method, pass the index as a parameter. It is essential to validate the index to ensure it is within the correct range;
  • If index == 0, simply update the reference by setting head = head.next;
  • Next, perform operations with the for loop to find the desired index of the element;
  • Once the required element is found, perform the following operation:
    node.next = node.next.next;;
  • This way, you will remove the reference to the deleted element, effectively deleting it from the list.

Once you finish the task, you can run the main method, where some tests are provided for you.

Additionally, you can run the unit tests located in the test folder to validate your solution.

  • Iterate carefully – when implementing the size() method, traverse the list just like in display(), but instead of printing values, increment a counter;
  • Mind the edge cases – in the delete() method, always check if index == 0 to properly update head, and ensure the index is within bounds before proceeding;
  • Update links correctly – when deleting a node, update current.next = current.next.next to bypass the element, effectively removing it from the list.
package com.example;

class Node {
    int data;
    Node next;

    public Node(int data) {
        this.data = data;
        this.next = null;
    }
}

class SinglyLinkedList {
    private Node head;

    public SinglyLinkedList() {
        this.head = null;
    }

    public void append(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
            return;
        }

        Node current = head;
        while (current.next != null) {
            current = current.next;
        }

        current.next = newNode;
    }

    public void display() {
        Node current = head;
        while (current != null) {
            System.out.print(current.data + " ");
            current = current.next;
        }
        System.out.println();
    }

    public void update(int index, int newData) {
        if (index < 0 || index >= size()) {
            System.out.println("Invalid index");
            return;
        }

        Node current = head;
        for (int i = 0; i < index; i++) {
            current = current.next;
        }

        current.data = newData;
    }

    public void delete(int index) {
        if (index < 0 || index >= size()) {
            System.out.println("Invalid Index");
            return;
        }

        if (index == 0) {
            head = head.next;
            return;
        }

        Node current = head;
        for (int i = 0; i < index - 1; i++) {
            current = current.next;
        }

        current.next = current.next.next;
    }

    public int size() {
        int count = 0;
        Node current = head;
        while (current != null) {
            count++;
            current = current.next;
        }
        return count;
    }

}

public class Main {
    public static void main(String[] args) {
        SinglyLinkedList linkedList = new SinglyLinkedList();

        // Adding elements to the end of the list (Create)
        linkedList.append(1);
        linkedList.append(2);
        linkedList.append(3);
        linkedList.append(4);

        // Displaying the content of the list (Read)
        System.out.println("Contents of the list:");
        linkedList.display();

        // Updating a value by index (Update)
        linkedList.update(2, 10);
        System.out.println("List after updating the value at index 2:");
        linkedList.display();

        // Deleting an element by index (Delete)
        linkedList.delete(1);
        System.out.println("List after deleting the element at index 1:");
        linkedList.display();
    }
}
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 7

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

course content

Kursinhalt

Java Data Structures

Java Data Structures

1. Fundamental Data Structures in Java
2. Advanced Data Structures in Java
3. Mastering Map in Java
4. Advanced Java Features and Techniques

book
Challenge: Implementing size and delete Methods in LinkedList

Task

You implemented three out of four CRUD operations together. In this task, you need to implement two methods:

  • public int size();
  • public void delete(int index).

The assignment is on GitHub, so you need to implement it locally in your IDE.

You'll see the solution to this task with comments on the implementation.

A brief guide on how to complete this task:

  • I suggest starting with the size() method. First, you need to declare a variable to keep track of the number of elements in the list, and then simply return this variable from the method;
  • In the size() method, follow the same steps as in the display() method. However, instead of printing the elements to the screen, increment the variable you created;
  • In the delete() method, pass the index as a parameter. It is essential to validate the index to ensure it is within the correct range;
  • If index == 0, simply update the reference by setting head = head.next;
  • Next, perform operations with the for loop to find the desired index of the element;
  • Once the required element is found, perform the following operation:
    node.next = node.next.next;;
  • This way, you will remove the reference to the deleted element, effectively deleting it from the list.

Once you finish the task, you can run the main method, where some tests are provided for you.

Additionally, you can run the unit tests located in the test folder to validate your solution.

  • Iterate carefully – when implementing the size() method, traverse the list just like in display(), but instead of printing values, increment a counter;
  • Mind the edge cases – in the delete() method, always check if index == 0 to properly update head, and ensure the index is within bounds before proceeding;
  • Update links correctly – when deleting a node, update current.next = current.next.next to bypass the element, effectively removing it from the list.
package com.example;

class Node {
    int data;
    Node next;

    public Node(int data) {
        this.data = data;
        this.next = null;
    }
}

class SinglyLinkedList {
    private Node head;

    public SinglyLinkedList() {
        this.head = null;
    }

    public void append(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
            return;
        }

        Node current = head;
        while (current.next != null) {
            current = current.next;
        }

        current.next = newNode;
    }

    public void display() {
        Node current = head;
        while (current != null) {
            System.out.print(current.data + " ");
            current = current.next;
        }
        System.out.println();
    }

    public void update(int index, int newData) {
        if (index < 0 || index >= size()) {
            System.out.println("Invalid index");
            return;
        }

        Node current = head;
        for (int i = 0; i < index; i++) {
            current = current.next;
        }

        current.data = newData;
    }

    public void delete(int index) {
        if (index < 0 || index >= size()) {
            System.out.println("Invalid Index");
            return;
        }

        if (index == 0) {
            head = head.next;
            return;
        }

        Node current = head;
        for (int i = 0; i < index - 1; i++) {
            current = current.next;
        }

        current.next = current.next.next;
    }

    public int size() {
        int count = 0;
        Node current = head;
        while (current != null) {
            count++;
            current = current.next;
        }
        return count;
    }

}

public class Main {
    public static void main(String[] args) {
        SinglyLinkedList linkedList = new SinglyLinkedList();

        // Adding elements to the end of the list (Create)
        linkedList.append(1);
        linkedList.append(2);
        linkedList.append(3);
        linkedList.append(4);

        // Displaying the content of the list (Read)
        System.out.println("Contents of the list:");
        linkedList.display();

        // Updating a value by index (Update)
        linkedList.update(2, 10);
        System.out.println("List after updating the value at index 2:");
        linkedList.display();

        // Deleting an element by index (Delete)
        linkedList.delete(1);
        System.out.println("List after deleting the element at index 1:");
        linkedList.display();
    }
}
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 7
some-alt