diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/Main.java b/src/Main.java index 3e59c38..57c0842 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,58 @@ +import edu.greenriver.sdev333.*; + +import java.sql.SQLOutput; +import java.util.Iterator; + public class Main { public static void main(String[] args) { System.out.println("Hello world!"); + List friends = new ArrayList(); + System.out.println("initial size is " + friends.size()); + + friends.add("Jose"); + friends.add("Jesse"); + friends.add("Jesus"); + friends.add("Derek"); + friends.add("Stephen"); + friends.add("Binal"); + friends.add("Simon"); + friends.add("Herb"); + friends.add("Roman"); + friends.add("Kye"); + friends.add("Matt"); + friends.add("Cullen"); + + System.out.println("size is now" + friends.size()); + +// for (int i = 0; i < friends.size(); i++) { +// System.out.println(friends.get(i)); +// +// } + Iterator itr = friends.iterator(); + while(itr.hasNext()){ + String name = itr.next(); + System.out.println(name); + } + + for(String name : friends) { + System.out.println(name); + } + + System.out.println("the size of my friends arraylist right now is " + + friends.size() + ", " + "and the " + friends.isEmpty() + " means its not empty."); + System.out.println(friends.contains("Jesse") + " ,my friend Jesse is in the array"); + System.out.println("the index of my friend jesse in the array is " + friends.indexOf("Jesse")); + friends.add("Todd"); + friends.add(3, "Jack"); + friends.remove(0); + System.out.println("my friend is " + friends.get(6) + " at index 6."); + friends.set(0, "Jesse"); + System.out.println("Now my friend at the top spot index 0 is " + friends.get(0)); + System.out.println("Is Todd in my Array true or false?... " + friends.contains("Todd")); + //friends.clear(); + + + + } } \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java new file mode 100644 index 0000000..9227685 --- /dev/null +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -0,0 +1,391 @@ +//created package in local directory so need to bring it in by using keyword package +package edu.greenriver.sdev333; + +//imports so we can use the methods from like .hasNext and .next +import java.util.Iterator; +import java.util.ListIterator; + +public class ArrayList implements List{ + //NEED FIELDS? YES + //1 plain private common java array + private ItemType[] data; + + //1 int to keep track of size, the number of spots in data array + private int size; + + //constructor set initial size to 0 and set the array data to hold 10 items + public ArrayList() { + size = 0; + data = (ItemType[]) new Object[10]; + } + + // + /** + * Returns the number of items in this collection. + * + * @return the number of items in this collection + */ + @Override + public int size() { + return size; + } + + /** + * Returns true if this collection contains no items. + * + * @return true if this collection contains no items + */ + //if the size of my array is 0 then its empty so give me back true and if not false + //or just give me back 0 if size is 0 + @Override + public boolean isEmpty() { + return size == 0; + } + + /** + * Returns true if this collection contains the specified item. + * + * @param item items whose presence in this collection is to be tested + * @return true if this collection contains the specified item + * @throws NullPointerException if the specified item is null + * and this collection does not permit null items + */ + @Override + public boolean contains(ItemType item) { + int i = indexOf(item); + if(i != -1) { + return true; + } + return false; + } + + /** + * Returns an iterator over the elements in this collection. + * + * @return an Iterator over the elements in this collection + */ + @Override + public Iterator iterator() { + return new OurCustomIterator(); + } + + + + private void checkSize(){ + if(size == data.length){ + //resize up(double up to the array size) step 1 create larger array a temporary array to copy to + ItemType[] temp = (ItemType[]) new Object[size * 2]; + ///step 2 iterate through, copy items from first array to the second, from data to temp + for (int i = 0; i < size; i++) { + temp[i] = data[i]; + } + //step 3 repoint data to the new array + data = temp; + //now temp is disconnected /optional. + temp = null; + }// end of if + } + + /** + * Adds the specified item to the collection. + * + * @param item item to be added to the collection + * @throws NullPointerException if the specified item is null + * and this collection does not permit null items + */ + @Override + public void add(ItemType item) { + //code above works until size = the length, no room left. + //do check first to not get error then update size + checkSize(); + data[size] = item; + size++; + } + + /** + * Removes a single instance of the specified item from this collection, + * if it is present. + * + * @param item item to be removed from this collection, if present + * @throws NullPointerException if the specified item is null + * and this collection does not permit null items + */ + @Override + public void remove(ItemType item) { + int i = indexOf(item); + if(i !=-1) { + remove(i); + } + } + + /** + * Removes all items from this collection. + * The collection will be empty after this method returns. + */ + @Override + public void clear() { + //lazy deleting array items + size = 0; + } + + /** + * Returns true if this collection contains all the items + * in the specified other collection. + * + * @param otherCollection collection to be checked for containment in this collection + * @return true if this collection contains all the items + * in the specified other collection + */ + @Override + public boolean containsAll(Collection otherCollection) { + Iterator itr = (Iterator)otherCollection.iterator(); + while (itr.hasNext()) { + ItemType itemToCheck = itr.next(); + if(!contains(itemToCheck)) { + return false; + } + } + return true; + } + + /** + * Adds all the items in this specified other collection to this collection. + * + * @param otherCollection collection containing items to be added to this collection + */ + @Override + public void addAll(Collection otherCollection) { + throw new UnsupportedOperationException("Not going to work"); + + } + + /** + * Removes all of this collection's items that are also contained in the + * specified other collection. After this call returns, this collection will + * contain no elements in common with the specified other collection. + * + * @param otherCollection collection containing elements to be removed + * from this collection + */ + @Override + public void removeAll(Collection otherCollection) { + + } + + /** + * Retains only the items in this collection that are contained in the + * specified other collection. In other words, removes from this collection + * all of its items that are not contained in the specified other collection + * + * @param otherCollection collection containing elements to be retained in + * this collection + */ + @Override + public void retainAll(Collection otherCollection) { + + } + + /** + * Returns the item at the specified position in this list + * + * @param index index of the item to return + * @return the item at the specified position in this list + * @throws IndexOutOfBoundsException if this index is out of range + * (index < 0 || index >= size()) + */ + @Override + public ItemType get(int index) { + if(index >= size){ + throw new IndexOutOfBoundsException("index is beyond size"); + } + return data[index]; + } + + /** + * Replaces the item at the specified position in this list + * with the specified item + * + * @param index index of the item to replace + * @param item item to be stored at the specified position + * @throws NullPointerException if the specified item is null + * and this list does not permit null elements + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index >= size()) + */ + @Override + public void set(int index, ItemType item) { + if (index > size){ + throw new IndexOutOfBoundsException("index is beyond the size"); + } + data[index] = item; + } + + /** + * Inserts the specified item at the specified position in this list. + * Shifts the item currently at that position (if any) and any subsequent + * items to the right. + * + * @param index index at which the specified item is to be inserted + * @param item item to be inserted + * @throws NullPointerException if the specified item is null + * and this list does not permit null elements + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index >= size()) + */ + @Override + //adding item at specific index without overwriting the current item at the index + public void add(int index, ItemType item) { + //error checking method created already and now invoked + checkSize(); + for (int i = size; i >= index + 1; i--) { + data[i] = data[i - 1]; + } + + data[index] = item; + size++; + + } + + /** + * Removes the element at the specified position in this list. + * Shifts any subsequent items to the left. + * + * @param index the index of the item to be removed + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index >= size()) + */ + @Override + public void remove(int index) { + //shift values left to overwrite the item at index + for (int i = index; i < size - 1; i++) { + data[i] = data[i + 1]; + } + size--; + } + + /** + * Returns the index of the first occurrence of the specified item + * in this list, or -1 if this list does not contain the item. + * + * @param item the item to search for + * @return the index of the first occurrence of the specified item + * in this list, or -1 if this list does not contain the item + * @throws NullPointerException if the specified item is null and this + * list does not permit null items + */ + + //gives the index # of the item in arraylist given by the user + @Override + public int indexOf(ItemType item) { + for (int i = 0; i < size; i++) { + if(item.equals(data[i])) { + return i; + } + } + // if -1 returned then item is not in the array + return -1; + } + + /** + * Returns the index of the last occurrence of the specified item + * in this list, or -1 if this list does not contain the item. + * + * @param item the item to search for + * @return the index of the first occurrence of the specified item + * in this list, or -1 if this list does not contain the item + * @throws NullPointerException if the specified item is null and this + * list does not permit null items + */ + @Override + public int lastIndexOf(ItemType item) { + return 0; + } + + /** + * Returns a list iterator over the elements in this list + * (in proper sequence). + * + * @return a list iterator over the elements in this list + * (in proper sequence) + */ + @Override + public ListIterator listIterator() { + return null; + } + + //Making class inside another class so not making a new file + private class OurCustomIterator implements Iterator { + //fields only 1 below + private int currentPosition; + //need constructor + public OurCustomIterator() { + currentPosition = 0; + } + + @Override + public boolean hasNext() { + return currentPosition < size(); + } + + @Override + public ItemType next() { + ItemType result = get(currentPosition); + currentPosition++; + return result; + } + } + + private class SecondCustomIterator implements ListIterator { + //fancier Iterator - allows us to go forwards and back + private int currentPosition; + public SecondCustomIterator() { + currentPosition =0; + } + + @Override + public boolean hasNext() { + return false; + } + + @Override + public ItemType next() { + return null; + } + + @Override + public boolean hasPrevious() { + return currentPosition > 0; + } + + @Override + public ItemType previous() { + return null; + } + + @Override + public int nextIndex() { + return 0; + } + + @Override + public int previousIndex() { + return 0; + } + + @Override + public void remove() { + + } + + @Override + public void set(ItemType itemType) { + + } + + @Override + public void add(ItemType itemType) { + + } + } + +}// end of class ArrayList diff --git a/src/edu/greenriver/sdev333/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java new file mode 100644 index 0000000..bc73dc5 --- /dev/null +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -0,0 +1,562 @@ +package edu.greenriver.sdev333; + +import java.util.Iterator; +import java.util.ListIterator; + +public class SinglyLinkedList implements List { + + //FIELDS -- what does a linked list actually have in it? + private Node head; + //size tracker + private int size = 0; + + //helper//inner classes + private class Node { + ItemType data; + Node Next; + } + //constructor + public SinglyLinkedList() { + //an empty list has no nodes, so it has no head so set to null + head = null; + //could also put the variable size here set to 0; + size = 0; + } + + // + /** + * Returns the number of items in this collection. + * + * @return the number of items in this collection + */ + @Override + public int size() { + int count = 0; + Node current = head; //assuming head is the first node of the list + while (current != null) { + count++; + current = current.Next; + } + return count; + } + + + /** + * Returns true if this collection contains no items. + * + * @return true if this collection contains no items + */ + @Override + public boolean isEmpty() { + return head == null; + } + + + /** + * Returns true if this collection contains the specified item. + * + * @param item items whose presence in this collection is to be tested + * @return true if this collection contains the specified item + * @throws NullPointerException if the specified item is null + * and this collection does not permit null items + */ + @Override + public boolean contains(ItemType item) { + int position = indexOf(item); + if (position == -1) { + return false; + } + return true; + } + + /** + * Returns an iterator over the elements in this collection. + * + * @return an Iterator over the elements in this collection + */ + @Override + public Iterator iterator() { + return new OurCustomIterator(); + } + + /** + * Adds the specified item to the collection. + * + * @param item item to be added to the collection + * @throws NullPointerException if the specified item is null + * and this collection does not permit null items + */ + //add item at end + @Override + public void add(ItemType item) { + Node theNewNode = new Node(); + theNewNode.data = item; + theNewNode.Next = null; + if (head == null) { + head = theNewNode; + } else { + Node current = head; + while (current.Next != null) { + current = current.Next; + } + current.Next = theNewNode; + } + size++; + } + + + /** + * Removes a single instance of the specified item from this collection, + * if it is present. + * + * @param item item to be removed from this collection, if present + * @throws NullPointerException if the specified item is null + * and this collection does not permit null items + */ + @Override + public void remove(ItemType item) { + if (item == null) { + throw new NullPointerException(); + } + + if (head.data == item) { + head = head.Next; + size--; + } + else { + Node current = head; + Node previous; + while (current.Next != null) { + previous = current; + current = current.Next; + + if (current.data.equals(item)) { + previous.Next = current.Next; + size--; + } + } + } + + } + @Override + public void clear() { + head = null; + size = 0; + } + + /** + * Returns true if this collection contains all the items + * in the specified other collection. + * + * @param otherCollection collection to be checked for containment in this collection + * @return true if this collection contains all the items + * in the specified other collection + */ + @Override + public boolean containsAll(Collection otherCollection) { + for( ItemType item : otherCollection) { + if (!this.contains(item)) { + return false; + } + return true; + } + + /** + * Adds all the items in this specified other collection to this collection. + * + * @param otherCollection collection containing items to be added to this collection + */ + + public void addAll(Collection otherCollection) { + // walk through the other collection + // for-each loop + // or use Iterator + + Iterator itr = (Iterator)otherCollection.iterator(); + while (itr.hasNext()) { + ItemType currentItem = itr.next(); + add(currentItem); + } + + + /** + * Removes all of this collection's items that are also contained in the + * specified other collection. After this call returns, this collection will + * contain no elements in common with the specified other collection. + * + * @param otherCollection collection containing elements to be removed + * from this collection + */ + @Override + public void removeAll(Collection otherCollection) { + + } + + /** + * Retains only the items in this collection that are contained in the + * specified other collection. In other words, removes from this collection + * all of its items that are not contained in the specified other collection + * + * @param otherCollection collection containing elements to be retained in + * this collection + */ + @Override + public void retainAll(Collection otherCollection) { + + } + + /** + * Returns the item at the specified position in this list + * + * @param index index of the item to return + * @return the item at the specified position in this list + * @throws IndexOutOfBoundsException if this index is out of range + * (index < 0 || index >= size()) + */ + @Override + public ItemType get(int index) { + if (index <0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + + Node current = head; + int counter =0; + while (counter != index) { + current = current.next; + counter++; + } + return current.data; + } + + /** + * Replaces the item at the specified position in this list + * with the specified item + * + * @param index index of the item to replace + * @param item item to be stored at the specified position + * @throws NullPointerException if the specified item is null + * and this list does not permit null elements + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index >= size()) + */ + @Override + public void set(int index, ItemType item) { + if (item == null) { + throw new NullPointerException(); + } + + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + + Node current = head; + int counter = 0; + while (counter != index) { + current = current.Next; + counter++; + } + + current.data = item; + } + + + /** + * Inserts the specified item at the specified position in this list. + * Shifts the item currently at that position (if any) and any subsequent + * items to the right. + * + * @param index index at which the specified item is to be inserted + * @param item item to be inserted + * @throws NullPointerException if the specified item is null + * and this list does not permit null elements + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index >= size()) + */ + @Override + public void add(int index, ItemType item) { + checkIndex(index); + + if (index == 0) { + // if someone wants to add at the beginning, I need to change the head + Node theNewOne = new Node(); + theNewOne.data = item; + theNewOne.next = head; + head = theNewOne; + } + else { + Node current = head; + + // stop one before the position I want to insert at + for (int i = 0; i < index - 1; i++) { + current = current.next; + } + + // when I get here, current is pointing the node *BEFORE* the node at the index + Node theNewOne = new Node(); + theNewOne.data = item; + theNewOne.next = current.next; + + current.next = theNewOne; + } + + size++; + } + + /** + * Removes the element at the specified position in this list. + * Shifts any subsequent items to the left. + * + * @param index the index of the item to be removed + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index >= size()) + */ + + private void checkIndex(int index) { + if(index < 0 || index >= size) } + throw new IndexOutOfBoundsException(); + } + + /** + * Adds all the items in this specified other collection to this collection. + * + * @param otherCollection collection containing items to be added to this collection + */ + @Override + public void addAll(Collection otherCollection) { + + } + + /** + * Removes all of this collection's items that are also contained in the + * specified other collection. After this call returns, this collection will + * contain no elements in common with the specified other collection. + * + * @param otherCollection collection containing elements to be removed + * from this collection + */ + @Override + public void removeAll(Collection otherCollection) { + + } + + /** + * Retains only the items in this collection that are contained in the + * specified other collection. In other words, removes from this collection + * all of its items that are not contained in the specified other collection + * + * @param otherCollection collection containing elements to be retained in + * this collection + */ + @Override + public void retainAll(Collection otherCollection) { + + } + + /** + * Returns the item at the specified position in this list + * + * @param index index of the item to return + * @return the item at the specified position in this list + * @throws IndexOutOfBoundsException if this index is out of range + * (index < 0 || index >= size()) + */ + @Override + public ItemType get(int index) { + return null; + } + + /** + * Replaces the item at the specified position in this list + * with the specified item + * + * @param index index of the item to replace + * @param item item to be stored at the specified position + * @throws NullPointerException if the specified item is null + * and this list does not permit null elements + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index >= size()) + */ + @Override + public void set(int index, ItemType item) { + + } + + /** + * Inserts the specified item at the specified position in this list. + * Shifts the item currently at that position (if any) and any subsequent + * items to the right. + * + * @param index index at which the specified item is to be inserted + * @param item item to be inserted + * @throws NullPointerException if the specified item is null + * and this list does not permit null elements + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index >= size()) + */ + @Override + public void add(int index, ItemType item) { + + } + + @Override + public void remove(int index) { + checkIndex(index); + + if (index == 0) { + head = head.next; + } + else { + Node current = head; + for (int i = 0; i < index - 1; i++) { + current = current.next; + } + // when I get here - current is pointing to the node BEFORE the one at index + + current.next = current.next.next; + } + + size--; + + } + + + /** + * Returns the index of the first occurrence of the specified item + * in this list, or -1 if this list does not contain the item. + * + * @param item the item to search for + * @return the index of the first occurrence of the specified item + * in this list, or -1 if this list does not contain the item + * @throws NullPointerException if the specified item is null and this + * list does not permit null items + */ + @Override + @Override + public int indexOf(ItemType item) { + int counter = 0; + Node current = head; + while (current != null) { + if (current.data.equals(item)) { + return counter; + } + counter++; + current = current.next; + } + + // if we get here, it's not found + return -1; + } + + /** + * Returns the index of the last occurrence of the specified item + * in this list, or -1 if this list does not contain the item. + * + * @param item the item to search for + * @return the index of the first occurrence of the specified item + * in this list, or -1 if this list does not contain the item + * @throws NullPointerException if the specified item is null and this + * list does not permit null items + */ + @Override + public int lastIndexOf(ItemType item) { + return 0; + } + + /** + * Returns a list iterator over the elements in this list + * (in proper sequence). + * + * @return a list iterator over the elements in this list + * (in proper sequence) + */ + @Override + public ListIterator listIterator() { + return new OurEnhancedIterator(); + } + + private class OurCustomIterator implements Iterator { + + // field + private Node currentPosition; + + public OurCustomIterator() { + currentPosition = head; + } + + @Override + public boolean hasNext() { + // see if I'm on the last node: if (current.next == null) + // see if I made it past the last node: if (current == null) + if (currentPosition != null) { + return true; + } + return false; + } + + @Override + public ItemType next() { + ItemType result = currentPosition.data; + currentPosition = currentPosition.next; + return result; + } + } + + private class OurEnhancedIterator implements ListIterator { + + private Node currentPosition; + private int currentIndex; + + public OurEnhancedIterator() { + currentPosition = head; + currentIndex = 0; + } + + @Override + public boolean hasNext() { + return currentPosition != null; + } + + @Override + public ItemType next() { + ItemType result = currentPosition.data; + currentPosition = currentPosition.next; + return result; + } + + @Override + public boolean hasPrevious() { + return false; + } + + @Override + public ItemType previous() { + return null; + } + + @Override + public int nextIndex() { + return 0; + } + + @Override + public int previousIndex() { + return 0; + } + + @Override + public void remove() { + + } + + @Override + public void set(ItemType itemType) { + + } + + @Override + public void add(ItemType itemType) { + + } + } + +} diff --git a/src/edu/greenriver/sdev333/SinglyListMain.java b/src/edu/greenriver/sdev333/SinglyListMain.java new file mode 100644 index 0000000..4e3030c --- /dev/null +++ b/src/edu/greenriver/sdev333/SinglyListMain.java @@ -0,0 +1,47 @@ +package edu.greenriver.sdev333; + +public class SinglyListMain { + public static void main(String[] args) { + // create a new LinkedList + SinglyLinkedList list = new SinglyLinkedList<>(); + + // check if the list is empty + System.out.println("Is the list empty? " + list.isEmpty()); + + // add some items to the list + list.add("Jesse"); + list.add("Adam"); + list.add("Jimmy"); + + // print the size of the list + System.out.println("The size of the list is: " + list.size()); + + // check if the list contains a specific item + System.out.println("Does the list contain 'banana'? " + list.contains("banana")); + + // get the index of a specific item in the list + System.out.println("The index of 'cherry' in the list is: " + list.indexOf("cherry")); + + // remove an item from the list + list.remove(1); + + // print the updated list + System.out.println("The updated list is: " + list); + + // get an item from the list + String item = list.get(0); + System.out.println("The item at index 0 is: " + item); + + // set an item in the list + list.set(1, "date"); + + // print the updated list + System.out.println("The updated list is: " + list); + + // clear the list + list.clear(); + + // print the size of the list after clearing it + System.out.println("The size of the list after clearing it is: " + list.size()); + } +}