From c5eead044152edb32d255da0433c7b5356e8a0f6 Mon Sep 17 00:00:00 2001 From: dithizac000 Date: Wed, 11 Jan 2023 14:26:34 -0800 Subject: [PATCH 1/9] Updated with add method and return size --- .idea/vcs.xml | 6 + src/Main.java | 17 ++- src/edu/greenriver/sdev333/ArrayList.java | 129 ++++++++++++++++++++++ 3 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 .idea/vcs.xml create mode 100644 src/edu/greenriver/sdev333/ArrayList.java 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..c7545e2 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,20 @@ +import edu.greenriver.sdev333.*; + 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("Jess"); + friends.add("Tina"); + friends.add("Susan"); + friends.add("Tyler"); + friends.add("Usman"); + friends.add("Dee"); + friends.add("Rose"); + + System.out.println(friends.get(0)); + System.out.println("Size is now " + friends.size()); } } \ 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..c67023a --- /dev/null +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -0,0 +1,129 @@ +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]; + } + + @Override + public int size() { + return size; + } + + @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) { + // all above code works until I run out of room + // when size becomes the same as length + 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 + data[size] = item; + size++; + } // end of method + + @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) { + + } + + @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; + } +} From b9d21fe05459223dc5b869a4aa3318124eaf7f13 Mon Sep 17 00:00:00 2001 From: dithizac000 Date: Wed, 18 Jan 2023 14:51:16 -0800 Subject: [PATCH 2/9] updated --- src/Main.java | 27 +++- src/edu/greenriver/sdev333/ArrayList.java | 168 ++++++++++++++++++++-- 2 files changed, 174 insertions(+), 21 deletions(-) diff --git a/src/Main.java b/src/Main.java index c7545e2..92ea920 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,4 +1,5 @@ import edu.greenriver.sdev333.*; +import java.util.Iterator; public class Main { public static void main(String[] args) { @@ -9,12 +10,26 @@ public static void main(String[] args) { friends.add("Jess"); friends.add("Tina"); friends.add("Susan"); - friends.add("Tyler"); - friends.add("Usman"); - friends.add("Dee"); - friends.add("Rose"); + friends.add(0,"Bob"); System.out.println(friends.get(0)); System.out.println("Size is now " + friends.size()); - } -} \ No newline at end of file + + // friends .add all will cause an intentional crash + +// for (int i = 0; i < friends.size() ; i++) { +// System.out.println(friends.get(i)); +// } + + // above : import java.util.Itereator; + Iterator itr = friends.iterator(); + while (itr.hasNext()) { + String name = itr.next(); + System.out.println(name); + } + + for(String name:friends) { + System.out.println(name); + } + } // end of main method + } // end of main class \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java index c67023a..8dacbbb 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -25,23 +25,30 @@ public int size() { @Override public boolean isEmpty() { - return false; + 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 null; + return new OurCustomIterator(); } - @Override - public void add(ItemType item) { - // all above code works until I run out of room - // when size becomes the same as length + private void checkSize() { if(size == data.length) { // need to double up array size // Step 1 - create a new larger array @@ -58,28 +65,56 @@ public void add(ItemType item) { // 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) { - return false; + 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 @@ -94,27 +129,54 @@ public void retainAll(Collection otherCollection) { @Override public ItemType get(int index) { - return null; + 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) { - return 0; + + for (int i = 0; i < size; i++) { + if(item.equals(data[i])) { + return i; + } + } return -1; // if not then return -1 } @Override @@ -124,6 +186,82 @@ public int lastIndexOf(ItemType item) { @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 From ba86c865cc25ffd48a5a6c8b7ca35c0266b30add Mon Sep 17 00:00:00 2001 From: dithizac000 Date: Thu, 19 Jan 2023 15:38:04 -0800 Subject: [PATCH 3/9] Updated with add method and return size --- src/Main.java | 7 +++++-- src/edu/greenriver/sdev333/ArrayList.java | 13 ++++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/Main.java b/src/Main.java index 92ea920..4c41508 100644 --- a/src/Main.java +++ b/src/Main.java @@ -11,9 +11,12 @@ public static void main(String[] args) { friends.add("Tina"); friends.add("Susan"); friends.add(0,"Bob"); + friends.add("Bob"); + friends.add("Bob"); System.out.println(friends.get(0)); System.out.println("Size is now " + friends.size()); + System.out.println("Last Index of Bob: "+friends.lastIndexOf("Bob")); // friends .add all will cause an intentional crash @@ -25,11 +28,11 @@ public static void main(String[] args) { Iterator itr = friends.iterator(); while (itr.hasNext()) { String name = itr.next(); - System.out.println(name); + // System.out.println(name); } for(String name:friends) { - System.out.println(name); + // System.out.println(name); } } // end of main method } // end of main class \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java index 8dacbbb..447c996 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -1,5 +1,4 @@ package edu.greenriver.sdev333; - import java.util.Iterator; import java.util.ListIterator; @@ -17,6 +16,10 @@ public ArrayList() { size = 0; data = (ItemType[]) new Object[10]; } + public ArrayList(int capacity) { + size = 0; + data = (ItemType[]) new Object[capacity]; + } @Override public int size() { @@ -181,9 +184,12 @@ public int indexOf(ItemType item) { @Override public int lastIndexOf(ItemType item) { - return 0; + for (int i = size ; i >= 0; i--) { + if(item.equals(data[i])) { + return i; + } + } return - 1; } - @Override public ListIterator listIterator() { @@ -263,5 +269,6 @@ public void set(ItemType itemType) { public void add(ItemType itemType) { } + } }// end of ArrayList class From 60b047edc6d3a12978df19e31901a91bb37336ce Mon Sep 17 00:00:00 2001 From: dithizac000 Date: Mon, 23 Jan 2023 14:22:57 -0800 Subject: [PATCH 4/9] Updated with add method and return size --- src/edu/greenriver/sdev333/SinglyLinkedList.java | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/edu/greenriver/sdev333/SinglyLinkedList.java diff --git a/src/edu/greenriver/sdev333/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java new file mode 100644 index 0000000..80ed8ce --- /dev/null +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -0,0 +1,4 @@ +package edu.greenriver.sdev333; + +public class SinglyLinkedList { +} From 565dc789e94172ad7fa258a80a9992e17eacf6a7 Mon Sep 17 00:00:00 2001 From: dithizac000 Date: Mon, 23 Jan 2023 14:46:01 -0800 Subject: [PATCH 5/9] Updated with add method and return size --- .../greenriver/sdev333/DoublyLinkedList.java | 255 ++++++++++++++++++ .../greenriver/sdev333/SinglyLinkedList.java | 251 ++++++++++++++++- 2 files changed, 505 insertions(+), 1 deletion(-) create mode 100644 src/edu/greenriver/sdev333/DoublyLinkedList.java 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/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java index 80ed8ce..d4f27bf 100644 --- a/src/edu/greenriver/sdev333/SinglyLinkedList.java +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -1,4 +1,253 @@ package edu.greenriver.sdev333; +import java.util.Iterator; +import java.util.ListIterator; -public class SinglyLinkedList { +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 0; + } + + /** + * 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) { + 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() { + 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) { + 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; + } } From c43f166e332e9e3a5b3fa16de8fa2cb874fd4602 Mon Sep 17 00:00:00 2001 From: dithizac000 Date: Wed, 1 Feb 2023 21:55:02 -0800 Subject: [PATCH 6/9] Updated with add method and return size --- src/Main.java | 64 +++-- src/edu/greenriver/sdev333/Node.java | 35 +++ .../greenriver/sdev333/SinglyLinkedList.java | 240 +++++++++++++++++- 3 files changed, 307 insertions(+), 32 deletions(-) create mode 100644 src/edu/greenriver/sdev333/Node.java diff --git a/src/Main.java b/src/Main.java index 4c41508..511bbf9 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,38 +1,60 @@ import edu.greenriver.sdev333.*; import java.util.Iterator; +import java.util.ListIterator; public class Main { public static void main(String[] args) { - List friends = new ArrayList(); + System.out.println("Hello world!"); + + List friends = new SinglyLinkedList(); + System.out.println("initial size is " + friends.size()); friends.add("Jess"); friends.add("Tina"); + friends.add("Josh"); friends.add("Susan"); - friends.add(0,"Bob"); - friends.add("Bob"); - friends.add("Bob"); - - System.out.println(friends.get(0)); - System.out.println("Size is now " + friends.size()); - System.out.println("Last Index of Bob: "+friends.lastIndexOf("Bob")); - - // friends .add all will cause an intentional crash - -// for (int i = 0; i < friends.size() ; i++) { -// System.out.println(friends.get(i)); -// } - - // above : import java.util.Itereator; + 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); + System.out.println(name); } - for(String name:friends) { - // System.out.println(name); + + for (String name : friends) { + System.out.println(name); } - } // end of main method - } // end of main class \ No newline at end of file + + 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); + } + + + + + + } +} \ 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/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java index d4f27bf..dea0b1b 100644 --- a/src/edu/greenriver/sdev333/SinglyLinkedList.java +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -1,4 +1,6 @@ package edu.greenriver.sdev333; + +import javax.naming.OperationNotSupportedException; import java.util.Iterator; import java.util.ListIterator; @@ -18,11 +20,12 @@ private class Node { * Constructor */ public SinglyLinkedList() { - // an empty list has no nodes, which means it has no head, - // so set head to null + // 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. * @@ -30,7 +33,7 @@ public SinglyLinkedList() { */ @Override public int size() { - return 0; + return size; } /** @@ -53,7 +56,12 @@ public boolean isEmpty() { */ @Override public boolean contains(ItemType item) { - return false; + // assume indexOf is working... + int position = indexOf(item); + if (position == -1) { + return false; + } + return true; } /** @@ -63,7 +71,7 @@ public boolean contains(ItemType item) { */ @Override public Iterator iterator() { - return null; + return new OurCustomIterator(); } /** @@ -75,7 +83,13 @@ public Iterator iterator() { */ @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); } /** @@ -88,6 +102,35 @@ public void add(ItemType item) { */ @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--; + } + } + } } @@ -121,7 +164,21 @@ public boolean containsAll(Collection otherCollection) { */ @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()) { + while (itr.hasNext()) { + ItemType currentItem = itr.next(); + add(0,currentItem); // add to front of list, make linear O-n to constant O-1 + } + } + /* for each option + for (ItemType currentItem : otherCollection) { + add(currentItem); + } + */ } /** @@ -160,7 +217,21 @@ public void retainAll(Collection otherCollection) { */ @Override public ItemType get(int index) { - return null; + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + + 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; + }*/ } /** @@ -176,7 +247,9 @@ public ItemType get(int index) { */ @Override public void set(int index, ItemType item) { - + // not sure if this is right. just made this + head.data = get(index); + head.next.equals(item); } /** @@ -193,7 +266,39 @@ public void set(int index, ItemType item) { */ @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++; + } + + private void checkIndex(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } } /** @@ -206,6 +311,22 @@ 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--; } @@ -221,7 +342,18 @@ public void remove(int index) { */ @Override public int indexOf(ItemType item) { - return 0; + 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; } /** @@ -248,6 +380,92 @@ public int lastIndexOf(ItemType item) { */ @Override public ListIterator listIterator() { - return null; + 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) { + + } + } + +} \ No newline at end of file From b56d719d610bbee2ddc37b77c8315ddd74a91cd1 Mon Sep 17 00:00:00 2001 From: dithizac000 Date: Mon, 6 Feb 2023 13:06:51 -0800 Subject: [PATCH 7/9] Updated with add method and return size --- src/edu/greenriver/sdev333/RecursiveLinkdList.java | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/edu/greenriver/sdev333/RecursiveLinkdList.java diff --git a/src/edu/greenriver/sdev333/RecursiveLinkdList.java b/src/edu/greenriver/sdev333/RecursiveLinkdList.java new file mode 100644 index 0000000..e33e53b --- /dev/null +++ b/src/edu/greenriver/sdev333/RecursiveLinkdList.java @@ -0,0 +1,4 @@ +package edu.greenriver.sdev333; + +public class RecursiveLinkdList { +} From 10c01834381e78d4448c11cfe1281976ee50cb0f Mon Sep 17 00:00:00 2001 From: dithizac000 Date: Mon, 13 Feb 2023 21:42:48 -0800 Subject: [PATCH 8/9] Updated with add method and return size --- .idea/.name | 1 + .idea/uiDesigner.xml | 124 ++++++++++++++ .idea/vcs.xml | 2 + src/{ => edu/greenriver/sdev333}/Main.java | 15 +- .../sdev333/RecursiveLinkdList.java | 4 - .../sdev333/RecursiveLinkedList.java | 160 ++++++++++++++++++ .../greenriver/sdev333/SinglyLinkedList.java | 81 ++++++--- .../sdev333/learningLinkedList.java | 34 ++++ src/edu/greenriver/sdev333/scratch.txt | 19 +++ src/map_client/LinkedMap.java | 47 +++++ src/map_client/main.java | 86 ++++++++++ src/notes.txt | 14 ++ src/princeton/HelloGoodbye.java | 4 + 13 files changed, 559 insertions(+), 32 deletions(-) create mode 100644 .idea/.name create mode 100644 .idea/uiDesigner.xml rename src/{ => edu/greenriver/sdev333}/Main.java (82%) delete mode 100644 src/edu/greenriver/sdev333/RecursiveLinkdList.java create mode 100644 src/edu/greenriver/sdev333/RecursiveLinkedList.java create mode 100644 src/edu/greenriver/sdev333/learningLinkedList.java create mode 100644 src/edu/greenriver/sdev333/scratch.txt create mode 100644 src/map_client/LinkedMap.java create mode 100644 src/map_client/main.java create mode 100644 src/notes.txt create mode 100644 src/princeton/HelloGoodbye.java 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 index 35eb1dd..6e97bd9 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -2,5 +2,7 @@ + + \ No newline at end of file diff --git a/src/Main.java b/src/edu/greenriver/sdev333/Main.java similarity index 82% rename from src/Main.java rename to src/edu/greenriver/sdev333/Main.java index 511bbf9..cc56e1b 100644 --- a/src/Main.java +++ b/src/edu/greenriver/sdev333/Main.java @@ -1,3 +1,5 @@ +package edu.greenriver.sdev333; + import edu.greenriver.sdev333.*; import java.util.Iterator; import java.util.ListIterator; @@ -7,7 +9,7 @@ public static void main(String[] args) { System.out.println("Hello world!"); - List friends = new SinglyLinkedList(); + List friends = new RecursiveLinkedList(); System.out.println("initial size is " + friends.size()); @@ -30,17 +32,15 @@ public static void main(String[] args) { //} // 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(); @@ -51,9 +51,14 @@ public static void main(String[] args) { 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)); } diff --git a/src/edu/greenriver/sdev333/RecursiveLinkdList.java b/src/edu/greenriver/sdev333/RecursiveLinkdList.java deleted file mode 100644 index e33e53b..0000000 --- a/src/edu/greenriver/sdev333/RecursiveLinkdList.java +++ /dev/null @@ -1,4 +0,0 @@ -package edu.greenriver.sdev333; - -public class RecursiveLinkdList { -} 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 index dea0b1b..9fabdd4 100644 --- a/src/edu/greenriver/sdev333/SinglyLinkedList.java +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -154,7 +154,24 @@ public void clear() { */ @Override public boolean containsAll(Collection otherCollection) { - return false; + 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; + */ + } /** @@ -165,20 +182,21 @@ public boolean containsAll(Collection otherCollection) { @Override public void addAll(Collection otherCollection) { // walk through the other collection - // for each loop or use Iterator + // for-each loop + // or use Iterator + Iterator itr = (Iterator)otherCollection.iterator(); - while(itr.hasNext()) { - while (itr.hasNext()) { - ItemType currentItem = itr.next(); - add(0,currentItem); // add to front of list, make linear O-n to constant O-1 - } + while (itr.hasNext()) { + ItemType currentItem = itr.next(); + add(currentItem); } - /* for each option + + /* + // alternative implementation using a for-each loop for (ItemType currentItem : otherCollection) { add(currentItem); } - - */ + */ } /** @@ -217,9 +235,7 @@ public void retainAll(Collection otherCollection) { */ @Override public ItemType get(int index) { - if (index < 0 || index >= size) { - throw new IndexOutOfBoundsException(); - } + checkIndex(index); Node current = head; int counter = 0; @@ -247,9 +263,34 @@ public ItemType get(int index) { */ @Override public void set(int index, ItemType item) { - // not sure if this is right. just made this - head.data = get(index); - head.next.equals(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(); + } } /** @@ -273,7 +314,6 @@ public void add(int index, ItemType item) { Node theNewOne = new Node(); theNewOne.data = item; theNewOne.next = head; - head = theNewOne; } else { @@ -295,11 +335,6 @@ public void add(int index, ItemType item) { size++; } - private void checkIndex(int index) { - if (index < 0 || index >= size) { - throw new IndexOutOfBoundsException(); - } - } /** * Removes the element at the specified position in this list. @@ -395,7 +430,7 @@ public OurCustomIterator() { @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) + // see if I made it past the last node: if (cLurrent == null) if (currentPosition != null) { return true; } 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..fd6d17a --- /dev/null +++ b/src/map_client/LinkedMap.java @@ -0,0 +1,47 @@ +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..1616d4e --- /dev/null +++ b/src/map_client/main.java @@ -0,0 +1,86 @@ +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 { +} From e0891a51207ebe269898dea3106d3326abd9aaeb Mon Sep 17 00:00:00 2001 From: dithizac000 Date: Mon, 13 Feb 2023 21:53:03 -0800 Subject: [PATCH 9/9] Singlly Linked List Test --- src/map_client/LinkedMap.java | 3 +++ src/map_client/main.java | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/map_client/LinkedMap.java b/src/map_client/LinkedMap.java index fd6d17a..f06d28c 100644 --- a/src/map_client/LinkedMap.java +++ b/src/map_client/LinkedMap.java @@ -1,6 +1,7 @@ package map_client; public class LinkedMap { + /* // helper class private class Node { KeyType key; @@ -44,4 +45,6 @@ public ValueType get(KeyTyoe theKey) { // key not in the list return null; } + + */ } diff --git a/src/map_client/main.java b/src/map_client/main.java index 1616d4e..e7c2954 100644 --- a/src/map_client/main.java +++ b/src/map_client/main.java @@ -75,12 +75,14 @@ public static void main(String[] args) throws FileNotFoundException { System.out.println(value); } - +/* LinkedMap smallEx = new LinkedMap<>(); smallEx.put("S",0); smallEx.put("E",1); smallEx.put("A",2); + */ + } }