Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
4.7 kB
1
Indexable
Never

//List ADT
  interface List<E> {

    int size();

    boolean isEmpty();

    E get(int i);

    E set(int i, E e);

    void add(int i, E e);

    E remove(int i);
}






  class ArrayList<E> implements List<E> {

    private E[] data;
    private int size = 0;
    private static final int CAPACITY = 10;

    public ArrayList() {
        data = (E[]) new Object[CAPACITY];
    }

    public ArrayList(int c) {
        data = (E[]) new Object[c];
    }

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

    @Override
    public boolean isEmpty() {

        if (size == 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public E get(int i) {
        //validate the index
        //i   0---> n-1
        if (i < 0 || i >= size) {
            System.out.println("Invalid index");
            return null;
        } else {
            return data[i];
        }
    }

    @Override
    public E set(int i, E e) {
        //validate the index
        //0---> n-1
        if (i < 0 || i >= size) {
            System.out.println("Invalid index");
            return null;
        }
        //store old value
        E temp = data[i];
        //update
        data[i] = e;
        return temp;
    }

    @Override
    public void add(int i, E e) {

        //validate the index
        //0--->n
        if (i < 0 || i > size) {
            System.out.println("Invalid index");
            return;//exit
        }

        if (size == data.length) {
            resize(2 * data.length);
        }

        //shifting
        for (int k = size - 1; k >= i; k--) {
            data[k + 1] = data[k];
        }
        data[i] = e;
        size++;
    }

    @Override
    public E remove(int i) {
        //validate the index
        if (i < 0 || i >= size) {
            System.out.println("Invalid index");
            return null;
        }

        E temp = data[i];
        //shifting  to left
        for (int k = i; k < size - 1; k++) {
            data[k] = data[k + 1];
        }
        data[size - 1] = null;
        size--;
        return temp;
    }

    //resize
    private void resize(int capacity) {
        E[] temp = (E[]) new Object[capacity];
        //copy all elements
        for (int i = 0; i < size; i++) {
            temp[i] = data[i];
        }
        //change the array
        data = temp;
    }
}

class Main {

    public static void main(String[] args) {

        ArrayList<Integer> list = new ArrayList<>(5);
        list.add(0, 10);
        list.add(1, 20);
        list.add(2, 30);
        list.add(3, 40);
        list.add(4, 50);
        list.add(5, 60);

        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }

    }

    public static int sum(int[] A, int size, int count) {
        int total = 0;

        for (int i = 0; i < count; i++) {
            total += A[i];
        }//end loop

        return total;
    }

    public static int search(int[] A, int size, int count, int key) {
        for (int i = 0; i < count; i++) {
            if (key == A[i]) {
                return i;
            }
        }//end for
        return -1;
    }

    public static void printArray(int[] A, int count, int size) {
        for (int i = 0; i < count; i++) {
            System.out.print(A[i] + "  ");
        }

        System.out.println();
    }

    public static void insrtedSort(int[] A, int count, int size, int key) {

        if (count == size) {
            System.out.println("Array is full!");
            return; //exit
        }
        int i;
        //to find the correct location (index)
        for (i = 0; i < count; i++) {
            if (A[i] > key) {
                break;
            }
        }

        //shift
        for (int j = count - 1; j >= i; j--) {
            A[j + 1] = A[j];
        }
        A[i] = key;
        count++;
    }

    public static void delete(int[] A, int count, int size, int key) {

        if (count == 0) {
            System.out.println("Array is empty!");
            return; //exit
        }

        int index = search(A, size, count, key);
        if (index == -1) {
            System.out.println("The key isn not found!");
            return; //exit
        }

        //shift to left
        for (int i = index; i < size - 1; i++) {
            A[i] = A[i + 1];
        }

        count = count - 1;
    }
}