From 8ee67a64a4518fd875865b8fd07862da1bec1947 Mon Sep 17 00:00:00 2001 From: Fuad Hamdi Bahar Date: Mon, 17 Feb 2020 12:06:46 +0800 Subject: [PATCH 1/2] TEST --- assignment-01/H071181015/test.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 assignment-01/H071181015/test.py diff --git a/assignment-01/H071181015/test.py b/assignment-01/H071181015/test.py new file mode 100644 index 0000000..e69de29 From a9ff1e5e88bdba7488f56d3b1ec5126ed2eef176 Mon Sep 17 00:00:00 2001 From: Fuad Hamdi Bahar Date: Fri, 21 Feb 2020 00:01:04 +0800 Subject: [PATCH 2/2] SingleLinkedList :) --- .../H071181015/bin/com/belajar/Main.class | Bin 0 -> 449 bytes .../H071181015/bin/com/belajar/Node.class | Bin 0 -> 313 bytes .../bin/com/belajar/SingleLinkedList.class | Bin 0 -> 906 bytes .../H071181015/src/com/belajar/Main.java | 14 + .../H071181015/src/com/belajar/Node.java | 10 + .../src/com/belajar/SingleLinkedList.java | 581 ++++++++++++++++++ assignment-01/H071181015/test.py | 0 7 files changed, 605 insertions(+) create mode 100644 assignment-01/H071181015/bin/com/belajar/Main.class create mode 100644 assignment-01/H071181015/bin/com/belajar/Node.class create mode 100644 assignment-01/H071181015/bin/com/belajar/SingleLinkedList.class create mode 100644 assignment-01/H071181015/src/com/belajar/Main.java create mode 100644 assignment-01/H071181015/src/com/belajar/Node.java create mode 100644 assignment-01/H071181015/src/com/belajar/SingleLinkedList.java delete mode 100644 assignment-01/H071181015/test.py 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 0000000000000000000000000000000000000000..814d8ac41dfb4852b77b4fb69efaa1547df4a622 GIT binary patch literal 449 zcmZuuO-sW-6r4?)kH*;6*49=VKW^26K`$N@K?F}x4;4W?ZPKM|nxr93{a;>MQ1A!% zqr|s@)Ps9?^WI}-cG&&=diwxyhJ710nkF`FXlPn!DQsKVQrJOh@-}oL>afVD3i=-E MI%LMML3<62Z`KY}nE(I) literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..cb63d5cea52bc263d06a89013c4650de6ec7029a GIT binary patch literal 313 zcmZWk!D_-l6r7Ez>Bdy8wjbb4Q!vLKD!mjui9IOZH*t{-aRVWWzvV%M9{K@(lytV2 zf@OJc_q~}nJMVJ+`Udcdrw~3m5)E_%JO=0q_*Nfkf!-`Dw@IoCm8&Y5muvk-Y^`eL zuw3m`QYgDg7IkIphFEiAtf}7xo`?V8(Tc+#6cOmmjMekqHr3Tqr3E|TqTE%Po*Gw3 z_tn_xN08`82yidZy*0zupNG#h*BL!GhNBe*T*l{e9^(#8W;hLI!H@`fxA=<5aGWqc bL&Ps9c*kE(#N4KaoANm)c?2Fl)(vdv{oOD&v1Q<%fbXr6O@UaY(d(4zsx4cxU#j(*s?6BH{-^}R6M?{v-L>6a z0dFSzoRLS&5SXjjT~!-)>Z-pd>usJGPG32)OT>7l(vt6FsV%z)CClyG-Gj0&4M@jP zIw4}YvT<0I&PA!;VU8;hwtB;ULp`=NUfg0`Tao8bH7oYO-PWKc<`ET`zuvPxkDD+N zF_A&UK-NSKd6I4p(#730OC~)%t+!OeHBms(z_y7J?h7QRA+K{&cUQeg) zoJ()WK>S>@-77uQVkle5j-F&wx>8ShLVqz5fm`FVt0nAsJdHO3J2%Yn0+>)l;gt5q8Kzj-FnlL(JLzhtBkDETVjOQ`w#pEJhR*^|JV15WqJl%nyZpOMd~vG xF~j#A!F_Ljgpl 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 diff --git a/assignment-01/H071181015/test.py b/assignment-01/H071181015/test.py deleted file mode 100644 index e69de29..0000000