Зміст курсу
Java Data Structures
Java Data Structures
Section 1 Summary
Great, the first section is complete, and now you have a grasp of the basics of the Java Collections Framework. You've learned how to work with wrapper classes, ArrayList
, and LinkedList
. We even wrote our simplified version of LinkedList
, part of which you implemented yourself. Let's take a look at this implementation:
main
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(); } }
Yes, the solution turned out to be quite large, so I understand the inconvenience of scrolling through the code in a small window. However, you can run this code and see how it works.
Here are the two methods that you implemented yourself:
SinglyLinkedList
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; }
Your solution may differ slightly from the one I provided, but that's okay because programming is a creative process, and everyone's code can be completely different while serving the same functions.
Let's go over the main questions that were present in this section:
Дякуємо за ваш відгук!