Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Challenge (LinkedList) | Basic Data Structures
Java Data Structures

Challenge (LinkedList)Challenge (LinkedList)

Task

In the previous chapter, we implemented three out of four CRUD operations together. In this task, you need to implement two methods: public int size() and public void delete(int index). The assignment is on GitHub, so you need to implement it locally in your IDE. In the next chapter, you'll see the solution to this task with comments on the implementation. Good luck!

A brief guide on how to complete this task:

  • First, I recommend implementing the size() method. Firstly, create a variable to store the number of elements in the list and then return this variable from the method;
  • In the size() method, perform the same manipulations as we did for the display method, but instead of printing to the screen, increment the created variable;
  • In the delete() method, you should take the index as a parameter. It is crucial not to forget to check the validity of the index;
  • If index == 0, simply redirect the link like 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.

Link to the Task: GitHub

Everything was clear?

Section 1. Chapter 7
course content

Course Content

Java Data Structures

Challenge (LinkedList)Challenge (LinkedList)

Task

In the previous chapter, we implemented three out of four CRUD operations together. In this task, you need to implement two methods: public int size() and public void delete(int index). The assignment is on GitHub, so you need to implement it locally in your IDE. In the next chapter, you'll see the solution to this task with comments on the implementation. Good luck!

A brief guide on how to complete this task:

  • First, I recommend implementing the size() method. Firstly, create a variable to store the number of elements in the list and then return this variable from the method;
  • In the size() method, perform the same manipulations as we did for the display method, but instead of printing to the screen, increment the created variable;
  • In the delete() method, you should take the index as a parameter. It is crucial not to forget to check the validity of the index;
  • If index == 0, simply redirect the link like 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.

Link to the Task: GitHub

Everything was clear?

Section 1. Chapter 7
some-alt