diff --git a/assignment-01/H071181501/Main.class b/assignment-01/H071181501/Main.class new file mode 100644 index 0000000..197f2b5 Binary files /dev/null and b/assignment-01/H071181501/Main.class differ diff --git a/assignment-01/H071181501/Main.java b/assignment-01/H071181501/Main.java new file mode 100644 index 0000000..e3a3ac0 --- /dev/null +++ b/assignment-01/H071181501/Main.java @@ -0,0 +1,33 @@ +public class Main { + public static void main(String[] args) { + SingleLinkedList str = new SingleLinkedList(); + SingleLinkedList str2 = new SingleLinkedList(); + SingleLinkedList integer = new SingleLinkedList(); + // int[] numbers = {1, 200, 390, 9, 23, 56, 78}; + // SingleLinkedList list = new SingleLinkedList(); + // for (int number : numbers) { + // list.append(number); + // } + str.append("1"); + str.append("2"); + str.append("3"); + str.append("4"); + str.traverse(); + str2.append("data 1"); + str2.append("data 2"); + str2.append("data 3"); + str2.append("data 4"); + str.mergeAppend(str2); + str.traverse(); + str.get(5); + str.indexOf("data 3"); + + // str.indexOf("6"); + // str.get(2); + // str.size(); + // str.size(); + // str.clear(); + // System.out.println(str.isEmpty()); + // System.out.println(str.contain("6")); + } +} diff --git a/assignment-01/H071181501/Node.class b/assignment-01/H071181501/Node.class new file mode 100644 index 0000000..9bdfd8e Binary files /dev/null and b/assignment-01/H071181501/Node.class differ diff --git a/assignment-01/H071181501/Node.java b/assignment-01/H071181501/Node.java new file mode 100644 index 0000000..d2b443f --- /dev/null +++ b/assignment-01/H071181501/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/assignment-01/H071181501/SingleLinkedList.class b/assignment-01/H071181501/SingleLinkedList.class new file mode 100644 index 0000000..3799897 Binary files /dev/null and b/assignment-01/H071181501/SingleLinkedList.class differ diff --git a/assignment-01/H071181501/SingleLinkedList.java b/assignment-01/H071181501/SingleLinkedList.java new file mode 100644 index 0000000..323a906 --- /dev/null +++ b/assignment-01/H071181501/SingleLinkedList.java @@ -0,0 +1,612 @@ +import java.util.NoSuchElementException; + +public class SingleLinkedList { + 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 appends(T ... datas){ + for(T data : datas){ + append(data); + size++; + } + } + + + public void appendAfterData(T data, T after){ + Node newNode = new Node(data); + Node node = head; + if (head == null) { + head = new Node(data); + size++; + return; + } + if (tail == null) { + tail = new Node(data); + head.setPointer(tail); + size++; + return; + } + if (head.getData() == after){ + newNode.setPointer(node.getPointer()); + head.setPointer(newNode); + size++; + } + while(node.getPointer() != null){ + if(node.getPointer().getData() == after){ + newNode.setPointer(node.getPointer().getPointer()); + node.getPointer().setPointer(newNode); + size++; + return; + } + node = node.getPointer(); + } + if(node.getData() != data){ + append(data); + size++; + return; + } + } + + public void appendAfterIndex(T data, int indeks){ + Node newNode = new Node(data); + Node node = head; + int pos = 0; + if (head == null) { + head = new Node(data); + size++; + return; + } + if (tail == null) { + tail = new Node(data); + head.setPointer(tail); + size++; + return; + } + while(node.getPointer() != null){ + if (pos == indeks){ + newNode.setPointer(node.getPointer()); + node.setPointer(newNode); + size++; + return; + } + pos++; + node = node.getPointer(); + } + if(pos == indeks ){ + append(data); + size++; + return; + } + if(pos != indeks){ + append(data); + size++; + return; + } + } + + public void update(T newData, T data) { + Node newNode = new Node(newData); + Node node = head; + + if (head == null) { + throw new NoSuchElementException("Empty List"); + } + if (head.getData() == data){ + head = newNode; + newNode.setPointer(node.getPointer()); + node.setPointer(null); + return; + } + while (node.getPointer() != null){ + if(node.getPointer().getData() == data ){ + newNode.setPointer(node.getPointer().getPointer()); + node.setPointer(newNode); + return; + } + node = node.getPointer(); + } + throw new NoSuchElementException("No Such Element"); + } + + public void updateIndeks(T newData, int indeks){ + Node newNode = new Node(newData); + Node node = head; + int pos = 0; + + if (head == null) { + throw new NoSuchElementException("Empty List"); + } + if(0 == indeks){ + head = newNode; + newNode.setPointer(node.getPointer()); + node.setPointer(null); + return; + } + while(node.getPointer() != null){ + if((pos+1) == indeks){ + newNode.setPointer(node.getPointer().getPointer()); + node.setPointer(newNode); + return; + } + node = node.getPointer(); + pos++; + } + throw new IndexOutOfBoundsException("Index Out Of Bounds"); + } + + public void prepend(T data) { + if (head == null) { + head = new Node(data); + 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); + Node node = head; + if (head == null) { + head = new Node(data); + size++; + return; + } + if (head.getData() == before){ + newNode.setPointer(head); + head = newNode; + size++; + return; + } + while (node.getPointer() != null){ + if(node.getPointer().getData() == before){ + newNode.setPointer(node.getPointer()); + node.setPointer(newNode); + size++; + return; + } + node = node.getPointer(); + } + if (node.getData() != data){ + prepend(data); + size++; + return; + } + } + + public void prependBeforeIndex(T data, int indeks){ + Node newNode = new Node(data); + Node node = head; + int pos = 0; + + if (head == null) { + head = new Node(data); + size++; + return; + } + if(indeks == 0){ + newNode.setPointer(head); + head = newNode; + size++; + return; + } + while(node.getPointer() != null){ + if((pos+1) == indeks){ + newNode.setPointer(node.getPointer()); + node.setPointer(newNode); + size++; + return; + } + node = node.getPointer(); + pos++; + } + if(pos != indeks){ + prepend(data); + size++; + return; + } + } + + public void prepends(T ... datas){ + for(T data : datas){ + prepend(data); + size++; + } + } + + public void clear() { + head = null; + size = 0; + } + + 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 remove(T data){ + Node node = head; + + if(head.getData() == data){ + removeHead(); + return; + } + while(node.getPointer() != null){ + if(node.getPointer().getData() == data){ + node.setPointer(node.getPointer().getPointer()); + size--; + return; + } + node = node.getPointer(); + } + throw new NoSuchElementException("No Such Element"); + } + + public void removeIndeks(int indeks){ + Node node = head; + int pos = 0; + + if (indeks == 0){ + removeHead(); + return; + } + while(node.getPointer() != null){ + if((pos+1) == indeks){ + node.setPointer(node.getPointer().getPointer()); + size--; + return; + } + node = node.getPointer(); + pos++; + } + 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(); + } + throw new NoSuchElementException("No Such Element"); + } + + public void removeAfterIndeks(int indeks){ + Node node = head; + int pos = 0; + while(node.getPointer() != null){ + if(pos == indeks){ + node.setPointer(node.getPointer().getPointer()); + size--; + return; + } + node = node.getPointer(); + pos++; + } + throw new IndexOutOfBoundsException("Index Out Of Bounds"); + } + + public void removeBeforeData(T data) { + Node node = head; + try{ + if(head.getPointer().getData() == data){ + head = head.getPointer(); + size--; + return; + } + while(node.getPointer() != null){ + if(node.getPointer().getPointer().getData() == data){ + node.setPointer(node.getPointer().getPointer()); + size--; + return; + } + node = node.getPointer(); + } + }catch (Exception e){ + throw new NoSuchElementException("No Such Element"); + } + } + public void removeBeforeIndeks(int indeks) { + Node node = head; + int pos = 0; + + if((pos+1) == indeks){ + head = head.getPointer(); + size--; + return; + } + while(node.getPointer() != null){ + if((indeks) - pos == 2){ + node.setPointer(node.getPointer().getPointer()); + size--; + return; + } + node = node.getPointer(); + pos++; + } + throw new IndexOutOfBoundsException("Index Out Of Bounds"); + } + + public void removeForward(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 removeForwardIndeks(int indeks){ + Node node = head; + int pos = 0; + int count = 0; + try{ + while(pos != indeks){ + node = node.getPointer(); + count++; + pos++; + } + node.setPointer(null); + size = count+1; + }catch (Exception e){ + throw new IndexOutOfBoundsException("Index Out Of Bounds"); + } + } + + public void removeBackward(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 removeBackwardIndeks(int indeks){ + Node node = head; + int count = 0; + int pos = 0; + try{ + while(pos != indeks){ + node = node.getPointer(); + count++; + pos++; + } + head = node; + size -= count; + } + catch (Exception e){ + throw new IndexOutOfBoundsException("Index Out Of Bounds"); + } + } + + public void describeNode(T data) { + if (head == null) { + throw new NoSuchElementException("Empty List"); + } + if (head.getData() == data) { + System.out.printf("{\n data\t: %s\n index\t: %s\n}\n", head.getData(), 0); + return; + } + if (tail.getData() == data) { + System.out.printf("{\n data\t: %s\n index\t: %s\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: %s\n}\n", node.getData(), index); + return; + } + node = node.getPointer(); + index++; + } + throw new NoSuchElementException("No Such Element"); + } + + public void describeNodeIndeks(int indeks){ + int pos = 0; + if (head == null) { + throw new NoSuchElementException("Empty List"); + } + if (0 == indeks) { + System.out.printf("{\n data\t: %s\n index\t: %s\n}\n", head.getData(), 0); + return; + } + if ((size-1) == indeks) { + System.out.printf("{\n data\t: %s\n index\t: %s\n}\n", tail.getData(), size - 1); + return; + } + Node node = head; + int index = 0; + while (node.hasPointer()) { + if (pos == indeks) { + System.out.printf("{\n data\t: %s\n index\t: %s\n}\n", node.getData(), index); + return; + } + node = node.getPointer(); + index++; + pos++; + } + throw new IndexOutOfBoundsException("Index Out Of Bounds"); + } + + public void mergeAppend(SingleLinkedList list){ + tail.setPointer(list.head); + tail = list.tail; + size += list.size; + return; + } + public void mergePrepend(SingleLinkedList list){ + head = list.tail; + list.tail.setPointer(list.head); + size += list.size; + return; + } + + 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 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 indeks){ + try{ + Node node = head; + int pos = 0; + + while(pos != indeks){ + node = node.getPointer(); + pos++; + } + if(pos == indeks){ + 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 get(int indeks) { + Node node = head; + int pos = 0; + while(node.getPointer() != null){ + if(pos == indeks){ + System.out.printf("data => [%s]\n", node.getData()); + return; + } + node = node.getPointer(); + pos++; + } + if (size-1 == indeks){ + System.out.printf("data => [%s]\n", node.getData()); + return; + } + throw new IndexOutOfBoundsException("Index Out Of Bounds"); + } + + public void indexOf(T data){ + Node node = head; + int pos =0; + while(node.getPointer() != null){ + pos++; + node = node.getPointer(); + if(node.getData() == data){ + System.out.printf("indeks => %s\n", pos); + return; + } + } + throw new NoSuchElementException("No Such Element"); + } + + public int size(){ + System.out.println("Size ==> " + size); + return size; + } + + public boolean isEmpty(){ + if (size == 0){ + return true; + } + return false; + } + + public boolean contain(T data){ + Node node = head; + while(node.getPointer() != null){ + if(node.getData() == data){ + return true; + } + node = node.getPointer(); + } + return false; + } +}