diff --git a/.idea/misc.xml b/.idea/misc.xml index 6f29fee..5af9c98 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/src/ArrayIntList.java b/src/ArrayIntList.java new file mode 100644 index 0000000..f67b8c8 --- /dev/null +++ b/src/ArrayIntList.java @@ -0,0 +1,335 @@ +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class ArrayIntList implements IntList { + + //fields + + // the number of indexes in the array which have a value. + private int size; + private int[] buffer; + + public ArrayIntList(){ + //initialize fields + size = 0; + buffer = new int[10]; + } + + /** + * 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 buffer if all its index spaces are filled + if(size == buffer.length){ + resize(size * 2); + } + + for (int i = size; i >= 1 ; i--) { // Count 5,4,3,2,1 + buffer[i] = buffer[i - 1]; //copy from 4,3,2,1,0 + } + +// for (int i = size - 1; i >= 0 ; i--) { +// buffer[i +1] = buffer[i]; +// } +// buffer[4] <- buffer[3]; +// buffer[4] <- buffer[3]; +// buffer[4] <- buffer[3]; +// buffer[4] <- buffer[3]; + + //put the value at the front of the array at position 0 + buffer[0] = value; + 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){ +// int[] temp = new int[size + 10]; +// for (int i = 0; i < size; i++) { +// temp[i] = buffer[i]; +// +// } +// //point to the new buffer +// buffer = temp; +// } + if(size == buffer.length){ + resize(size*2); + } + + buffer[size] = value; + 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 < 0){ + throw new IndexOutOfBoundsException("Index can not be less than 0"); + } + if(index >= size){ + throw new IndexOutOfBoundsException("Index must be less than size " + size); + } + if (size == buffer.length){ + resize(size*2); + } + if(index < size){ + //index less than size, move every value from size to index to the right + // buffer[i] = buffer[i -1] + //buffer [index] = value + for (int i = size; i > index; i--){ + buffer[i] = buffer[i-1]; + + } + buffer[index] = value; + 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(!isEmpty()){ + for (int i = 0; i < size - 1; i++) { + buffer[i] = buffer[i+1]; + } + //optional, but a good idea - since we shifted everything to the left by 1. + //we want to clear out the right-most value to be zero + buffer[size-1] = 0; + size--; + } + } + + /** + * Removes the value located at the back of the list + * (at index size()-1), if it is present. + */ + @Override + public void removeBack() { + //could also use size>0 + if(!isEmpty()){ + buffer[size-1] = 0; + 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) { + //check if index is out of bounds + if(index >= size){ + throw new IndexOutOfBoundsException("Index is higher than size"); + } + else if(index < 0){ + throw new IndexOutOfBoundsException("Index cannot be negative."); + } + + for (int i = index; i < size; i++) { + buffer[i] = buffer[i+1]; + } + + //Set the last value to zero + buffer[size-1] = 0; + + //don't forget to decrease size + size--; + 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(index >= size || index < 0){ + throw new IndexOutOfBoundsException(); + } + else{ + return buffer[index]; + } + } + + /** + * 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(size == 0 || buffer.length == 0){ + return false; + } + for (int i = 0; i < size; i++){ + if(buffer[i] == value){ + return true; + } + } + 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(size == 0 || buffer.length == 0){ + return -1; + } + for(int i = 0; i < size; i++){ + if(buffer[i] == value){ + return i; + } + } + return -1; + } + + /** + * Returns true if this list contains no values. + * + * @return true if this list contains no values + */ + @Override + public boolean isEmpty() { + + return size() == 0; + } + + /** + * Returns the number of values in this list. + * + * @return the number of values in this list + */ + @Override + public int size() { + //not sure if we are supposed to check if there are actually values, + // but since we don't know if 0 is supposed to be a value in the Array + // we would have to rely on size to tell us. + + return size; + + } + + /** + * Removes all the values from this list. + * The list will be empty after this call returns. + */ + @Override + public void clear() { + //this is one way. +// for (int i = 0; i < size; i++) { +// buffer[i] = 0; +// } + + //create a new buffer, other one just disappears + buffer = new int[10]; + + size = 0; + + } + + private void resize(int newSize){ + + //create a new space, separate from old space (buffer) + int[] newBuffer = new int[newSize]; + + //copy everything over from buffer into newBuffer + for (int i = 0; i < size; i++) { + newBuffer[i] = buffer[i]; + } + + //override old buffer with new buffer + buffer = newBuffer; + + //the old space is no longer "pointed to" and will eventually be cleaned up by the garbage collector. + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + IntListIterator theInterator = new IntListIterator(); + return theInterator; + } + + //create a private helper Iterator class + private class IntListIterator implements Iterator{ + private int i; + + public IntListIterator(){i = 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() { + return i < size; + } + + /** + * Returns the next element in the iteration. + * + * @return the next element in the iteration + * @throws NoSuchElementException if the iteration has no more elements + */ + @Override + public Integer next() { + if (i > size){ + throw new NoSuchElementException("i is now out of bounds."); + } + int currentValue = buffer[i]; + i++; + return currentValue; + } + } + + //iterators are what enables main/client to use a for-each loop on my IntList +} diff --git a/src/DoublyLinkedList.java b/src/DoublyLinkedList.java new file mode 100644 index 0000000..4084144 --- /dev/null +++ b/src/DoublyLinkedList.java @@ -0,0 +1,201 @@ +import java.util.Iterator; + +public class DoublyLinkedList implements IntList{ + + private class Node{ + int data; + + Node next; //address of the node after + + Node prev; //address of the node before + + public Node(){ + next=null; + prev=null; + } + } + + private Node pre; + + private Node post; + + private int size; + + public DoublyLinkedList() { + //an empty list has 2 sentinel(dummy) nodes that serve as bookends + pre = new Node(); + post = new Node(); + pre.next = post; + //pre.next.next = //this is one way + post.prev = pre; + size=0; + } + + /** + * 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) { + + } + + /** + * 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) { + Node theLastOne = post.prev; + + //setup the new node and fill it out (data, prev, next) + Node theNewOne = new Node(); + theNewOne.data = value; + theNewOne.next = post; + theNewOne.prev = pre; + + //go to the end of the list's sentinel, and update its prev + post.prev = theNewOne; + + // go to the node before the new one and update it's next + pre.next = theNewOne; + 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) { + + } + + /** + * 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() { + + } + + /** + * Removes the value located at the back of the list + * (at index size()-1), if it is present. + */ + @Override + public void removeBack() { + //setup a variable for convenience + Node theOneToRemove = post.prev; + + theOneToRemove.prev.next = post; + post.prev = theOneToRemove.prev; + + //optional to clean up + theOneToRemove.next=null; + theOneToRemove.prev=null; + theOneToRemove.data=0; + + 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) { + return 0; + } + + /** + * 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) { + 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) { + return 0; + } + + /** + * Returns true if this list contains no values. + * + * @return true if this list contains no values + */ + @Override + public boolean isEmpty() { + return false; + } + + /** + * Returns the number of values in this list. + * + * @return the number of values in this list + */ + @Override + public int size() { + return 0; + } + + /** + * Removes all the values from this list. + * The list will be empty after this call returns. + */ + @Override + public void clear() { + + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return null; + } +} diff --git a/src/LinkedIntList.java b/src/LinkedIntList.java new file mode 100644 index 0000000..e57aae4 --- /dev/null +++ b/src/LinkedIntList.java @@ -0,0 +1,359 @@ +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class LinkedIntList implements IntList{ + + //define node + //class is type + private class Node { + int data; + Node next; + } + + + //set up the head field + private Node head; + + //set up the size field + private int size; + + + //add a constructor + public LinkedIntList(){ + head = null; + size = 0; + } + + + + /** + * 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) { + Node theNewOne = new Node(); + theNewOne.data = value; + if(head == null){ + // the list is currently empty + head = theNewOne; + //increase size + size++; + } + else{ + // The list has something in it. + //point the new node at the "first" node (we don't want to lose the list) + theNewOne.next = head; + //point the head at the new node + head = theNewOne; + + //increase size + 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) { + //pretty sure we should have a method that builds the node WITH the value + Node theNewNode = new Node(); + theNewNode.data = value; + + //check to see if the LinkedList is empty + if (head == null){ + head = theNewNode; + } + + //set your pointer to move through the linkedlist + Node current = head; + //move through the linked list until you get to the last node + while(current.next != null){ + current = current.next; + } + + //set the current nodes pointer to the new node + current.next = theNewNode; + //increase the size to account for the new node + 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) { + Node current = head; + Node theNewNode = new Node(); + theNewNode.data = value; + int position = 1; + if(head == null && index == 1){ + head = theNewNode; + } + else if(index < 0 ){ + throw new IndexOutOfBoundsException("Index value can not be less than 1."); + } + else if(index > size){ + throw new IndexOutOfBoundsException("Index cannot be greater than size: " + (size)); + } + else{ + while(current.next != null && position != index-1){ + current = current.next; + position++; + } + //add the new node to the next position and increase size + theNewNode.next = current.next; + current.next = theNewNode; + 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() { + 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() { + Node current = head; + while(current.next.next!= null){ + current = current.next; + } + current.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) { + + Node current = head; + int value; + int position = 1; + if(head == null){ + throw new IndexOutOfBoundsException("Can not call remove on an empty linkedlist. "); + } + else if(index < 0 ){ + throw new IndexOutOfBoundsException("Index value can not be less than 1."); + } + else if(index > size){ + throw new IndexOutOfBoundsException("Index cannot be greater than size: " + (size)); + } + else{ + while(current.next.next != null && position != index-1){ + + current = current.next; + position++; + } + //If the node to remove points to null, have previous node point to null + if(current.next.next == null){ + value = current.next.data; + current.next = null; + size --; + } + else{ + value = current.next.data; + current.next = current.next.next; + size --; + } + + } + + return value; + } + + /** + * 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) { + Node current = head; + + if(index < 1 ){ + throw new IndexOutOfBoundsException("Index must be greater than zero"); + }else if(index > size){ + throw new IndexOutOfBoundsException("Index cannot be greater than size: " + size); + } + //move the position and the pointer until the position matches the index position. + int position = 1; + while (index > position){ + current = current.next; + position++; + } + return current.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) { + Node current = head; + + while(current != null){ + if(current.data == value){ + return true; + } + current = current.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) { + Node current = head; + int index = 1; + if(head == null){ + return -1; + } + //iterate over the nodes to look for if current is null + //end loop and returns -1 + while(current != null){ + if (current.data == value){ + return index; + } + + current = current.next; + index++; + } + + //value was not in the linkedList + return -1; + } + + /** + * Returns true if this list contains no values. + * + * @return true if this list contains no values + */ + @Override + public boolean isEmpty() { + return size() == 0; + } + + /** + * 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() { + head = null; + size = 0; + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + singlyLinkedIterator theIterator = new singlyLinkedIterator(); + + return theIterator; + } + + //helper class/type that defines how te iterator works + private class singlyLinkedIterator implements Iterator{ + + //track the current node you are on. + private Node current; + + public singlyLinkedIterator(){ + current = head; + } + + /** + * 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 (current != null){ +// return true; +// } +// else{ +// return false; +// } + return current!=null; + + } + + /** + * Returns the next element in the iteration. + * + * @return the next element in the iteration + * @throws NoSuchElementException if the iteration has no more elements + */ + @Override + public Integer next() { + + if(current == null){ + throw new NoSuchElementException("There is no next one to go to!!"); + } + //change current to next node. + int item = current.data; + current = current.next; + return item; + } + } +} diff --git a/src/Main.java b/src/Main.java index 930198c..278d452 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,15 +1,143 @@ + +import java.util.Iterator; + //TIP To Run code, press or // click the icon in the gutter. 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!"); + + + + ////////////////////// + //// ArrayIntList //// + ////////////////////// + + //wasn't sure how to do tests which broke the program and keep the + // program working for the rest of the tests + IntList firstList; + + ArrayIntList secondList = new ArrayIntList(); + + IntList thirdList = new ArrayIntList(); + thirdList.addFront(15); + thirdList.addFront(12); + thirdList.addBack(8); + thirdList.add(1, 10); + System.out.println("ThirdList built"); + for(int value : thirdList){ + System.out.println(value); + } + + thirdList.removeFront(); + thirdList.removeBack(); + //add more values to continue testing + thirdList.addFront(9); + thirdList.addFront(23); + thirdList.addBack(77); + thirdList.remove(2); + System.out.println("The value at index 3 is: " + thirdList.get(3)); + System.out.println("The buffer array contains 10 " + thirdList.contains(10)); //note that 10 has been removed above + System.out.println("The buffer array contains 77 " + thirdList.contains(77)); + System.out.println("The index position of value 4 is " + thirdList.indexOf(4)); + System.out.println("This index position of 9 is " + thirdList.indexOf(9)); + System.out.println("The array thirdList is empty: " + thirdList.isEmpty()); + System.out.println("The array secondList is empty: " + secondList.isEmpty()); + System.out.println("The size of the thirdList is: " + thirdList.size()); + thirdList.addFront(9); + thirdList.addFront(23); + thirdList.addBack(77); + thirdList.addFront(15); + thirdList.addFront(12); + thirdList.addBack(8); + thirdList.add(1, 10); + System.out.println("The new size of the thirdList is: " + thirdList.size()); + + //Where an iterator gets used +// for(int value : thirdList){ +// System.out.println(value); +// } + + //alternate way to use an iterator + Iterator itr = thirdList.iterator(); + while(itr.hasNext()){ + int value = itr.next(); + System.out.println(value); + } - 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); + thirdList.clear(); + System.out.println("Array is cleared " + thirdList.isEmpty()); + //Where an iterator gets used + for(int value : thirdList){ + System.out.println(value); } + + + /////////////////////// + //// LinkedIntList //// + /////////////////////// + + LinkedIntList fourthList = new LinkedIntList(); + System.out.println(); + System.out.println("Start LinkedIntList"); + + fourthList.addFront(25); + fourthList.addFront(30); + fourthList.addBack(6); + System.out.println("add to index 1 value 8"); + fourthList.add(1, 8); + System.out.println("The current value in index 4: " + fourthList.get(4)); + System.out.println("add to index 4 value 10"); + fourthList.add(4, 10); + System.out.println("The current size is " + fourthList.size()); + //Where an iterator gets used + for(int value : fourthList){ + System.out.println(value); + } + + System.out.println("The value at index 1 is: " + fourthList.get(1)); + //this throws an error (as intended) +// System.out.println("The value at index 10 is: " + fourthList.get(10) ); + System.out.println("Contains the number 30 " + fourthList.contains(30)); + System.out.println("Contains the number 20 " + fourthList.contains(20)); + System.out.println("The index position of 6 is " + fourthList.indexOf(6)); + System.out.println("The index position of 10 is " + fourthList.indexOf(10)); + + fourthList.removeFront(); + System.out.println("Removing front node"); + for(int value : fourthList){ + System.out.println(value); + } + + //remove the last value 6 + fourthList.removeBack(); + System.out.println("Removing last node"); + for(int value: fourthList){ + System.out.println(value); + } + + System.out.println("Removing index 2: " + fourthList.remove(2)); + for(int value: fourthList){ + System.out.println(value); + } + + System.out.println("Removing index 2: " + fourthList.remove(2)); + for(int value: fourthList){ + System.out.println(value); + } + + System.out.println("The size of the LinkedList is: " + fourthList.size()); + System.out.println("The linkedlist is empty: " + fourthList.isEmpty()); + System.out.println("Clearing list"); + fourthList.clear(); + System.out.println("The linkedlist is empty: " + fourthList.isEmpty()); + + ///////////////////////////// + //// DoublyLinkedIntList //// + ///////////////////////////// + + } } \ No newline at end of file