From 7f0261230c26df87eca6f857f3be2aa648117b2f Mon Sep 17 00:00:00 2001 From: Iksan03 Date: Fri, 21 Feb 2020 21:40:57 +0800 Subject: [PATCH] tugas 1 --- H071181321/Tugas 1/Main.class | Bin 0 -> 718 bytes H071181321/Tugas 1/Main.java | 22 + H071181321/Tugas 1/Node.class | Bin 0 -> 796 bytes H071181321/Tugas 1/Node.java | 24 + H071181321/Tugas 1/SingleLinkedList.class | Bin 0 -> 9467 bytes H071181321/Tugas 1/SingleLinkedList.java | 650 ++++++++++++++++++++++ 6 files changed, 696 insertions(+) create mode 100644 H071181321/Tugas 1/Main.class create mode 100644 H071181321/Tugas 1/Main.java create mode 100644 H071181321/Tugas 1/Node.class create mode 100644 H071181321/Tugas 1/Node.java create mode 100644 H071181321/Tugas 1/SingleLinkedList.class create mode 100644 H071181321/Tugas 1/SingleLinkedList.java diff --git a/H071181321/Tugas 1/Main.class b/H071181321/Tugas 1/Main.class new file mode 100644 index 0000000000000000000000000000000000000000..191101183075d3304ab450d7637d9978ae6a629a GIT binary patch literal 718 zcmZ`$-%k@k5dOAT?w0F9>nSK8A}HE&mCEl2)WjM|nzZmx0-{e>x-^UJwYgpi{!u>o zQW8!41N@_mGY3hD#=Gn{v$NmMH#7U|_xGOwHnHQv!<>VME>f6dm}ht-vB37R#G=HK z#1o0963ZNW#_(Js&$i%T#epwizcQhT-w0Uw!jVAwUAL(PvQ-o6{XwUpqr;%lrbxOI zm{4Fk|EYQ&Tm+?d5VlJ7I5J^tmoK{Y?jSnVdxpv5H6pJvPvI`^3FPX;Yir8-SvRYu zA1f5`LSdB;#hOA1FUeV_wLxo>))r~@gLt5@hINGvaBy2;3)^(U0+e=C%0c5?pT+`? z>A&yv;x9=<9|dF_1s6K%Q&o1*>*=s5FqOa6!}Z0ahfGIDE&X=H`n&3c<(ROkFAvWC zlcyr!^&-tmWqsC-bcN|Wxqn3~g(EED4m~A*DF~hx;S}xW$qKS#D_0QT$e|R3b`qo! z<31jc&(yE5=m$=IAa!yDt9oN={t)S7f-&E-hj4sPCODp;n_z~p=qdJ#Lrh$gX*+rB zds%CU97jg-W#mZ6f<&4;o8%qL&~AYWT80ZB8G0{;9ZcXgCQ(Kfd&I0!mxt8nhorw5 I%8|nCA8wt6q5uE@ literal 0 HcmV?d00001 diff --git a/H071181321/Tugas 1/Main.java b/H071181321/Tugas 1/Main.java new file mode 100644 index 0000000..350a26a --- /dev/null +++ b/H071181321/Tugas 1/Main.java @@ -0,0 +1,22 @@ +public class Main { + public static void main(String[] args) { + SingleLinkedList str = new SingleLinkedList<>(); + SingleLinkedList str2 = new SingleLinkedList<>(); + + str.isEmpty(); + str.traverse(); + str.append("1"); + str.append("2"); + str.append("3"); + str.append("4"); + str.append("5"); + str2.append("satu"); + str.mergeAppend(str2); + str.traverse(); + str.indexOf("satu"); + str.prependBeforeIndex("6", 0); + str.traverse(); + + } + +} \ No newline at end of file diff --git a/H071181321/Tugas 1/Node.class b/H071181321/Tugas 1/Node.class new file mode 100644 index 0000000000000000000000000000000000000000..8771d7af758b47554a26ae3d8c597fce1e1c6077 GIT binary patch literal 796 zcmZuuO;6iE6r8nV8#`YRKKhv!Qd;aF0f(O2D5}(|r!=jUD2LuQ!Ac##i4v!OsTU3) zfsi3;ip=^Apv0P_n)`BpI z>H_QTqN3NK-)GVUij5%jo70o7KW@wJF%i?Hgz9kme)MBzv*da+qkMXtSviyc`fN98 zQv!wLob)NcqT-i$%{6K~vZ&#D@p{fLIhkAQuJ=GBNGX-n(o}Z(KRbuGGBr3@V)cZ)r+D^=mG;o8lv19P@&Ydz)piH`tLgG; jx?5P8Kgj5(1hja;WXcVci5Sqbp9Bku^((xN+cy6KJ8gtW literal 0 HcmV?d00001 diff --git a/H071181321/Tugas 1/Node.java b/H071181321/Tugas 1/Node.java new file mode 100644 index 0000000..d2b443f --- /dev/null +++ b/H071181321/Tugas 1/Node.java @@ -0,0 +1,24 @@ +public class Node { + private T data; + private Node pointer; + + public Node(T data) { + this.data = data; + } + + public Node getPointer() { + return pointer; + } + + public void setPointer(Node pointer) { + this.pointer = pointer; + } + + public T getData() { + return data; + } + + public boolean hasPointer() { + return pointer != null; + } +} diff --git a/H071181321/Tugas 1/SingleLinkedList.class b/H071181321/Tugas 1/SingleLinkedList.class new file mode 100644 index 0000000000000000000000000000000000000000..addeab309a696c2d3debb9ece18cc85136f59593 GIT binary patch literal 9467 zcmcgx33Qy*b^hL)8O>-k+GQKrl09Cv+hD*LS(f)LXfenb%iv`!jqI_tF&Y`09c-3B z62Jt5S%MANVhbRGWi}g<5S)-wnn37DPn(pUHf?%HLPL^68*KHv?|-vMmci-C=>dNI z^ZvKo``zz;@4m-7Ctp1V;5@wP$5>pGjw)R1#dUtTaJ~GvK^`~C&zt0NvpjbB*|u9A zx5(pGdEDm3?LK_fk1X8b#hpHU&5vB%;ltN`_=XpE`EYkS?!g`(?)BoEe%yz>e%y}- zWWIeqJm|wi^7~;Q9`WH@K0NBfejg6_@R%2m`%!{xe0W0s9+bx+dGvYlq=I`(IMk|; zR@czg8lIz&-W1u~8H)8r!wMOuv1Zk(Iko)aj)fxa{L>S;n%~q44yuWCMq;%JuF|sA z{IZBsC}h_~I>QaU9h<_@RiRDoY)K1scZWN<%Ba%1t)cCq8SSCY%`+M|Z4I}?=E%s* zrdX(DTYacIKA@Cq%7<)cb{a+swG9LL z;*Iny>FAE_G$dck551j{D|<<5cTZO|#<28uw}xU#P~$)CN~23xjKmiq%L+>tPfL3^ z6jkuWqM_~KXb`q-rx~(YVGRAm1lM`QJX|$`u z0sTqJ2|1Md!J1xLv#R>kIcraCQt&r*^+sF5OCyr)qlR;My5xKS=VMj?(=mh4Npz&4 zI)HO=UH~&OBY>yzOaRZ~+W|a>=L5J{0T;Sf)07}lV+q0%x>rk%9KGnrx*PJyom286kp>H2Fa38dUbGW58op~Kl#z> zzs}FFBX8l z=O@h*AAF@{@$JgXb}?9?fQ?Dp?B+5ofgFp?&XR(8)6Sk)xPv*;)hnsw%o6FEv6Ak^ zI3yhEUW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8@QzSKF6&`!&05PMlLqtn3{)g=ErT?&m6bZ)xu5q^vp1(Uj!%oa})8 ziJH62d6S-_<9*~r>fGX)e!Q5IGD&jQ=_NDRTRzdNG&@^*#X-9R+l?z;ikQ1R^)b=< z4UzgC!}AG(>h~yBpHfQyz^(O<+*<#{Qsgt1BL9s`)c;_;`ZNFE#ODaBljvX}utO_c zuQhgS7w*t*+^N&BS9`HvryJCBn4c$5!SM3m5^r(51=IKIwMUTbBdK}j4 zB7}7@+VliObPzpy60Xyeu}e=Oic@j7o`!pLDIU^gIH=1FlDS0kB==$m%4vb(e48&@ z3FWo~T`O$9WRxo@U0rg(Oz=C4I73&#qo>2K<)*$pG4MW$fK+z%P7DH)UXz$!W}}gk zjpA>#Y_cN(Y)LoUiLK%OSq4lQ_AObr8%@wN3DvpC*5@Hd&%!8u0gCi&oUJb;Omk4D zYpf=?a-5275>4#jEs>*B-85fL*k_B0_8^yG_L{!IKiGZA2*gw)5F;qlcy0yI^9aCv z1oQ$F=tZ<(F{bDxn5LJ-S9cC*S-BSBE?UuzE3Fl;TD5F7R+xQ}XM#%6`Xa$#nuCGa z6lJ@Wmluqcrcg8IGqa(bzIU>6qqk0VY0mw+j+WOG!v*wVu#zU`s4sobb$^W&*TLpgE4A9Fl;E7R4j>sbKQ<5Kc)C!zua1 z^LHwz65sleRi6xPznH?CkPWQ8a#)7S-;|S zv0QiK3f+SZ`buonQEb&Q!-FEESr%qb8kAYR==EYdn1O3k3yrJdg$D1BoP9Ks!VKeM z79($6`mOL}j{NvPxZW>yKTlJq-lLd { + private Node head; + private Node tail; + private int size; + + public void append(T data) { + if (head == null) { + head = new Node(data); + size++; + return; + } + if (tail == null) { + tail = new Node(data); + head.setPointer(tail); + size++; + return; + } + Node newNode = new Node(data); + tail.setPointer(newNode); + tail = newNode; + size++; + } + + public void appendAfterData(T data, T after) { + if (head == null) { + head = new Node(data); + size++; + return; + } + if (tail == null) { + tail = new Node(data); + head.setPointer(tail); + size++; + return; + } + + Node node = head; + Node newNode = new Node(data); + + while (node.getPointer() != null) { + if (node.getData() == after) { + newNode.setPointer(node.getPointer()); + node.setPointer(newNode); + size++; + return; + } + if (tail.getData() == after || node.getData() != after) { + append(data); + size++; + return; + } + node = node.getPointer(); + } + } + + public void appendAfterIndex(T data, int index) { + if (head == null) { + head = new Node(data); + size++; + return; + } + if (tail == null) { + tail = new Node(data); + head.setPointer(tail); + size++; + return; + } + + int pos = 0; + Node node = head; + Node newNode = new Node(data); + + while (node.getPointer() != null) { + if (pos == index) { + newNode.setPointer(node.getPointer()); + node.setPointer(newNode); + size++; + return; + } + pos++; + node = node.getPointer(); + } + if ((size - 1) == index || pos != index) { + append(data); + size++; + return; + } + } + + public void appendVarargs(T... datas) { + for (T data : datas) { + append(data); + size++; + } + } + + public void prependVarargs(T... datas) { + for (T data : datas) { + prepend(data); + size++; + } + } + + public void mergeAppend(SingleLinkedList list) { + tail.setPointer(list.head); + tail = list.tail; + size += list.size; + return; + } + + public void mergePrepend(SingleLinkedList list) { + head = list.head; + list.tail.setPointer(list.head); + size += list.size; + return; + } + + public void prepend(T data) { + if (head == null) { + head = new Node(data); + size++; + return; + } + + if (tail == null) { + tail = head; + Node newNode = new Node(data); + newNode.setPointer(tail); + head = newNode; + size++; + return; + } + Node newNode = new Node(data); + newNode.setPointer(head); + head = newNode; + size++; + } + + public void prependBeforeData(T data, T before) { + Node newNode = new Node(data); + + if (head == null) { + head = new Node(data); + size++; + return; + } + if (tail == null) { + tail = head; + newNode.setPointer(tail); + head = newNode; + size++; + return; + } + + Node node = head; + + while (node.getPointer().getPointer() != null) { + if (node.getPointer().getData() == before) { + newNode.setPointer(node.getPointer()); + node.setPointer(newNode); + size++; + return; + } + if (node.getData() == before || node.getData() != before) { + prepend(data); + size++; + return; + } + node = node.getPointer(); + } + } + + public void prependBeforeIndex(T data, int index) { + if (head == null) { + head = new Node(data); + size++; + return; + } + if (tail == null) { + tail = new Node(data); + head.setPointer(tail); + size++; + return; + } + + int pos = 0; + Node node = head; + Node newNode = new Node(data); + + while (node.getPointer().getPointer() != null) { + if ((pos + 1) == index) { + newNode.setPointer(node.getPointer()); + node.setPointer(newNode); + size++; + return; + } + if (0 == index || pos != index) { + prepend(data); + size++; + return; + } + node = node.getPointer(); + pos++; + } + } + + public void removeByData(T data) { + Node node = head; + while (node.getPointer().getPointer() != null) { + if (node.getPointer().getData() == data) { + node.setPointer(node.getPointer().getPointer()); + size--; + return; + } + if (node.getData() == data) { + removeHead(); + } + node = node.getPointer(); + } + if (tail.getData() == data) { + removeTail(); + } + } + + public void removeByIndex(int index) { + int pos = 0; + Node node = head; + while (node.getPointer().getPointer() != null) { + if (index - pos == 1) { + node.setPointer(node.getPointer().getPointer()); + size--; + return; + } + pos++; + node = node.getPointer(); + } + if (0 == index) { + removeHead(); + } + if (size == index) { + removeTail(); + } + if ((size - 1) < index) { + throw new IndexOutOfBoundsException("Index Out Of Bounds"); + } + } + + public void removeAfterData(T data) { + Node node = head; + while (node.getPointer() != null) { + if (node.getData() == data) { + node.setPointer(node.getPointer().getPointer()); + size--; + return; + } + node = node.getPointer(); + } + if (node.getPointer() == tail) { + node.setPointer(null); + tail = node; + } + } + + public void removeAfterIndex(int index) { + int pos = 0; + Node node = head; + while (node.getPointer().getPointer() != null) { + if (pos == index) { + node.setPointer(node.getPointer().getPointer()); + size--; + return; + } + pos++; + node = node.getPointer(); + } + if (size - index == 2) { + removeTail(); + } + if ((size - 1) < index) { + throw new IndexOutOfBoundsException("Index Out Of Bounds"); + } + } + + public void removeBeforeData(T data) { + Node node = head; + while (node.getPointer().getPointer() != null) { + if (node.getData() == data) { + node.setPointer(node.getPointer().getPointer()); + size--; + return; + } + node = node.getPointer(); + } + if (head.getData() == data) { + removeHead(); + } + } + + public void removeBeforeIndex(int index) { + Node node = head; + int pos = 0; + + if ((pos + 1) == index) { + head = head.getPointer(); + size--; + return; + } + while (node.getPointer() != null) { + if ((index) - pos == 2) { + node.setPointer(node.getPointer().getPointer()); + size--; + return; + } + node = node.getPointer(); + pos++; + } + throw new IndexOutOfBoundsException("Index Out Of Bounds"); + } + + public void removeHead() { + if (head == null) { + throw new NoSuchElementException("Empty List"); + } + head = head.getPointer(); + size--; + } + + public void removeTail() { + if (head == null) { + throw new NoSuchElementException("Empty List"); + } else if (tail == null) { + throw new NoSuchElementException("Head"); + } else if (tail == head) { + throw new NoSuchElementException("Head is Tail"); + } + Node node = head; + while (node.getPointer().getPointer() != null) { + node = node.getPointer(); + } + node.setPointer(null); + tail = node; + size--; + } + + public void removeForwardData(T data) { + Node node = head; + int count = 0; + try { + while (node.getData() != data) { + node = node.getPointer(); + count++; + } + node.setPointer(null); + size = count + 1; + } catch (Exception e) { + throw new NoSuchElementException("No Such Element"); + } + } + + public void removeForwardIndex(int index) { + Node node = head; + int pos = 0; + int count = 0; + try { + while (pos != index) { + node = node.getPointer(); + count++; + pos++; + } + node.setPointer(null); + size = count + 1; + } catch (Exception e) { + throw new IndexOutOfBoundsException("Index Out Of Bounds"); + } + } + + public void removeBackwardData(T data) { + Node node = head; + int count = 0; + try { + while (node.getData() != data) { + node = node.getPointer(); + count++; + } + head = node; + size -= count; + } catch (Exception e) { + throw new NoSuchElementException("No Such Element"); + } + } + + public void removeBackwardIndex(int index) { + int pos = 0; + int count = 0; + Node node = head; + try { + while (pos != index) { + node = node.getPointer(); + count++; + pos++; + } + head = node; + size -= count; + } catch (Exception e) { + throw new IndexOutOfBoundsException("Index Out Of Bounds"); + } + } + + public void describeNodeData(T data) { + if (head == null) { + throw new NoSuchElementException("Empty List"); + } + if (head.getData() == data) { + System.out.printf("{\n data\t: %s\n index\t: %d\n}\n", head.getData(), 0); + return; + } + if (tail.getData() == data) { + System.out.printf("{\n data\t: %s\n index\t: %d\n}\n", tail.getData(), size - 1); + return; + } + Node node = head; + int index = 0; + while (node.hasPointer()) { + if (node.getData() == data) { + System.out.printf("{\n data\t: %s\n index\t: %d\n}\n", node.getData(), index); + return; + } + node = node.getPointer(); + index++; + } + throw new NoSuchElementException("No Such Element"); + } + + public void describeNodeIndex(int index) { + if (head == null) { + throw new NoSuchElementException("Empty List"); + } + if (0 == index) { + System.out.printf("{\n data\t: %s\n index\t: %d\n}\n", head.getData(), 0); + return; + } + if ((size - 1) == index) { + System.out.printf("{\n data\t: %s\n index\t: %d\n}\n", tail.getData(), size - 1); + return; + } + Node node = head; + int pos = 0; + while (node.hasPointer()) { + if (pos == index) { + System.out.printf("{\n data\t: %s\n index\t: %d\n}\n", node.getData(), pos); + return; + } + node = node.getPointer(); + pos++; + } + throw new NoSuchElementException("No Such Element"); + } + + public int indexOf(T data) { + int pos = 0; + Node node = head; + + while (node.getPointer() != null) { + pos++; + node = node.getPointer(); + if (node.getData() == data) { + System.out.printf("indeks => %s\n", pos); + return pos; + } + } + throw new NoSuchElementException("No Such Element"); + } + + public boolean isEmpty() { + if (size == 0) { + System.out.println("List is Empty"); + return true; + } + System.out.println("List is filled"); + return false; + } + + public void unique() { + } + + public void sort() { + } + + public void updateByData(T data, T newData) { + if (head == null) + throw new NoSuchElementException("Empty List"); + + Node node = head; + Node newNode = new Node(newData); + + if (node.getData() == data) { + head = newNode; + newNode.setPointer(node.getPointer()); + node.setPointer(null); + return; + } + while (node.getPointer().getPointer() != null) { + if (node.getPointer().getData() == data) { + newNode.setPointer(node.getPointer().getPointer()); + node.setPointer(newNode); + return; + } + node = node.getPointer(); + } + if (tail.getData() == data) { + newNode.setPointer(null); + node.setPointer(newNode); + tail = newNode; + return; + } + if (node.getPointer().getData() != data) { + throw new NoSuchElementException("No Such Element"); + } + } + + public void updateByIndex(int index, T newData) { + if (head == null) + throw new NoSuchElementException("Empty List"); + + int pos = 0; + Node node = head; + Node newNode = new Node(newData); + + if (0 == index) { + head = newNode; + newNode.setPointer(node.getPointer()); + node.setPointer(null); + return; + } + while (node.getPointer().getPointer() != null) { + if (index - pos == 1) { + newNode.setPointer(node.getPointer().getPointer()); + node.setPointer(newNode); + return; + } + pos++; + node = node.getPointer(); + } + if ((size - 1) == index) { + newNode.setPointer(null); + node.setPointer(newNode); + tail = newNode; + return; + } + if ((size - 1) < index) + throw new IndexOutOfBoundsException("Index Out Of Bounds"); + } + + public void clear() { + head = null; + size = 0; + } + + public void traverse() { + if (head == null) { + System.out.println("Empty Linked List"); + return; + } + Node node = head; + while (node.hasPointer()) { + System.out.printf("[%s] -> ", node.getData()); + node = node.getPointer(); + } + System.out.printf("[%s] -> null\n", node.getData()); + } + + public boolean contain(T data) { + Node node = head; + while (node.getPointer() != null) { + node = node.getPointer(); + } + if (node.getData() == data) { + System.out.println("Data is true"); + return true; + } else { + throw new NoSuchElementException("No Such Element"); + } + } + + public T get(int index) { + Node node = head; + int pos = 0; + while (node.getPointer() != null) { + if (pos == index) { + System.out.printf("data => [%s]\n", node.getData()); + return node.getData(); + } + node = node.getPointer(); + pos++; + } + if (size - 1 == index) { + System.out.printf("data => [%s]\n", node.getData()); + return node.getData(); + } + throw new IndexOutOfBoundsException("Index Out Of Bounds"); + } + + public int size() { + System.out.println("Size ::> " + size); + return size; + } + + public void traverseFromData(T data) { + try { + if (head == null) { + System.out.println("Empty Linked List"); + return; + } + Node node = head; + while (node.getData() != data) { + node = node.getPointer(); + } + while (node.getPointer() != null) { + System.out.printf("[%s] -> ", node.getData()); + node = node.getPointer(); + } + System.out.printf("[%s] -> null\n", node.getData()); + } catch (Exception E) { + throw new NoSuchElementException("No Such Element"); + } + } + + public void traverseFromIndex(int index) { + Node node = head; + int pos = 0; + while (node.getPointer().getPointer() != null) { + pos++; + node = node.getPointer(); + } + if (pos == index) { + while (node.getPointer() != null) { + System.out.printf("[%s] -> ", node.getData()); + node = node.getPointer(); + } + System.out.printf("[%s] -> null\n", node.getData()); + return; + } + if (pos != index) { + throw new IndexOutOfBoundsException("Index Out Of Bounds"); + } + } + +}