diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..6111a9a --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..92e95df --- /dev/null +++ b/pom.xml @@ -0,0 +1,25 @@ + + 4.0.0 + + org.yandrut + CustomDataStructure + 1.0-SNAPSHOT + jar + + CustomDataStructure + http://maven.apache.org + + + UTF-8 + + + + + junit + junit + 3.8.1 + test + + + diff --git a/src/main/java/org/yandrut/CustomList/CustomArrayList.java b/src/main/java/org/yandrut/CustomList/CustomArrayList.java new file mode 100644 index 0000000..b8a459b --- /dev/null +++ b/src/main/java/org/yandrut/CustomList/CustomArrayList.java @@ -0,0 +1,130 @@ +package org.yandrut.CustomList; +import java.util.Iterator; + +public class CustomArrayList implements CustomList { + private T[] myArrayList; + private int size; + + public CustomArrayList() { + myArrayList = (T[]) new Object[10]; + size = 0; + } + + @Override + public void add(T t) { + if (size == myArrayList.length) { + T[] temporaryArray = (T[]) new Object[size + size / 3]; + for (int i = 0; i < size(); i++) { + temporaryArray[i] = myArrayList[i]; + } + myArrayList = temporaryArray; + } + myArrayList[size] = t; + size++; + } + + @Override + public T get(int index) { + checkOutOfBounds(index); + return myArrayList[index]; + } + + @Override + public int size() { + return this.size; + } + + @Override + public boolean contains(Object toFind) { + for (int i = 0; i < size; i++) { + if (myArrayList[i].equals(toFind)) { + return true; + } + } + return false; + } + + @Override + public T remove(int index) { + checkOutOfBounds(index); + T removedObject = myArrayList[index]; + + if (index == size - 1) { + size--; + } else { + for (int i = index; i < size - 1; i++) { + myArrayList[i] = myArrayList[i + 1]; + } + size--; + } + return removedObject; + } + + @Override + public boolean remove(Object object) { + if (this.contains(object)) { + remove(indexOf(object)); + return true; + } + return false; + } + + @Override + public int indexOf(Object object) { + for (int i = 0; i < size; i++) { + if (myArrayList[i].equals(object)) { + return i; + } + } + return -1; + } + + @Override + public boolean isEmpty() { + return (size == 0); + } + + @Override + public T set(int index, T element) { + if (index < 0 || index > size - 1) { + throw new IndexOutOfBoundsException("Index " + index + " out of bounds for length " + size); + } + myArrayList[index] = element; + return element; + } + + @Override + public boolean addAll(CustomList list) { + if (list != null) { + for (int i = 0; i < list.size(); i++) { + this.add(list.get(i)); + } + return true; + } + return false; + } + private void checkOutOfBounds(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Index " + index + " out of bounds for length " + size); + } + } + + @Override + public Iterator iterator() { + return new MyIterator(); + } + + private class MyIterator implements Iterator { + private int index; + + @Override + public boolean hasNext() { + return index < size(); + } + + @Override + public T next() { + return myArrayList[index++]; + } + } +} \ No newline at end of file diff --git a/src/main/java/org/yandrut/CustomList/CustomLinkedList.java b/src/main/java/org/yandrut/CustomList/CustomLinkedList.java new file mode 100644 index 0000000..1dfe50f --- /dev/null +++ b/src/main/java/org/yandrut/CustomList/CustomLinkedList.java @@ -0,0 +1,204 @@ +package org.yandrut.CustomList; +import java.util.Iterator; +import java.util.Objects; + +public class CustomLinkedList implements CustomList { + + private static class Node { + private E currentElement; + private Node previous; + private Node next; + + public Node (E element) { + this.currentElement = element; + } + + public E getCurrentElement() { + return currentElement; + } + } + + private Node first; + private Node last; + private int size; + + public CustomLinkedList() { + this.first = null; + this.last = null; + size = 0; + } + + @Override + public void add(E t) { + Node currentNode = new Node<>(t); + if (this.isEmpty()) { + first = last = currentNode; + } + else if (size >= 1) { + last.next = currentNode; + currentNode.previous = last; + last = currentNode; + } + size++; + } + + @Override + public E get(int index) { + throwIfIndexOutOfBounds(index); + + Node currentNode = first; + int flag = 0; + while (Objects.nonNull(currentNode) && flag != index) { + currentNode = currentNode.next; + flag++; + } + return currentNode.getCurrentElement(); + } + + @Override + public int size() { + return this.size; + } + + @Override + public boolean contains(Object obj) { + Node currentNode = first; + while (currentNode != null) { + if (obj.equals(currentNode.getCurrentElement())) { + return true; + } + currentNode = currentNode.next; + } + return false; + } + + @Override + public E remove(int index) { + throwIfIndexOutOfBounds(index); + + E forRemoval = null; + int currentIndex = 1; + + // Removing the only element if size is 1 + if (this.size() == 1) { + forRemoval = first.getCurrentElement(); + first = last = null; + } + // Removing first element if size is more than 1 + else if (index == 0) { + forRemoval = first.getCurrentElement(); + first.next.previous = null; + first = first.next; + } + //Removing last element + else if (index == size() -1) { + forRemoval = last.getCurrentElement(); + last.previous.next = null; + last = last.previous; + } else { + Node currentNode = first.next; + Node previousOfCurrent = currentNode.previous; + Node nextOfCurrent = currentNode.next; + + // Removing at all the other possible scenarios + while (Objects.nonNull(currentNode)) { + if (currentIndex == index) { + forRemoval = currentNode.getCurrentElement(); + previousOfCurrent.next = currentNode.next; + nextOfCurrent.previous = previousOfCurrent; + currentNode = currentNode.next; + currentIndex++; + break; + } + } + } + size--; + return forRemoval; + } + + @Override + public boolean remove(Object obj) { + if (this.contains(obj)) { + remove(indexOf(obj)); + return true; + } + return false; + } + + @Override + public int indexOf(Object o) { + Node currentNode = first; + int currentIndex = 0; + while (Objects.nonNull(currentNode)) { + if (o.equals(currentNode.getCurrentElement())) { + return currentIndex; + } + currentNode = currentNode.next; + currentIndex++; + } + return -1; + } + + @Override + public boolean isEmpty() { + return (Objects.isNull(first) && Objects.isNull(last)); + } + + @Override + public E set(int index, E element) { + throwIfIndexOutOfBounds(index); + Node currentNode = first; + int currentIndex = 0; + while (currentNode != null) { + if (currentIndex == index) { + currentNode.currentElement = element; + return currentNode.getCurrentElement(); + } + currentNode = currentNode.next; + currentIndex++; + } + return null; + } + + @Override + public boolean addAll(CustomList myList) { + if (myList != null) { + for (int i = 0; i < myList.size(); i++) { + this.add(myList.get(i)); + } + return true; + } + return false; + } + + private void throwIfIndexOutOfBounds(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index " + index + " out of bounds for length " + size); + } + } + + @Override + public Iterator iterator() { + return new MyIterator(); + } + + private class MyIterator implements Iterator { + private Node currentNode; + + public MyIterator () { + currentNode = first; + } + + @Override + public boolean hasNext() { + return (currentNode != null); + } + + @Override + public E next() { + E currentElement = currentNode.getCurrentElement(); + currentNode = currentNode.next; + return currentElement; + } + } +} \ No newline at end of file diff --git a/src/main/java/org/yandrut/CustomList/CustomList.java b/src/main/java/org/yandrut/CustomList/CustomList.java new file mode 100644 index 0000000..c569df8 --- /dev/null +++ b/src/main/java/org/yandrut/CustomList/CustomList.java @@ -0,0 +1,14 @@ +package org.yandrut.CustomList; + +public interface CustomList extends Iterable { + void add(E t); + E get (int index); + int size(); + boolean contains (Object obj); + E remove(int index); + boolean remove(Object o); + int indexOf(Object o); + boolean isEmpty(); + E set(int index, E element); + boolean addAll(CustomList myList); +} \ No newline at end of file diff --git a/src/test/java/org/yandrut/ArrayListTest.java b/src/test/java/org/yandrut/ArrayListTest.java new file mode 100644 index 0000000..aab69b9 --- /dev/null +++ b/src/test/java/org/yandrut/ArrayListTest.java @@ -0,0 +1,18 @@ +package org.yandrut; + +import java.util.ArrayList; + +public class ArrayListTest { + public static void mainT() { + long start = System.nanoTime(); + ArrayList list = new ArrayList<>(); + for (int i = 0; i < 1000; i++) { + list.add("Object" + i); + } + for (int i = 0; i < list.size(); i++) { + list.remove(i); + } + long duration = (System.nanoTime() - start)/1000000; + System.out.println("Ended in: " + duration + " ms."); + } +} diff --git a/src/test/java/org/yandrut/CustomArrayListTest.java b/src/test/java/org/yandrut/CustomArrayListTest.java new file mode 100644 index 0000000..8a5e53c --- /dev/null +++ b/src/test/java/org/yandrut/CustomArrayListTest.java @@ -0,0 +1,34 @@ +package org.yandrut; + +import org.yandrut.CustomList.CustomArrayList; +import org.yandrut.CustomList.CustomList; + +public class CustomArrayListTest { + public static void main(String [] args) { + long start = System.nanoTime(); + + CustomList list = new CustomArrayList<>(); + for (int i = 0; i < 20; i++) { + list.add("Object" + i); + } + long duration = (System.nanoTime() - start)/1000000; + + System.out.println("Duration: " + duration + " ms."); + System.out.println("Size before: " + list.size()); + + CustomList al = new CustomArrayList<>(); + al.add("One"); + al.add("Two"); + al.add("Three"); + al.add("Four"); + al.add("Five"); + al.add("Six"); + + list.addAll(al); + System.out.println("Size after: " + list.size()); + + for (String s : list) { + System.out.println(s); + } + } +} \ No newline at end of file diff --git a/src/test/java/org/yandrut/CustomLinkedListTest.java b/src/test/java/org/yandrut/CustomLinkedListTest.java new file mode 100644 index 0000000..697bdc7 --- /dev/null +++ b/src/test/java/org/yandrut/CustomLinkedListTest.java @@ -0,0 +1,23 @@ +package org.yandrut; + +import org.yandrut.CustomList.CustomLinkedList; +import org.yandrut.CustomList.CustomList; + +public class CustomLinkedListTest { + public static void main(String[] args) { + CustomList strings = new CustomLinkedList<>(); + strings.add("first"); + strings.add("second"); + strings.add("third"); + strings.add("fourth"); + strings.add("fifth"); + strings.add("sixth"); + strings.add("seventh"); + strings.add("ninth"); + + for (String i : strings) { + System.out.println(strings.remove(i)); + System.out.println(strings.size()); + } + } +}