diff --git a/assignment-01/H071181015/bin/com/belajar/Main.class b/assignment-01/H071181015/bin/com/belajar/Main.class new file mode 100644 index 0000000..814d8ac Binary files /dev/null and b/assignment-01/H071181015/bin/com/belajar/Main.class differ diff --git a/assignment-01/H071181015/bin/com/belajar/Node.class b/assignment-01/H071181015/bin/com/belajar/Node.class new file mode 100644 index 0000000..cb63d5c Binary files /dev/null and b/assignment-01/H071181015/bin/com/belajar/Node.class differ diff --git a/assignment-01/H071181015/bin/com/belajar/SingleLinkedList.class b/assignment-01/H071181015/bin/com/belajar/SingleLinkedList.class new file mode 100644 index 0000000..3fe6dd8 Binary files /dev/null and b/assignment-01/H071181015/bin/com/belajar/SingleLinkedList.class differ diff --git a/assignment-01/H071181015/src/com/belajar/Main.java b/assignment-01/H071181015/src/com/belajar/Main.java new file mode 100644 index 0000000..2d89b33 --- /dev/null +++ b/assignment-01/H071181015/src/com/belajar/Main.java @@ -0,0 +1,14 @@ +package com.belajar; + +public class Main { + public static void main(String[] args) { + SingleLinkedList sl1 = new SingleLinkedList<>(); + sl1.append("data1"); + sl1.append("data2", "data1"); + sl1.append("data3", "data4"); + sl1.append("data4", 10); + sl1.prepand("data5", "data6"); + sl1.prepand("data6", 10); + sl1.print(); + } +} \ No newline at end of file diff --git a/assignment-01/H071181015/src/com/belajar/Node.java b/assignment-01/H071181015/src/com/belajar/Node.java new file mode 100644 index 0000000..c45ae25 --- /dev/null +++ b/assignment-01/H071181015/src/com/belajar/Node.java @@ -0,0 +1,10 @@ +package com.belajar; + +public class Node { + Node next; + T data; + + Node(T data) { + this.data = data; + } +} \ No newline at end of file diff --git a/assignment-01/H071181015/src/com/belajar/SingleLinkedList.java b/assignment-01/H071181015/src/com/belajar/SingleLinkedList.java new file mode 100644 index 0000000..b25496c --- /dev/null +++ b/assignment-01/H071181015/src/com/belajar/SingleLinkedList.java @@ -0,0 +1,581 @@ +package com.belajar; + +import java.util.NoSuchElementException; + +public class SingleLinkedList { + Node head; + Node tail; + int size; + + void append(T data) { + if (head == null) { + head = new Node(data); + tail = head; + size++; + return; + } + Node temp = head; + while (temp.next != null) { + temp = temp.next; + } + Node newNode = new Node(data); + temp.next = newNode; + tail = newNode; + size++; + } + + void append(T data, T after) { + Node temp = head; + if (head == null) { + prepand(data); + return; + } + if (tail.data == after) { + append(data); + return; + } + boolean cek = false; + while (temp.next != null) { + temp = temp.next; + if (temp.data == after) { + cek = true; + break; + } + } + if (!cek) { + append(data); + return; + } + Node newNode = new Node(data); + Node temp2 = temp.next; + temp.next = newNode; + newNode.next = temp2; + size++; + } + + void append(T data, int after) { + Node temp = head; + int start = 0; + if ((after + 1) == size) { + append(data); + return; + } else { + boolean cek = false; + while (temp.next != null) { + temp = temp.next; + start++; + if (start == after) { + cek = true; + break; + } + } + if (!cek) { + append(data); + return; + } + Node temp2 = temp.next; + Node newNode = new Node(data); + temp.next = newNode; + newNode.next = temp2; + size++; + } + } + + void append(T data[]) { + for (T a : data) { + append(a); + } + } + + void prepand(T data) { + Node newNode = new Node(data); + newNode.next = head; + head = newNode; + size++; + } + + void prepand(T data, T before) { + if (head.data == before) { + prepand(data); + return; + } + Node temp = head; + boolean cek = false; + while (temp.next != null) { + temp = temp.next; + if (temp.data == before) { + cek = true; + break; + } + } + if (!cek) { + prepand(data); + return; + } + Node newNode = new Node(data); + Node temp2 = temp.next; + temp.next = newNode; + newNode.next = temp2; + size++; + } + + void prepand(T data, int index) { + if (index == 0) { + prepand(data); + return; + } + if (index + 1 == size) { + append(data); + return; + } + Node temp = head; + int start = 1; + boolean cek = false; + while (temp.next != null) { + temp = temp.next; + start++; + if (start == index) { + cek = true; + break; + } + } + if (!cek) { + prepand(data); + return; + } + Node newNode = new Node(data); + Node temp2 = temp.next; + temp.next = newNode; + newNode.next = temp2; + size++; + } + + void prepand(T data[]) { + for (T a : data) { + prepand(a); + } + } + + void update(T data, T newData) { + Node temp = head; + boolean cek = false; + while (temp.next != null) { + temp = temp.next; + if (temp.data == data) { + cek = true; + break; + } + } + if (!cek) { + throw new NoSuchElementException("DATA YANG DIMAKSUD TIDAK ADA"); + } + temp.data = newData; + } + + void update(T data, int index) { + Node temp = head; + int start = 0; + boolean cek = false; + while (temp.next != null) { + temp = temp.next; + start++; + if (start == index) { + cek = true; + break; + } + } + if (!cek) { + throw new IndexOutOfBoundsException("INDEKS DILUAR BATAS"); + } + temp.data = data; + } + + void clear() { + head = null; + size = 0; + } + + void remove(T data) { + Node temp = head; + if (head.data == data) { + head = head.next; + size--; + return; + } + if (tail.data == data) { + removeTail(); + return; + } + boolean cek = false; + while (temp.next != null) { + temp = temp.next; + if (temp.data == data) { + cek = true; + break; + } + } + if (!cek) { + throw new NoSuchElementException("DATA TIDAK ADA"); + } + temp.next = temp.next.next; + size--; + } + + void remove(int index) { + int start = 1; + if (index == 0) { + removeHead(); + return; + } + if ((index + 1) == size) { + removeTail(); + return; + } + Node temp = head; + boolean cek = false; + while (temp.next != null) { + temp = temp.next; + start++; + if (start == index) { + cek = true; + break; + } + } + if (!cek) { + throw new IndexOutOfBoundsException("INDEX DILUAR BATAS"); + } + temp.next = temp.next.next; + size--; + } + + void removeHead() { + head = head.next; + size--; + } + + void removeTail() { + Node temp = head; + while (temp.next.next != null) { + temp = temp.next; + } + temp.next = null; + tail = temp; + size--; + } + + void removeAfter(T data) { + Node temp = head; + if (tail.data == data) { + throw new NoSuchElementException("DATA TIDAK ADA"); + } + while (temp.next != null) { + temp = temp.next; + } + temp.next = temp.next.next; + size--; + } + + void removeAfter(int index) { + Node temp = head; + int start = 0; + if ((index + 1) == size) { + throw new IndexOutOfBoundsException("INDEKS DILUAR BATAS"); + } + while (start != index) { + temp = temp.next; + start++; + } + temp.next = temp.next.next; + size--; + } + + void removeBefore(T data) { + Node temp = head; + if (head.data == data) { + throw new NoSuchElementException("DATA TIDAK ADA"); + } + if (head.next.data == data) { + removeHead(); + return; + } + while (temp.next.next.data != data) { + temp = temp.next; + } + temp.next = temp.next.next; + size--; + } + + void removeBefore(int index) { + Node temp = head; + int start = 0; + if (index == 0) { + throw new IndexOutOfBoundsException("INDEKS DILUAR BATAS"); + } + if (index == 1) { + removeHead(); + return; + } + while (start + 2 != index) { + temp = temp.next; + start++; + } + temp.next = temp.next.next; + size--; + } + + void removeForward(T data) { + Node temp = head; + int start = 0; + boolean cek = false; + while (temp.next != null) { + temp = temp.next; + start++; + if (temp.data == data) { + cek = false; + break; + } + } + if (!cek) { + throw new NoSuchElementException("DATA TIDAK ADA"); + } + temp.next = null; + tail = temp; + size = start + 1; + } + + void removeForward(int index) { + Node temp = head; + int start = 0; + boolean cek = false; + while (temp.next != null) { + temp = temp.next; + start++; + if (start - 1 == index) { + cek = true; + break; + } + } + if (!cek) { + throw new IndexOutOfBoundsException("INDEKS DILUAR BATAS"); + } + temp.next = null; + tail = temp; + size = index + 1; + } + + void removeBackward(T data) { + Node temp = head; + int start = 0; + boolean cek = false; + while (temp.next != null) { + temp = temp.next; + start++; + if (temp.data == data) { + cek = true; + break; + } + } + if (!cek) { + throw new NoSuchElementException("DATA TIDAK ADA"); + } + Node temp2 = temp.next; + head = temp; + head.next = temp2; + size = size - start; + } + + void removeBackward(int index) { + Node temp = head; + int start = 0; + boolean cek = false; + while (temp.next != null) { + temp = temp.next; + start++; + if (start - 1 == index) { + cek = true; + break; + } + } + if (!cek) { + throw new IndexOutOfBoundsException("DATA DILUAR DATA"); + } + Node temp2 = temp.next; + head = temp; + head.next = temp2; + size = size - start; + } + + void mergeAppend(SingleLinkedList list) { + Node temp = head; + while (temp.next != null) { + temp = temp.next; + } + temp.next = list.head; + tail = list.tail; + size += list.size(); + } + + void mergePrepand(SingleLinkedList list) { + list.tail.next = head; + head = list.head; + size += list.size(); + } + + void print() { + Node temp = head; + System.out.println(size); + if (temp == null) { + System.out.println("EMPTY LIST"); + return; + } + while (temp.next != null) { + System.out.printf("[%s] -> ", temp.data); + temp = temp.next; + } + System.out.printf("[%s] -> null\n", temp.data); + } + + void printFrom(T data) { + Node temp = head; + boolean cek = false; + while (temp.next != null) { + temp = temp.next; + if (temp.data == data) { + cek = true; + break; + } + break; + } + if (!cek) { + throw new NoSuchElementException("DATA TIDAK ADA"); + } + System.out.println(size); + while (temp.data != data) { + temp = temp.next; + } + while (temp.next != null) { + System.out.printf("%s -> ", temp.data); + temp = temp.next; + } + System.out.printf("%s\n", temp.data); + + System.out.printf("head : %s -> tail : %s\n", head.data, tail.data); + } + + void printFrom(int index) { + Node temp = head; + int start = 0; + boolean cek = false; + while (temp.next != null) { + temp = temp.next; + start++; + if (start - 1 == index) { + cek = true; + break; + } + } + if (!cek) { + throw new IndexOutOfBoundsException("INDEKS DILUAR BATAS"); + } + System.out.println(size); + while (temp.next != null) { + System.out.printf("%s -> ", temp.data); + temp = temp.next; + } + System.out.printf("%s\n", temp.data); + + System.out.printf("head : %s -> tail : %s\n", head.data, tail.data); + } + + void describeNode(String data) { + Node temp = head; + int start = 0; + boolean cek = false; + while (temp.next != null) { + temp = temp.next; + start++; + if (temp.data == data) { + cek = true; + break; + } + } + if (!cek) { + throw new NoSuchElementException("DATA TIDAK ADA"); + } + System.out.println("{"); + System.out.printf("\t\"Data\" : %s,\n", data); + System.out.printf("\t\"Index\" : %s\n", start); + System.out.println("}"); + } + + void describeNode(int index) { + Node temp = head; + int start = 0; + boolean cek = false; + while (temp.next != null) { + temp = temp.next; + start++; + if (start - 1 == index) { + cek = true; + break; + } + } + if (!cek) { + throw new IndexOutOfBoundsException("DATA DILUAR BATAS"); + } + System.out.println("{"); + System.out.printf("\tNode \t: %s\n", temp.data); + System.out.printf("\tIndex \t: %s\n", index); + System.out.println("}"); + } + + T get(int index) { + Node temp = head; + int start = 0; + while (start != index) { + temp = temp.next; + start++; + } + return temp.data; + } + + int indexOf(T data) { + Node temp = head; + int start = 0; + while (temp.data != data) { + temp = temp.next; + start++; + } + return start; + } + + int size() { + return size; + } + + boolean isEmpty() { + if (size == 0) { + return true; + } + return false; + } + + boolean isContain(T data) { + Node temp = head; + boolean cek = false; + while (temp.next != null) { + if (temp.data == data) { + return true; + } + temp = temp.next; + if (temp.data == data) { + cek = true; + break; + } + } + if (!cek) { + throw new NoSuchElementException("DATA TIDAK ADA"); + } + return cek; + } +} \ No newline at end of file