diff --git a/src/ArrayIntList.java b/src/ArrayIntList.java new file mode 100644 index 0000000..482af93 --- /dev/null +++ b/src/ArrayIntList.java @@ -0,0 +1,275 @@ +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class ArrayIntList implements IntList { + + // fields: + private int size; + private int[] buffer; + + public ArrayIntList() { + // initialize my 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) { + 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 + } + + // 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) { + // TODO: check to see if we are full - if so, we need to create a larger buffer + 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 (size == buffer.length) { + resize(size * 2); + } + + } + + /** + * 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() { + // call remove(0) + if (!isEmpty()) { + for (int i = 0; i <= size - 2; 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() { + // call remove(size-1) + 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) { + // first, check the index to see if it is valid + if (index < 0) { + throw new IndexOutOfBoundsException("Index cannot be negative"); + } + else if (index >= size) { + throw new IndexOutOfBoundsException("Index is higher than size"); + } + + // save a copy of the value to be removed so we can return it later + int copyOfRemovedValue = buffer[index]; + + // shift values to the left + for (int i = index; i <= size - 1; i++) { + buffer[i] = buffer[i+1]; + } + + buffer[size - 1] = 0; + + // don't forget to decrement size + size--; + + return copyOfRemovedValue; + } + + /** + * 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 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) { + 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 size; + } + + /** + * Removes all the values from this list. + * The list will be empty after this call returns. + */ + @Override + public void clear() { + buffer = new int[10]; + size = 0; + } + + private void resize(int newSize) { + // create new space, separate from the old space (buffer) + int[] newBuffer = new int[newSize]; + + // copy everything over from buffer into newBuffer + for (int i = 0; i < buffer.length; i++) { + newBuffer[i] = buffer[i]; + } + + // set the new space into buffer + buffer = newBuffer; + + // the old buffer 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() { + return new IntListIterator(); + } + + // create a private helper Iterator class + private class IntListIterator implements Iterator { + // private fields: + private int i; + + private 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/DoublyLinkedIntList.java b/src/DoublyLinkedIntList.java new file mode 100644 index 0000000..20e276c --- /dev/null +++ b/src/DoublyLinkedIntList.java @@ -0,0 +1,124 @@ +import java.util.Iterator; + +public class DoublyLinkedIntList implements IntList { + // private fields + + private class Node { + int data; + Node next; // address of the node "after" this one in line + Node prev; // address of the node "before" this one in line + + public Node() { + next = null; + prev = null; + } + } + + private Node pre; + private Node post; + private int size; + + // constructor + public DoublyLinkedIntList() { + // an empty list has 2 sentinel (dummy) nodes that serve as bookends + pre = new Node(); + post = new Node(); + pre.next = post; + post.prev = pre; + size = 0; + } + + + @Override + public void addFront(int value) { + + } + + @Override + public void addBack(int value) { + Node theLastOne = post.prev; + + // set up my new node and fill it out (data, prev, next) + Node theNewOne = new Node(); + theNewOne.data = value; + theNewOne.next = post; + theNewOne.prev = theLastOne; + + // go to the end of the list's sentinel, and update it's prev + post.prev = theNewOne; + + // go to the node before the new one, and update it's next + theLastOne.next = theNewOne; + + size++; + + } + + @Override + public void add(int index, int value) { + + } + + @Override + public void removeFront() { + + } + + @Override + public void removeBack() { + if (size > 0) { + // set up a temp variable for convenience + Node theOneToRemove = post.prev; + + theOneToRemove.prev.next = post; + post.prev = theOneToRemove.prev; + + // optional, but strongly recommended to clean up + theOneToRemove.next = null; + theOneToRemove.prev = null; + theOneToRemove.data = 0; + + size--; + } + } + + @Override + public int remove(int index) { + return 0; + } + + @Override + public int get(int index) { + return 0; + } + + @Override + public boolean contains(int value) { + return false; + } + + @Override + public int indexOf(int value) { + return 0; + } + + @Override + public boolean isEmpty() { + return false; + } + + @Override + public int size() { + return 0; + } + + @Override + public void clear() { + + } + + @Override + public Iterator iterator() { + return null; + } +} diff --git a/src/LinkedIntList.java b/src/LinkedIntList.java new file mode 100644 index 0000000..f057a4a --- /dev/null +++ b/src/LinkedIntList.java @@ -0,0 +1,209 @@ +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class LinkedIntList implements IntList { + + // define what a node is + 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 to initialize the fields + 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) { + // set up a new node + Node theNewOne = new Node(); + + if (head == null) { + // the list is currently empty + head = theNewOne; + size++; + } + else { + // the list currently has some nodes in it + theNewOne.next = head; + head = theNewOne; + } + } + + /** + * 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) { + + } + + /** + * 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() { + + } + + /** + * 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() { + SinglyLinkedIterator theIterator = new SinglyLinkedIterator(); + return theIterator; + } + + // helper class/type that defines how the iterator works + private class SinglyLinkedIterator implements Iterator { + + // private int i; // the index of the item I am on in the arraylist + private Node current; + + public SinglyLinkedIterator() { + current = head; + } + + @Override + public boolean hasNext() { + return current != null; + } + + @Override + public Integer next() { + if (current == null) { + throw new NoSuchElementException("There is no next one to go to!!"); + } + + int item = current.data; + current = current.next; + return item; + } + } + +} diff --git a/src/Main.java b/src/Main.java index 930198c..9b797ee 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,3 +1,5 @@ +import java.util.Iterator; + //TIP To Run code, press or // click the icon in the gutter. public class Main { @@ -6,10 +8,28 @@ public static void main(String[] args) { // to see how IntelliJ IDEA suggests fixing it. System.out.printf("Hello and welcome!"); - 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); + IntList firstList; + + ArrayIntList secondList = new ArrayIntList(); + + IntList thirdList = new ArrayIntList(); + thirdList.addFront(15); + thirdList.addFront(12); + thirdList.addBack(8); + + // 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); + } + + } + } \ No newline at end of file