ArrayList vs LinkedList

Both ArrayList vs LinkedList, are the implementation of List interface in java. But they have different use cases and characteristics.

CustomList.java

public interface CustomList {
int size();
boolean isEmpty();
void add(String value);
void add(int index, String value);
void remove(int index);
String get(int index);
}


CustomLinkedList.java

package com.godel.ds.implementation;

import com.godel.ds.interfaces.CustomList;

@SuppressWarnings("unchecked")
public class CustomLinkedList implements CustomList {
Node head;
int size;

public CustomLinkedList () {
this.head = null;
this.size = 0;
}

@Override
public int size() {
return size;
}

@Override
public boolean isEmpty() {
return size == 0;
}

@Override
public void add(String value) {
Node newNode = new Node<>(value);
if (size == 0)
this.head = newNode;
else {
Node currentNode = this.head;
while (currentNode.nextNode != null)
currentNode = currentNode.nextNode;
currentNode.nextNode = newNode;
}
size++;
}

@Override
public void add(int index, String value) {
Node newNode = new Node<>(value);
Node currentNode = this.head;
for (int i = 0 ; i < index - 1; i++)
currentNode = currentNode.nextNode;
newNode.nextNode = currentNode.nextNode;
currentNode.nextNode = newNode;
size++;
}

@Override
public void remove(int index) {
Node currentNode = head;
for(int i = 0; i < index - 2; i++) {
currentNode = currentNode.nextNode;
}

currentNode.nextNode = currentNode.nextNode.nextNode;
}

@Override
public String get(int index) {
Node currentNode = head;
for(int i = 0; i < index - 1; i++) {
currentNode = currentNode.nextNode;
}
return currentNode.data;
}

private static class Node {
String data;
Node nextNode;

Node(String data) {
this.data = data;
this.nextNode = null;
}
}
}



CustomArrayList.java

import com.godel.ds.interfaces.CustomList;
import java.util.Arrays;

public class CustomArrayList implements CustomList {
private int defaultCapacity = 10;
private String[] elements;
private int size;

public CustomArrayList() {
this.elements = new String[defaultCapacity];
this.size = 0;
}

public CustomArrayList(int initialCapacity) {
if (initialCapacity <= 0) {
throw new IllegalArgumentException("Initial capacity cannot be
negative value");
}
this.elements = new String[initialCapacity];
this.size = 0;
}
@Override
public int size() {
return size;
}

@Override
public boolean isEmpty() {
return size == 0;
}

@Override
public void add(String value) {
ensureCapacity();
elements[size] = value;
size++;
}

@Override
public void add(int index, String value) {
        if (index >= size || index < 0) {
throw new IllegalArgumentException("Index out of bounds");
}
ensureCapacity();
System.arraycopy(elements, index, elements, index+1, size-index);
elements[index] = value;
size++;
}

@Override
public void remove(int index) {
if (index >= size || index < 0) {
throw new IllegalArgumentException("Index out of bounds");
}
String element = elements[index];
System.arraycopy(elements, index+1, elements, index, (size-index)-1);
System.out.println("Deleted: "+ element);
size--;
}

@Override
public String get(int index) {
if (index >= size || index < 0) {
throw new IllegalArgumentException("Index out of bounds");
}
return elements[index];
}

private void ensureCapacity() {
int newSize = elements.length * 2;
elements = Arrays.copyOf(elements, newSize);
}
}

Comments

Popular posts from this blog

MongoDB

Git

JAXB