diff --git a/src/Main.java b/src/Main.java index ad5ee5a..5559164 100644 --- a/src/Main.java +++ b/src/Main.java @@ -7,7 +7,10 @@ public static void main(String[] args) { System.out.println("Hello world!"); - List friends = new RecursiveLinkedList(); + + List friends = new ArrayList(); + List friends2 = new ArrayList(); + System.out.println("initial size is " + friends.size()); @@ -23,7 +26,13 @@ public static void main(String[] args) { friends.add("Dorothy"); friends.add("Sophia"); friends.add(2, "Wednesday"); + friends.add("Tina"); + friends2.add("daw"); + friends2.add("hi"); + friends2.add("Tina"); + friends2.add("Rose"); System.out.println("size is now " + friends.size()); + System.out.println(friends.lastIndexOf("Tina")); //for (int i = 0; i < friends.size(); i++) { // System.out.println(friends.get(i)); @@ -36,9 +45,18 @@ public static void main(String[] args) { String name = itr.next(); System.out.println(name); } + friends.addAll(friends2); + System.out.println("size is now " +friends.size()); + friends.removeAll(friends2); + System.out.println("size is now " +friends.size()); + System.out.println(friends2.size()); + System.out.println(friends2.contains("Tina")); + friends.retainAll(friends2); + System.out.println("size is now " +friends.size()); - - for (String name : friends) { + Iterator itrs = friends2.iterator(); + while (itrs.hasNext()) { + String name = itrs.next(); System.out.println(name); } @@ -56,7 +74,29 @@ public static void main(String[] args) { */ + for (String name : friends) { + System.out.println(name); + } + System.out.println(); + System.out.println(); - + List linked1 = new SinglyLinkedList<>(); + linked1.add("Tina1"); + linked1.add("Josh1"); + linked1.add("Susan1"); + linked1.add("Tyler1"); + linked1.add("Usman1"); + linked1.add("Dee1"); + linked1.add("Rose1"); + linked1.add("Blanche1"); + linked1.add("Dorothy1"); + linked1.add(3,"wadwe"); + int haha=linked1.indexOf("wadwe"); + System.out.println(haha); + Iterator itrss = linked1.iterator(); + while (itrss.hasNext()) { + String name = itrss.next(); + System.out.println(name); + } } } \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java index 483ec57..a4ca75f 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -15,7 +15,7 @@ public class ArrayList implements List { // size is DIFFERENT than length private int size; - + // construction public ArrayList() { size = 0; data = (ItemType[]) new Object[10]; @@ -179,7 +179,15 @@ public boolean containsAll(Collection otherCollection) { */ @Override public void addAll(Collection otherCollection) { - throw new UnsupportedOperationException("Not gonna do it!"); + Iterator itr = (Iterator)otherCollection.iterator(); + while (itr.hasNext()) { + checkSize(); + + //ItemType itemToCheck = itr.next(); + + add(itr.next()); + } + } /** @@ -192,7 +200,14 @@ public void addAll(Collection otherCollection) { */ @Override public void removeAll(Collection otherCollection) { + Iterator itr = (Iterator)otherCollection.iterator(); + while (itr.hasNext()) { + + //ItemType itemToCheck = itr.next(); + + remove(itr.next()); + } } /** @@ -205,7 +220,16 @@ public void removeAll(Collection otherCollection) { */ @Override public void retainAll(Collection otherCollection) { - + Iterator itr = (Iterator)otherCollection.iterator(); + Collection collection = new ArrayList(); + while (itr.hasNext()) { + ItemType itemToCheck = itr.next(); + if (contains(itemToCheck)) { + collection.add(itemToCheck); + } + } + clear(); + addAll(collection); } /** @@ -218,7 +242,7 @@ public void retainAll(Collection otherCollection) { */ @Override public ItemType get(int index) { - if (index >= size) { + if (index >= size||index<0) { throw new IndexOutOfBoundsException("index is beyond size"); } @@ -238,7 +262,7 @@ public ItemType get(int index) { */ @Override public void set(int index, ItemType item) { - if (index >= size) { + if (index >= size||index<0) { throw new IndexOutOfBoundsException("index is beyond size"); } @@ -279,6 +303,9 @@ public void add(int index, ItemType item) { */ @Override public void remove(int index) { + if (index >= size||index<0) { + throw new IndexOutOfBoundsException("index is beyond size"); + } // shift values left to overwrite the item at index for (int i = index; i < size - 1; i++) { data[i] = data[i + 1]; @@ -321,6 +348,11 @@ public int indexOf(ItemType item) { */ @Override public int lastIndexOf(ItemType item) { + for (int i = size; i > 0; i--) { + if (item.equals(data[i])) { + return i; + } + } return 0; } @@ -333,7 +365,7 @@ public int lastIndexOf(ItemType item) { */ @Override public ListIterator listIterator() { - return null; + return new SecondCustomIterator(); } @@ -361,41 +393,46 @@ public ItemType next() { private class SecondCustomIterator implements ListIterator { // fancier Iterator - lets us go forwards and backwards private int currentPosition; - + private int lastPosition; public SecondCustomIterator() { currentPosition = 0; + lastPosition=-1; } @Override public boolean hasNext() { - return false; + return currentPosition < size(); } @Override public ItemType next() { - return null; + ItemType result = get(currentPosition); + currentPosition++; + return result; } @Override public boolean hasPrevious() { // hasNext checked currentPosition with size // hasPrevious check currentPosition against 0 - return false; + return currentPosition > 0; } @Override public ItemType previous() { - return null; + ItemType result = get(currentPosition); + currentPosition--; + return result; } @Override public int nextIndex() { - return 0; + return currentPosition+1; } @Override public int previousIndex() { - return 0; + return currentPosition -1; } @Override diff --git a/src/edu/greenriver/sdev333/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java index 4ae2b20..b9ba522 100644 --- a/src/edu/greenriver/sdev333/SinglyLinkedList.java +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -1,29 +1,32 @@ 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?? + //Fields private Node head; private int size; - // helper/inner classes - private class Node { + //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; + + //constructor + public SinglyLinkedList(){ size = 0; + head = null; + } + //helper method + private void checkIndex(int index){ + if(index<0 || index>= size){ + throw new IndexOutOfBoundsException(); + } } /** @@ -43,7 +46,9 @@ public int size() { */ @Override public boolean isEmpty() { - return size == 0; + + return size() == 0||head==null; + } /** @@ -54,6 +59,7 @@ public boolean isEmpty() { * @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... @@ -62,6 +68,7 @@ public boolean contains(ItemType item) { return false; } return true; + } /** @@ -83,13 +90,22 @@ public Iterator iterator() { */ @Override public void add(ItemType item) { - if (item == null) { - throw new NullPointerException(); + + Node newNode = new Node(); + newNode.data = item; + newNode.next = null; + if(head == null){ + head = newNode; + }else { + Node current = head; + while (current.next != null) { + current = current.next; + } + //current is pointing to last node + current.next = newNode; } - // 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); + size++; + } /** @@ -102,35 +118,33 @@ public void add(ItemType item) { */ @Override public void remove(ItemType item) { - if (item == null) { + + if(item == null){ throw new NullPointerException(); } - // alternative - easier to write, but less efficient /* + alternative slower method with less code: int position = indexOf(item); - if (position != -1) { - remove(position); + if(position != -1){ + remove(position); } - */ + */ - if (head.data == item) { + if(head.data == item){ head = head.next; - size--; - } - else { + }else{ Node current = head; Node previous; - while (current.next != null) { + while (current.next != null){ previous = current; current = current.next; - if (current.data.equals(item)) { + if(current.data.equals(item)){ previous.next = current.next; - size--; } } - } + }size -=1; } @@ -140,8 +154,10 @@ public void remove(ItemType item) { */ @Override public void clear() { - head = null; + size = 0; + head = null; + } /** @@ -154,25 +170,14 @@ public void clear() { */ @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)) { + for(ItemType item: otherCollection) { + + if (!this.contains(item)) { return false; } } - return true; - */ - } /** @@ -181,6 +186,8 @@ public boolean containsAll(Collection otherCollection) { * @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 @@ -198,6 +205,7 @@ public void addAll(Collection otherCollection) { add(currentItem); } */ + } /** @@ -211,6 +219,12 @@ public void addAll(Collection otherCollection) { @Override public void removeAll(Collection otherCollection) { + Iterator itr = (Iterator)otherCollection.iterator(); + while(itr.hasNext()) { + ItemType currentItem = itr.next(); + remove(currentItem); + } + } /** @@ -236,21 +250,18 @@ 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; - while (counter != index) { + while(counter != index){ + current = current.next; counter++; } return current.data; - /*for (int i = 0; i < index; i++) { - current = current.next; - }*/ + } /** @@ -267,6 +278,15 @@ public ItemType get(int index) { @Override public void set(int index, ItemType item) { + checkIndex(index); + Node current = head; + int counter = 0; + while(counter!= index){ + current = current.next; + counter++; + } + current.data = item; + } /** @@ -284,13 +304,13 @@ public void set(int index, ItemType item) { @Override public void add(int index, ItemType item) { checkIndex(index); - + //if the head is null 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; @@ -314,6 +334,7 @@ public void add(int index, ItemType item) { private void checkIndex(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException(); + } } @@ -329,7 +350,7 @@ private void checkIndex(int index) { public void remove(int index) { checkIndex(index); - if (index == 0) { + if(index == 0){ head = head.next; } else { @@ -337,11 +358,9 @@ public void remove(int index) { 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--; } @@ -362,13 +381,12 @@ public int indexOf(ItemType item) { 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; } @@ -396,6 +414,7 @@ public int lastIndexOf(ItemType item) { */ @Override public ListIterator listIterator() { + return new OurEnhancedIterator(); } @@ -413,6 +432,7 @@ 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; @@ -426,6 +446,7 @@ public ItemType next() { } } + private class OurEnhancedIterator implements ListIterator { private Node currentPosition; @@ -485,3 +506,4 @@ public void add(ItemType itemType) { } } +