チャレンジ:LinkedListにおけるsizeメソッドとdeleteメソッドの実装
メニューを表示するにはスワイプしてください
課題
4つのCRUD操作のうち3つをすでに実装しました。この課題では、2つのメソッドを実装する必要があります。
public int size();public void delete(int index);
課題はGitHub上にありますので、ローカルのIDEで実装してください。
この課題の解答例と実装に関するコメントは後ほど確認できます。
この課題を完了するための簡単なガイド:
- まずは
size()メソッドから始めることを推奨します。最初に、リスト内の要素数を管理する変数を宣言し、この変数をメソッドから返却します; size()メソッドでは、display()メソッドと同じ手順でリストを走査します。ただし、要素を表示する代わりに、作成した変数をインクリメントします;delete()メソッドでは、indexをパラメータとして受け取ります。インデックスの検証を行い、正しい範囲内であることを確認することが重要です;index == 0の場合は、head = head.nextとすることで参照を更新します;- 次に、
forループを使って目的のインデックスの要素を探します; - 必要な要素が見つかったら、次の操作を行います:
node.next = node.next.next;; - これにより、削除した要素への参照が外れ、リストから要素が削除されます。
課題が完了したら、いくつかのテストが用意されているmainメソッドを実行できます。
さらに、testフォルダ内のユニットテストを実行して、解答を検証できます。
- 慎重なイテレーション ―
size()メソッドを実装する際は、display()と同様にリストを走査し、値を表示する代わりにカウンターをインクリメントします; - 境界値の考慮 ―
delete()メソッドでは、常にindex == 0を確認してheadを正しく更新し、処理を進める前にインデックスが範囲内かどうかを必ず確認します; - リンクの正しい更新 ― ノードを削除する際は、
current.next = current.next.nextとすることで要素をバイパスし、リストから削除します。
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();
}
}
すべて明確でしたか?
フィードバックありがとうございます!
セクション 1. 章 7
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください
セクション 1. 章 7