diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..1b2425b --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,299 @@ + + + + \ No newline at end of file diff --git a/IntListReview.iml b/IntListReview.iml index c90834f..ff79d70 100644 --- a/IntListReview.iml +++ b/IntListReview.iml @@ -7,5 +7,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/lib/apiguardian-api-1.1.2.jar b/lib/apiguardian-api-1.1.2.jar new file mode 100644 index 0000000..2b678e1 Binary files /dev/null and b/lib/apiguardian-api-1.1.2.jar differ diff --git a/lib/junit-jupiter-5.8.1.jar b/lib/junit-jupiter-5.8.1.jar new file mode 100644 index 0000000..730b9ae Binary files /dev/null and b/lib/junit-jupiter-5.8.1.jar differ diff --git a/lib/junit-jupiter-api-5.8.1.jar b/lib/junit-jupiter-api-5.8.1.jar new file mode 100644 index 0000000..8424eca Binary files /dev/null and b/lib/junit-jupiter-api-5.8.1.jar differ diff --git a/lib/junit-jupiter-engine-5.8.1.jar b/lib/junit-jupiter-engine-5.8.1.jar new file mode 100644 index 0000000..cfa38d2 Binary files /dev/null and b/lib/junit-jupiter-engine-5.8.1.jar differ diff --git a/lib/junit-jupiter-params-5.8.1.jar b/lib/junit-jupiter-params-5.8.1.jar new file mode 100644 index 0000000..1e4d0ec Binary files /dev/null and b/lib/junit-jupiter-params-5.8.1.jar differ diff --git a/lib/junit-platform-commons-1.8.1.jar b/lib/junit-platform-commons-1.8.1.jar new file mode 100644 index 0000000..20185cd Binary files /dev/null and b/lib/junit-platform-commons-1.8.1.jar differ diff --git a/lib/junit-platform-engine-1.8.1.jar b/lib/junit-platform-engine-1.8.1.jar new file mode 100644 index 0000000..54ce076 Binary files /dev/null and b/lib/junit-platform-engine-1.8.1.jar differ diff --git a/lib/opentest4j-1.2.0.jar b/lib/opentest4j-1.2.0.jar new file mode 100644 index 0000000..d500636 Binary files /dev/null and b/lib/opentest4j-1.2.0.jar differ diff --git a/src/ArrayIntList.java b/src/ArrayIntList.java new file mode 100644 index 0000000..96e4bb3 --- /dev/null +++ b/src/ArrayIntList.java @@ -0,0 +1,368 @@ +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class ArrayIntList implements IntList +{ + // internal (private) representation + private int[] buffer; + private int size; //number of "spots used" in the buffer + private final static int INITIAL_CAPACITY = 10; + + public ArrayIntList(){ + buffer = new int[INITIAL_CAPACITY]; + size = 0 ; + } + + /** + * relatively slow liner time O(size) + * because needs shift size items + * addFront-liner resize-liner + * / \ + * O(n) O(n) + * \ / + * O(2n) or O(2size) + * Prepends (inserts) the specified value at the front of the list (at index 0). + * Shifts the value currently at the front of the list (if any) and any + * subsequent values to the right. + * + * @param value value to be inserted + */ + @Override + public void addFront(int value) + { + resize(2*buffer.length); + for (int i = size; i >= 1; i--) + { + buffer[i]=buffer[i-1]; + } + buffer[0]=value; + size++; + +// if(size == buffer.length){ +// //if the size meathces the capacity then i know +// //I need to resize the size of the buffer +// // +// resize(2*buffer.length); +// } +// for (int i = size; i >=1; i--) +// { +// buffer[i]= buffer[i-1]; +// } +// //but value in position [0] +// buffer[0]= value; +// size++; + } + + /** + * fast, constant time if no resize + * can be slow liner time if resize O(n) Or O(size) + * Appends (inserts) the specified value at the back of the list (at index size()-1). + * + * @param value value to be inserted + */ + @Override + public void addBack(int value) + { + if(size == buffer.length){ + //if the size meathces the capacity then i know + //I need to resize the size of the buffer + // + resize(2*buffer.length); + } + + buffer[size] = value; + size++; + + } + + /** + * worst case: add index 0, liner time O(n) + * best csae: add at last, O(1) + * averagecase O(1/2 n) + * Inserts the specified value at the specified position in this list. + * Shifts the value currently at that position (if any) and any subsequent + * values to the right. + * + * @param index index at which the specified value is to be inserted + * @param value value to be inserted + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public void add(int index, int value) + { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } + resize(2*buffer.length); + for (int i = size; i >=index+1 ; i--) + { + if(size= size) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } + + + int removedElement = buffer[index]; + + for (int i = index; i < size - 1; i++) { + buffer[i] = buffer[i + 1]; + } +// + buffer[size - 1] = 0; + size--; + + return removedElement; + + } + + /** + * fast , constant time, O(1) + * Returns the value at the specified position in the list. + * + * @param index index of the value to return + * @return the value at the specified position in this list + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public int get(int index) + { + if(index <0 || index>=size) { + throw new IndexOutOfBoundsException("index is out of range"); + } + + return buffer[index]; + } + + /** + * WcC liner time o(n) + * when value is not in list + * or value is last in list + * Returns true if this list contains the specified value. + * + * @param value value whose presence in this list is to be searched for + * @return true if this list contains the specified value + */ + @Override + public boolean contains(int value) + { + for (int i = 0; i < size; i++) + { + if(buffer[i] == value) + return true; + } + return false; + } + + /** + * * WC liner time o(n) + * * when value is not in list + * * or value is last in list + * Returns the index of the first occurrence of the specified value + * in this list, or -1 if this list does not contain the value. + * + * @param value value to search for + * @return the index of the first occurrence of the specified value in this list + * or -1 if this list does not contain the value + */ + @Override + public int indexOf(int value) + { + for (int i = 0; i < size; i++) + { + if(buffer[i] == value){ + return i; + } + } + return -1; + } + + /** + * fast + * Returns true if this list contains no values. + * + * @return true if this list contains no values + */ + @Override + public boolean isEmpty() + { + if (size<1) return true; + return false; + } + + /** + * Returns the number of values in this list. + * + * @return the number of values in this list + */ + @Override + public int size() + { + return size; + } + + /** + * Removes all the values from this list. + * The list will be empty after this call returns. + */ + @Override + public void clear() + { + for (int i = 0; i < size; i++) + { + buffer[i] = 0; + } + size =0; + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() + { //return a new instance of + return new ArrayIntListIterator(); + + + } + + @Override + //slow liner time, o(n) + public String toString(){ + if(size == 0){ + return"[]"; + } + StringBuilder sb = new StringBuilder(); + sb.append("["); + + sb.append(buffer[0]); // we know there is at least a value at[0] + // then start at [1]; + for (int i = 1; i < size; i++) + { + + sb.append(", " ); + sb.append(buffer[i]); + } + + sb.append("]"); + return sb.toString(); + } +/* +Slow - liner time, 0(n) or(size) + */ + private void resize(int newSize){ + // crete a new array that is of the new size + int [] temp = new int[newSize]; + + + // copy over values form the exiting buffer + for (int i = 0; i < size; i++) + { + temp[i] = buffer[i]; + } + + // make the switchover + buffer = temp; + } + public class ArrayIntListIterator implements Iterator{ + + private int currentPosition; + + public ArrayIntListIterator(){ + currentPosition =0; + } + + /** + * Returns {@code true} if the iteration has more elements. + * (In other words, returns {@code true} if {@link #next} would + * return an element rather than throwing an exception.) + * + * @return {@code true} if the iteration has more elements + */ + @Override + public boolean hasNext() + { +// if(currentPosition{theList.removeBack(); + }); + } + + @org.junit.jupiter.api.Test + void removeBackSingletonList() + { + } + + @org.junit.jupiter.api.Test + void removeBackfromListOf10() + { + //create a list + ArrayIntList theList = new ArrayIntList(); + + // fill list with 10 items + for (int i = 0; i < 10; i++) + { + theList.addBack(i); + } + theList.removeBack(); + String out = theList.toString(); + assertEquals(out, "[0, 1, 2, 3, 4, 5, 6, 7, 8]"); + } + + @org.junit.jupiter.api.Test + void remove() + { + } + + @org.junit.jupiter.api.Test + void get() + { + } + + @org.junit.jupiter.api.Test + void contains() + { + } + + @org.junit.jupiter.api.Test + void indexOf() + { + } + + @org.junit.jupiter.api.Test + void isEmpty() + { + } + + @org.junit.jupiter.api.Test + void size() + { + } + + @org.junit.jupiter.api.Test + void clear() + { + } + + @org.junit.jupiter.api.Test + void iterator() + { + } + + @org.junit.jupiter.api.Test + void testToString() + { + } +} \ No newline at end of file diff --git a/src/IntList.java b/src/IntList.java index 398c27b..ff4a58a 100644 --- a/src/IntList.java +++ b/src/IntList.java @@ -17,6 +17,8 @@ public interface IntList extends Iterable { * Appends (inserts) the specified value at the back of the list (at index size()-1). * @param value value to be inserted */ + + void addBack(int value); /** diff --git a/src/LinkedIntList.java b/src/LinkedIntList.java new file mode 100644 index 0000000..7d967b9 --- /dev/null +++ b/src/LinkedIntList.java @@ -0,0 +1,263 @@ +import java.util.Iterator; + +public class LinkedIntList implements IntList +{ //helper inner/ nested class + public class Node{ + int data; + Node next; +//default + public Node(){ + data = 0; + next = null; + } + public Node(int data, Node next){ + this.data = data; + this.next = next; + } + }//end of the node class + + //field for linkedIntlist class + private Node head; + private int size; + public LinkedIntList(){ + head = null; + size = 0; + } + + + /** + * head is var that hold the memory of first node object + * + * Prepends (inserts) the specified value at the front of the list (at index 0). + * Shifts the value currently at the front of the list (if any) and any + * subsequent values to the right. + * + * @param value value to be inserted + */ + @Override + public void addFront(int value) + { + + // if the list is not empty + head = new Node(value,head); + size++; + } + + /** + * Appends (inserts) the specified value at the back of the list (at index size()-1). + * + * @param value value to be inserted + */ + @Override + public void addBack(int value) + { + + //set a pointer + Node cur = head; + while (cur.next != null){ + cur = cur.next; + } + cur.next = new Node(value,null); + size++; + } + + /** + * Inserts the specified value at the specified position in this list. + * Shifts the value currently at that position (if any) and any subsequent + * values to the right. + * + * @param index index at which the specified value is to be inserted + * @param value value to be inserted + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public void add(int index, int value) + { // if index is greater than size or index is less than zero + if( index< 0 || index >= size()){ + return; + } + //if index is zero + if(index == 0) addFront(value); + Node newNode = new Node(value,null); + Node cur = head; + for (int i = 0; i < index - 1; i++) + { + cur = cur.next; + } + newNode.next = cur.next; + cur.next = newNode; + size++; + } + + /** + * Removes the value located at the front of the list + * (at index 0), if it is present. + * Shifts any subsequent values to the left. + */ + @Override + public void removeFront() + { + if( head == null ) return; + if(head.next == null){ + head = null; + } + head= head.next; + size--; + } + + /** + * Removes the value located at the back of the list + * (at index size()-1), if it is present. + */ + @Override + public void removeBack() + { + if(head == null) return; + if(head.next == null) { + head = null; + size = 0; + return; + } + Node cur = head; + while(cur.next.next != null){ + cur = cur.next; + } + cur.next = null; + size--; + } + + /** + * Removes the value at the specified position in this list. + * Shifts any subsequent values to the left. Returns the value + * that was removed from the list. + * + * @param index the index of the value to be removed + * @return the value previously at the specified position + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public int remove(int index) + { + return 0; + } + + /** + * Returns the value at the specified position in the list. + * + * @param index index of the value to return + * @return the value at the specified position in this list + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public int get(int index) + { + if(head == null || index < 0 || index >= size){ + throw new IndexOutOfBoundsException("index is out of range"); + } + Node cur = head; + int count = 0 ; + while (count < index){ + cur = cur.next; + count ++; + } + return cur.data; + } + + /** + * Returns true if this list contains the specified value. + * + * @param value value whose presence in this list is to be searched for + * @return true if this list contains the specified value + */ + @Override + public boolean contains(int value) + { + if(head == null) return false; + + //check if the fist node is true + + Node cur = head; + while(cur != null){ + if(cur.data == value){ + return true; + } + cur = cur.next; + } + return false; + } + + /** + * Returns the index of the first occurrence of the specified value + * in this list, or -1 if this list does not contain the value. + * + * @param value value to search for + * @return the index of the first occurrence of the specified value in this list + * or -1 if this list does not contain the value + */ + @Override + public int indexOf(int value) + { + if(head == null) return -1; + Node cur = head; + int count = 0; + while(cur != null){ + if(cur.data == value){ + return count; + } + cur = cur.next; + count++; + } + return -1; + } + + /** + * Returns true if this list contains no values. + * + * @return true if this list contains no values + */ + @Override + public boolean isEmpty() + { + return head == null; + } + + /** + * Returns the number of values in this list. + * + * @return the number of values in this list + */ + @Override + public int size() + { + if(head == null) return 0; + int count = 0 ; + Node cur = head; + while(cur != null){ + count++; + cur = cur.next; + } + return count; + } + + /** + * Removes all the values from this list. + * The list will be empty after this call returns. + */ + @Override + public void clear() + { + head = null; + size = 0; + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() + { + return null; + } +} diff --git a/src/Main.java b/src/Main.java index 930198c..4495cb2 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,15 +1,53 @@ -//TIP To Run code, press or -// click the icon in the gutter. +import java.util.Iterator; + public class Main { public static void main(String[] args) { //TIP Press with your caret at the highlighted text // to see how IntelliJ IDEA suggests fixing it. - System.out.printf("Hello and welcome!"); + System.out.println("Hello and welcome!"); + //Noted. arrays in java are fixed sized + int[] arrayOfNumbers = new int[10]; + + IntList list1= new ArrayIntList(); + IntList list2 = new LinkedIntList(); + + list1.addBack(42); + list1.addBack(82); + list1.addBack(97); + + + + + System.out.println(list1); + +// list1.removeBack(); +// System.out.println("removeBack"+list1); + + list1.addFront(77); + System.out.println("addFront"+list1); + + list1.add(2, 11); + System.out.println("add "+list1); + + list1.removeFront(); + System.out.println("removeFront "+ list1); + + System.out.println("index 0 is "+list1.get(0)); + System.out.println("contain value 42: " + list1.contains(42)); + System.out.println("contain value 38 " + list1.contains(38)); + + System.out.println("Index of 42: "+ list1.indexOf(42)); + System.out.println("Index of 38: "+ list1.indexOf(38)); + + System.out.println("check if empty: "+list1.isEmpty()); + + System.out.println("size is :"+ list1.size()); + +// list1.clear(); +// System.out.println(list1); - for (int i = 1; i <= 5; i++) { - //TIP Press to start debugging your code. We have set one breakpoint - // for you, but you can always add more by pressing . - System.out.println("i = " + i); - } + Iterator it = list1.iterator(); + System.out.println(it.next()); + System.out.println(it.next()); } } \ No newline at end of file