> For the complete documentation index, see [llms.txt](https://polinema.gitbook.io/jti-modul-praktikum-algoritma-dan-struktur-data/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://polinema.gitbook.io/jti-modul-praktikum-algoritma-dan-struktur-data/struktur-data-linier/job-sheet-8-doubly-linked-list/praktikum-2.md).

# Praktikum 2

## Deskripsi

Pada praktikum 2 ini akan dibuat beberapa method untuk menghapus isi linked list pada class `DoublyLinkedList`. Penghapusan dilakukan dalam tiga cara di bagian paling depan, paling belakang, dan sesuai indeks yang ditentukan pada struktur data linked list. Method tambahan tersebut akan ditambahkan sesuai pada diagram class berikut,

<figure><img src="/files/SmTaE2FXlTs3tdHbVbID" alt=""><figcaption></figcaption></figure>

## Langkah Percobaan

* Buatlah method `removeFirst()` di dalam class DoublyLinkedList.

{% code overflow="wrap" lineNumbers="true" %}

```java
public void removeFirst() throws Exception {
    if (isEmpty()) {
        throw new Exception("Linked List masih kosong, tidak dapat dihapus!");
    } else if (size == 1) {
        removeLast();
    } else {
        head = head.next;
        head.prev = null;
        size--;
    }
}
```

{% endcode %}

* Tambahkan method `removeLast()` yang masih belum dibuat pada tahap sebelumnya.

{% code overflow="wrap" lineNumbers="true" %}

```java
public void removeLast() throws Exception {
    if (isEmpty()) {
        throw new Exception("Linked List masih kosong, tidak dapat dihapus!");
    } else if (head.next == null) {
        head = null;
        size--;
        return;
    }
    Node current = head;
    while (current.next.next != null) {
        current = current.next;
    }
    current.next = null;
    size--;
}
```

{% endcode %}

* Terakhir, tambahkan method `remove()` pada kelas `DoublyLinkedList`.

{% code overflow="wrap" lineNumbers="true" %}

```java
public void remove(int index) throws Exception {
    if (isEmpty() || index >= size) {
        throw new Exception("Nilai indeks di luar batas");
    } else if (index == 0) {
        removeFirst();
    } else {
        Node current = head;
        int i = 0;
        while (i < index) {
            current = current.next;
            i++;
        }
        if (current.next == null) {
            current.prev.next = null;
        } else if (current.prev == null) {
            current = current.next;
            current.prev = null;
            head = current;
        } else {
            current.prev.next = current.next;
            current.next.prev = current.prev;
        }
        size--;
    }
}
```

{% endcode %}

* Selanjutnya, untuk menguji method remove yang telah Anda buat, tambahkan beberapa kode berikut pada main class.

{% code overflow="wrap" lineNumbers="true" %}

```java
dll.addLast(50);
dll.addLast(40);
dll.addLast(10);
dll.addLast(20);
dll.print();
System.out.println("Size : " + dll.size());
System.out.println("==========================");
dll.removeFirst();
dll.print();
System.out.println("Size : " + dll.size());
System.out.println("==========================");
dll.removeLast();
dll.print();
System.out.println("Size : " + dll.size());
System.out.println("==========================");
dll.remove(1);
dll.print();
System.out.println("Size : " + dll.size());
System.out.println("==========================");
```

{% endcode %}

## Verifikasi Hasil Praktikum

Jika program berhasil dikompilasi dan dijalankan, maka akan menghasilkan output seperti gambar berikut,

<figure><img src="/files/I1BdYALhqD9ZwoCYmOUo" alt=""><figcaption></figcaption></figure>

## Pertanyaan

* Apakah maksud statement berikut pada method `removeFirst()`?\
  \
  `head = head.next;`\
  `head.prev = null;`
* Bagaimana cara mendeteksi posisi data ada pada bagian akhir pada method `removeLast()`?
* Jelaskan alasan mengapa potongan kode program di bawah ini tidak cocok untuk perintah remove!\
  \
  `Node tmp = head.next;`\
  `head.next = tmp.next;`\
  `tmp.next.prev = head;`
* Jelaskan fungsi kode program berikut ini pada fungsi remove!<br>

  ```java
  current.prev.next = current.next;
  current.next.prev = current.prev;
  ```
