diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..002da1d --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +Main.java \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..6e97bd9 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/src/Main.java b/src/Main.java deleted file mode 100644 index 3e59c38..0000000 --- a/src/Main.java +++ /dev/null @@ -1,5 +0,0 @@ -public class Main { - public static void main(String[] args) { - System.out.println("Hello world!"); - } -} \ 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..447c996 --- /dev/null +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -0,0 +1,274 @@ +package edu.greenriver.sdev333; +import java.util.Iterator; +import java.util.ListIterator; + +public class ArrayList implements List{ + //one plain old Java array + private ItemType[] data; + + //one int to keep track of size + // ...size is the # of index being used in the data array + // size != length + + private int size; + + public ArrayList() { + size = 0; + data = (ItemType[]) new Object[10]; + } + public ArrayList(int capacity) { + size = 0; + data = (ItemType[]) new Object[capacity]; + } + + @Override + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + /* + Or + if(size == 0) { + return true + } return false + */ + } + + @Override + public boolean contains(ItemType item) { + int i = indexOf(item); + if (i != -1) { + return true; + } + return false; + } + + @Override + public Iterator iterator() { + return new OurCustomIterator(); + } + + private void checkSize() { + if(size == data.length) { + // need to double up array size + // Step 1 - create a new larger array + ItemType[] temp = (ItemType[]) new Object[size * 2]; + + // Step 2 - copy items from data to temp + for(int i = 0; i < size; i++) { + temp[i] = data[i]; + } + + // Step 3 - repoint/reference data to the point to new array + data = temp; + + // optional + temp = null; + } // end of if + + } + @Override + public void add(ItemType item) { + checkSize(); + data[size] = item; + size++; + } // end of method + + @Override + public void remove(ItemType item) { + int i = indexOf(item); + if(i != -1) { + // if within index use other remove method to do work + remove(i); + } + } + + @Override + public void clear() { + // lazy deletion + size = 0; + } + + @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; + +// for (ItemType itemToCheck : otherCollection) { +// if(!contains(itemToCheck)) { +// return false; +// } +// } return true; this way is shorter + + //throw new UnsupportedOperationException("Not Implemented"); // fail fast , fail loud + //return false; + + } + + @Override + public void addAll(Collection otherCollection) { + throw new UnsupportedOperationException("No. t gonna do it!"); + } + + @Override + public void removeAll(Collection otherCollection) { + + } + + @Override + public void retainAll(Collection otherCollection) { + + } + + @Override + public ItemType get(int index) { + if(index >= size || index < 0) { + throw new IndexOutOfBoundsException("Index is out of bound"); + } + return data[index]; + } + + @Override + public void set(int index, ItemType item) { + if(index >= size || index < 0) { + throw new IndexOutOfBoundsException("Index is out of bound"); + } + data[index] = item; + } + + @Override + public void add(int index, ItemType item) { + if(item.equals(null)) { + throw new NullPointerException(); + } else if (index >= size || index < 0) { + throw new IndexOutOfBoundsException(); + } + + checkSize(); + for (int i = size; i > index; i--) { + data[i] = data[i-1]; + } + data[index] = item; + 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--; + } + + @Override + public int indexOf(ItemType item) { + + for (int i = 0; i < size; i++) { + if(item.equals(data[i])) { + return i; + } + } return -1; // if not then return -1 + } + + @Override + public int lastIndexOf(ItemType item) { + for (int i = size ; i >= 0; i--) { + if(item.equals(data[i])) { + return i; + } + } return - 1; + } + @Override + public ListIterator listIterator() { + + return null; + } + + private class OurCustomIterator implements Iterator { + // fields + private int currentPosition; + + public OurCustomIterator() { + currentPosition = 0; + } + @Override + public boolean hasNext() { + return currentPosition < size(); + } + + @Override + public ItemType next() { + ItemType result = get(currentPosition); + currentPosition++; + return result; + } + } // end of OurCustomIterator class + + private class SecondCustomIterator implements ListIterator { + // fancier Iterator-lets us go forwards and backwards + private int currentPosition; + + public SecondCustomIterator() { + currentPosition = 0; + } + @Override + public boolean hasNext() { + return false; + } + + @Override + public ItemType next() { + return null; + } + + @Override + public boolean hasPrevious() { + // hasNext checked currentPosition with size + // hasPrevious check currentPosition against 0 + 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) { + + } + + } +}// end of ArrayList class diff --git a/src/edu/greenriver/sdev333/DoublyLinkedList.java b/src/edu/greenriver/sdev333/DoublyLinkedList.java new file mode 100644 index 0000000..409e8ea --- /dev/null +++ b/src/edu/greenriver/sdev333/DoublyLinkedList.java @@ -0,0 +1,255 @@ +package edu.greenriver.sdev333; + +import java.util.Iterator; +import java.util.ListIterator; + +public class DoublyLinkedList implements List { + // FIELDS - what does a linked list actually have in it?? + private Node head; + private int size; + + // helper/inner classes + private class Node { + ItemType data; + Node next; + Node previous; + } + + /** + * Constructor + */ + public DoublyLinkedList() { + // an empty list has no nodes, which means it has no head, + // so set head to null + head = null; + size = 0; + } + /** + * Returns the number of items in this collection. + * + * @return the number of items in this collection + */ + @Override + public int size() { + return 0; + } + + /** + * Returns true if this collection contains no items. + * + * @return true if this collection contains no items + */ + @Override + public boolean isEmpty() { + return false; + } + + /** + * 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) { + 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 null; + } + + /** + * 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) { + + } + + /** + * 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) { + + } + + /** + * Removes all items from this collection. + * The collection will be empty after this method returns. + */ + @Override + public void clear() { + + } + + /** + * 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) { + return false; + } + + /** + * 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) { + + } + + /** + * 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) { + + } + + /** + * 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 + public int indexOf(ItemType item) { + return 0; + } + + /** + * 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; + } + + +} diff --git a/src/edu/greenriver/sdev333/Main.java b/src/edu/greenriver/sdev333/Main.java new file mode 100644 index 0000000..cc56e1b --- /dev/null +++ b/src/edu/greenriver/sdev333/Main.java @@ -0,0 +1,65 @@ +package edu.greenriver.sdev333; + +import edu.greenriver.sdev333.*; +import java.util.Iterator; +import java.util.ListIterator; + +public class Main { + public static void main(String[] args) { + + System.out.println("Hello world!"); + + List friends = new RecursiveLinkedList(); + + System.out.println("initial size is " + friends.size()); + + friends.add("Jess"); + friends.add("Tina"); + friends.add("Josh"); + friends.add("Susan"); + friends.add("Tyler"); + friends.add("Usman"); + friends.add("Dee"); + friends.add("Rose"); + friends.add("Blanche"); + friends.add("Dorothy"); + friends.add("Sophia"); + friends.add(2, "Wednesday"); + System.out.println("size is now " + friends.size()); + + //for (int i = 0; i < friends.size(); i++) { + // System.out.println(friends.get(i)); + //} + + // above: import java.util.Iterator; + /* + Iterator itr = friends.iterator(); + while (itr.hasNext()) { + String name = itr.next(); + System.out.println(name); + } + for (String name : friends) { + System.out.println(name); + } + ListIterator fancyItr = friends.listIterator(); + while (fancyItr.hasNext()) { + String name = fancyItr.next(); + System.out.println(name); + } + System.out.println(); + while (fancyItr.hasPrevious()) { + String name = fancyItr.previous(); + System.out.println(name); + } + */ + + + List hello = new SinglyLinkedList<>(); + hello.add("hello"); + hello.add("yo"); + hello.add("hi"); + System.out.println("This is my get " + hello.get(1)); + + + } +} \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/Node.java b/src/edu/greenriver/sdev333/Node.java new file mode 100644 index 0000000..1f9fea1 --- /dev/null +++ b/src/edu/greenriver/sdev333/Node.java @@ -0,0 +1,35 @@ +package edu.greenriver.sdev333; + +import javax.swing.*; + +public class Node { + int data; + Node next; + Node prev; + Node(int givenData) { + this.data = givenData; + } + + int countNodes(Node head) { + // assume that head != null + int count = 1; + Node current = head; + while(current.next != null) { + current = current.next; + count += 1; + } + return count; + } + public static void main(String[] args) { + Node nodeA = new Node(6); + Node nodeB = new Node(3); + Node nodeC = new Node(4); + Node nodeD = new Node(2); + Node nodeE = new Node(1); + + nodeA.next = nodeB; + nodeB.next = nodeC; + nodeC.next = nodeD; + nodeD.next = nodeE; + } +} diff --git a/src/edu/greenriver/sdev333/RecursiveLinkedList.java b/src/edu/greenriver/sdev333/RecursiveLinkedList.java new file mode 100644 index 0000000..4f24ba7 --- /dev/null +++ b/src/edu/greenriver/sdev333/RecursiveLinkedList.java @@ -0,0 +1,160 @@ +package edu.greenriver.sdev333; + +import java.util.Iterator; +import java.util.ListIterator; + +public class RecursiveLinkedList implements List { + // private helper class + private class Node { + ItemType data; + Node next; + } + + // fields + private Node head; + + public RecursiveLinkedList() { + head = null; + } + + @Override + public int size() { + return helpSize(head); + } + + // private helper method that does the actual recursive work + private int helpSize(Node theHead){ + if (theHead == null) { + // base case (the bottom / the end) + return 0; + } + else { + // VV "the rest" + return helpSize(theHead.next) + 1; + } + } + + @Override + public boolean isEmpty() { + return false; + } + + @Override + public boolean contains(ItemType item) { + return false; + } + + @Override + public Iterator iterator() { + return null; + } + + @Override + public void add(ItemType item) { + // adds an item to back + // loopy way: + // - created a current variable, set it to be same value as head + // - wrote a loop while (current.next != null) { current = current.next; } + // - once we are at the last node, create new node and attach it + head = helpAdd(head, item); + } + + private Node helpAdd(Node theHead, ItemType item) { + if (theHead == null) { + // hit bottom (at the end) + Node theNewNode = new Node(); + theNewNode.data = item; + theNewNode.next = null; + return theNewNode; + } + else { + // not at end/bottom + theHead.next = helpAdd(theHead.next, item); + return theHead; + } + } + + @Override + public void remove(ItemType item) { + + } + + @Override + public void clear() { + + } + + @Override + public boolean containsAll(Collection otherCollection) { + return false; + } + + @Override + public void addAll(Collection otherCollection) { + + } + + @Override + public void removeAll(Collection otherCollection) { + + } + + @Override + public void retainAll(Collection otherCollection) { + + } + + @Override + public ItemType get(int index) { + return null; + } + + @Override + public void set(int index, ItemType item) { + + } + + @Override + public void add(int index, ItemType item) { + // add(0, item) + if (index == 0) { + // add to the front - list may or may not be empty + Node theNewOne = new Node(); + theNewOne.data = item; + theNewOne.next = head; + head = theNewOne; + } + else { + // anywhere else other than 0 (front) + Node current = head; + for (int i = 0; i < index - 1; i++) { + current = current.next; + } + + Node theNewOne = new Node(); + theNewOne.data = item; + theNewOne.next = current.next; + current.next = theNewOne; + } + } + + @Override + public void remove(int index) { + + } + + @Override + public int indexOf(ItemType item) { + return 0; + } + + @Override + public int lastIndexOf(ItemType item) { + return 0; + } + + @Override + public ListIterator listIterator() { + return null; + } +} \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java new file mode 100644 index 0000000..9fabdd4 --- /dev/null +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -0,0 +1,506 @@ +package edu.greenriver.sdev333; + +import javax.naming.OperationNotSupportedException; +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; + private int size; + + // helper/inner classes + private class Node { + ItemType data; + Node next; + } + + /** + * Constructor + */ + public SinglyLinkedList() { + // an empty list has no nodes, + // which means it has no head, so set head to null + head = null; + size = 0; + } + + /** + * 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 + */ + @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) { + // assume indexOf is working... + 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 + */ + @Override + public void add(ItemType item) { + if (item == null) { + throw new NullPointerException(); + } + // the index at the end of the list is size - 1 + // example: if list is size 5, last index is 4 + // so we can just insert at the last index + add(size() - 1, item); + } + + /** + * 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(); + } + + // alternative - easier to write, but less efficient + /* + int position = indexOf(item); + if (position != -1) { + remove(position); + } + */ + + 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--; + } + } + } + + } + + /** + * Removes all items from this collection. + * The collection will be empty after this method returns. + */ + @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; + + /* + Iterator itr = (Iterator)otherCollection.iterator(); + while (itr.hasNext()) { + ItemType item = itr.next(); + if (!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 + */ + @Override + 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); + } + + /* + // alternative implementation using a for-each loop + for (ItemType currentItem : otherCollection) { + 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) { + checkIndex(index); + + Node current = head; + int counter = 0; + while (counter != index) { + current = current.next; + counter++; + } + return current.data; + + /*for (int i = 0; i < index; i++) { + current = current.next; + }*/ + } + + /** + * 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) { + checkIndex(index); + + get(index).equals(item); + /* + checkElementIndex(index); + Node x = node(index); + E oldVal = x.item; + x.item = element; + return oldVal; + + get index method + Node current = head; + int counter = 0; + while (counter != index) { + current = current.next; + counter++; + } + return current.data; + */ + + } + + + // check index out of bound method + private void checkIndex(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + } + + /** + * 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()) + */ + @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 + 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 (cLurrent == 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) { + + } + } + +} \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/learningLinkedList.java b/src/edu/greenriver/sdev333/learningLinkedList.java new file mode 100644 index 0000000..4abb419 --- /dev/null +++ b/src/edu/greenriver/sdev333/learningLinkedList.java @@ -0,0 +1,34 @@ +package edu.greenriver.sdev333; + +import java.util.LinkedList; + +public class learningLinkedList { + public static void main(String[] args) { + LinkedList list = new LinkedList<>(); + /* + list.push("A"); + list.push("B"); + list.push("C"); + list.push("D"); + list.push("F"); + list.pop(); + */ + + list.offer("A"); + list.offer("B"); + list.offer("C"); + list.offer("D"); + list.offer("F"); + list.poll(); + list.add(4,"E"); + list.remove("E"); + System.out.println(list.indexOf("B")); + list.addFirst("0"); + list.addLast("Z"); + System.out.println(list.peekFirst() + "---" +list.peekLast()); + System.out.println(list); + System.out.println(list.get(3)); + } + + +} diff --git a/src/edu/greenriver/sdev333/scratch.txt b/src/edu/greenriver/sdev333/scratch.txt new file mode 100644 index 0000000..3f50a84 --- /dev/null +++ b/src/edu/greenriver/sdev333/scratch.txt @@ -0,0 +1,19 @@ +//Constructor: LinkedList() +//public int size() +//public boolean isEmpty() +//public boolean contains(ItemType item) - hint: this method can use indexOf to do its work +//public int indexOf(ItemType item) +//public void add(ItemType item) +//public void add(int index, ItemType item) +//public void remove(int index) +//public ItemType get(int index) +//public void set(int index, ItemType item) +//public void clear() +public Iterator iterator() + +public void remove(ItemType item) - hint: this method can use indexOf, then remove(index) to do its work +**public int lastIndexOf(ItemType item) +**public void addAll(Collection otherCollection) +**public boolean equals(Object otherObject) +public ListIterator listIterator() +Move code to validate indexes into its own private method, something like: private boolean isValidIndex(int index) \ No newline at end of file diff --git a/src/map_client/LinkedMap.java b/src/map_client/LinkedMap.java new file mode 100644 index 0000000..f06d28c --- /dev/null +++ b/src/map_client/LinkedMap.java @@ -0,0 +1,50 @@ +package map_client; + +public class LinkedMap { + /* + // helper class + private class Node { + KeyType key; + ValueType value; + Node next; + } + + // fields + private Node head; + + public void put(KeyType theKey, ValueType theValue) { + // if theKey is in the map already? + Node current = head; + while (current != null) { + if(theKey.equals(current.key)) { + // I found it ! + current.value = theValue; + } + // move current fowar to the next node + current = current.next; + } + // if not, add it + // add a new node to represent the key-value pair + Node theNewNode = new Node(); + theNewNode.key = theKey; + theNewNode.value = theValue; + theNewNode.next = head; + head = theNewNode; + } + + public ValueType get(KeyTyoe theKey) { + Node current = head; + while (current != null) { + if (theKey.equals(current.key)) { + // i found the key + return current.value; + } + } + + // get to the end of the loop + // key not in the list + return null; + } + + */ +} diff --git a/src/map_client/main.java b/src/map_client/main.java new file mode 100644 index 0000000..e7c2954 --- /dev/null +++ b/src/map_client/main.java @@ -0,0 +1,88 @@ +package map_client; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.HashMap; +import java.util.Map; +import java.util.Scanner; +import java.util.TreeMap; + +public class main { + public static void main(String[] args) throws FileNotFoundException { + System.out.println("Hello"); + + // keys | Value + // domain names | IP address + // green.edu | 128..... + // zack.com | 128.. + + Map dnsTable = new TreeMap<>(); + // tree map, key comes out in order + // hash map, keys appears to be random + + // put a few items into map + dnsTable.put("greenriver.edu", "128.104.20.204"); + dnsTable.put("superbowl.net", "201.120.100.3"); + dnsTable.put("google.com", "101.101.101.101"); + + System.out.println(dnsTable); + + // get: ask for key, get value. Return null if cant find key. Only can get key + String answer = dnsTable.get("superbowl.net"); + System.out.println(answer); + + // change the value, update the value associated with the key + dnsTable.put("greenriver.edu", "1.2.3.4.5"); + System.out.println(dnsTable.get("greenriver.edu")); + + + Map wordCounts = new TreeMap<>(); + + Scanner scan = new Scanner(new File("src/notes.txt")); + + while (scan.hasNext()) { + String word = scan.next(); + int i = 1; + + // check to see if the word is in the map + if(wordCounts.containsKey(word)) { + + int value = wordCounts.get(word); // get current value + value++; // increment by one + wordCounts.put(word,value); // update key and return increment value + + // create hash set for max value + int maxValue = value; + + Map maxSet = new HashMap<>(); + if(wordCounts.containsValue(value)) { + maxSet.put(word,maxValue); + } + + } + // put key word and value in tree map + wordCounts.put(word, i); + } + + // write a loop to walk through (visit) items in the map + for (String key : wordCounts.keySet()) { + int value = wordCounts.get(key); + System.out.println("word "+key + + " appears " + value + " times."); + } + + for (int value : wordCounts.values()) { + System.out.println(value); + } + +/* + LinkedMap smallEx = new LinkedMap<>(); + smallEx.put("S",0); + smallEx.put("E",1); + smallEx.put("A",2); + + + */ + + } +} diff --git a/src/notes.txt b/src/notes.txt new file mode 100644 index 0000000..e3d9abb --- /dev/null +++ b/src/notes.txt @@ -0,0 +1,14 @@ +abstract data types (ADTs) +- stacks (push, pop, top , size, isEmpty) +- queue (enqueue, dequeue, front, size, isEmpty) +- bag (add, isEmpty) +- symbol table / map + +---------sep. concerns ---- +data structures, underlying the abstract data types +-fixed sized array (built in Java array) +-resizeable array (list -> arraylist) +-linked list (list -> linkedlist) + nodes +- binary tree +- hash table \ No newline at end of file diff --git a/src/princeton/HelloGoodbye.java b/src/princeton/HelloGoodbye.java new file mode 100644 index 0000000..fe41d73 --- /dev/null +++ b/src/princeton/HelloGoodbye.java @@ -0,0 +1,4 @@ +package princeton; + +public class HelloGoodbye { +}