diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..1b2425b --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,299 @@ + + + + \ 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..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/SDEV333-Term-Project.iml b/SDEV333-Term-Project.iml index c90834f..d8a1435 100644 --- a/SDEV333-Term-Project.iml +++ b/SDEV333-Term-Project.iml @@ -7,5 +7,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Bag.java b/src/Bag.java new file mode 100644 index 0000000..4adc97d --- /dev/null +++ b/src/Bag.java @@ -0,0 +1,25 @@ +/** + * Bag interface + * @param class / data type of the items in the queue + */ +public interface Bag extends Iterable { + + /** + * Add an item to the bag + * @param item the item to be added + */ + void add(Item item); + + /** + * Checks to see if the bag is empty + * @return true if empty, false is not empty + */ + boolean isEmpty(); + + /** + * Returns the count of items in the queue + * @return the number of items in the queue + */ + + int size(); +} diff --git a/src/List.java b/src/List.java index 2bf4aef..920f997 100644 --- a/src/List.java +++ b/src/List.java @@ -37,6 +37,12 @@ public interface List extends Iterable { */ void set(int i, E item); + void addFront(int value); + + void addBack(int value); + + void add(int index, int value); + /** * Remove item at the front of the list. * @return the item that was removed @@ -69,6 +75,10 @@ public interface List extends Iterable { */ boolean contains(E item); + boolean contains(int value); + + int indexOf(int value); + /** * Checks if the list is empty. * @return true if the list is empty, false otherwise @@ -80,4 +90,6 @@ public interface List extends Iterable { * @return number of items in the list */ int size(); + + void clear(); } diff --git a/src/lists/ArrayList.java b/src/lists/ArrayList.java new file mode 100644 index 0000000..55efff0 --- /dev/null +++ b/src/lists/ArrayList.java @@ -0,0 +1,424 @@ +/** + * Abstract arrayList implementation + * + * @author Lillian Nelson + * @version 1.0 + */ +package lists; + +import java.util.*; +import java.util.List; + + +public class ArrayList implements List +{ + //fields: + private int size; + private E[] buffer; + + //constructor + public ArrayList() + { + //initialize variables + size = 0; + this.buffer = (E[]) new Object[10]; + } + + /** + * inserts the specified value at the front of the list + * Shifts the value currently at the front of the list + * 0(n) linear time in the worst case because + * of the time to loop through array + * + * @param value to be inserted + */ + public void addFront(E value) + { + if (size == buffer.length) + { + resize(size * 2); + } + for (int i = size; i > 0; i--) + { + buffer[i] = buffer[i - 1]; + } + + buffer[0] = value; + size++; + } + + /** + * inserts the specified value at the back of the list + * 0(1) or constant in the worst case because + * No matter the size of list the operations are the same + * + * @param value to be inserted + */ + public void addBack(E value) + { + if (size == buffer.length) + { + resize(size * 2); + } + buffer[size] = value; + size++; + } + + /** + * Inserts the specified value at the specified position in this list. + * 0(n) linear time in the worst case because + * of the time to loop through array + * + * @param index the place selected + * @param value value to be inserted + * @throws IndexOutOfBoundsException if the index is out of range + */ + public void add(int index, E value) + { + if (size == buffer.length) + { + resize(2); + } + for (int i = size + 1; i > 0; i--) + { + buffer[i + 1] = buffer[i]; + } + buffer[index] = value; + size++; + } + + /** + * Removes the value located at the front of the list + * 0(n) linear time in the worst case because + * of the time to loop through array + */ + public E removeFront() + { + if (!isEmpty()) + { + E head = null; + for (int i = 0; i <= size - 2; i++) + { + head = buffer[i]; + buffer[i] = buffer[i + 1]; + } + size--; + return head; + } + + return null; + } + + /** + * Removes the value located at the back of the list + * O(1) constant time in the worst case because + * regardless of size, operations are the same + * + */ + public E removeBack() + { + if (size != 0) + { + E head = buffer[size]; + buffer[size] = null; + size--; + return head; + } + return null; + } + + /** + * Remove item at a specified index. + * 0(n) linear time in the worst case because + * of the time to loop through array + * + * @param index the index where the item should be removed + * @return the item that was removed + */ + @Override + public E remove(int index) + { + //validate index + if (index >= size) + { + throw new IndexOutOfBoundsException("Index cannot be greater than size"); + } + else if (index < 0) + { + throw new IndexOutOfBoundsException("Index cannot be less than zero"); + } + + E copyOfRemoveValue = buffer[index]; + + for (int i = index; i <= size - 1; i++) + { + buffer[i] = buffer[i + 1]; + } + + buffer[size - 1] = null; + + size--; + + return copyOfRemoveValue; + } + + + /** + * Returns the value at the specified position in the list. + * 0(n) linear time in the worst case because + * of the time to loop through array + * @param index index of the value to return + * @return the value at the specified position in this list + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public E get(int index) + { + if (index >= size) + { + throw new IndexOutOfBoundsException("Index cannot be greater than size"); + } + else if (index < 0) + { + throw new IndexOutOfBoundsException("Index cannot be less than zero"); + } + + return buffer[index]; + } + /** + * Returns true if this list contains the specified value. + * 0(n) linear time in the worst case because + * of the time to loop through array + * @param value value whose presence in this list is to be searched for + * @return true if this list contains the specified value + */ + public boolean contains(Object value) + { + for (int i = 0; i < size; i++) + { + if (buffer[i] == value) + { + return true; + } + } + return false; + } + /** + * Returns the index of the first occurrence of the specified value + * in this list, or -1 if this list does not contain the value. + * 0(n) linear time in the worst case because + * of the time to loop through array + * @param value value to search for + * @return the index of the first occurrence of value + */ + public int indexOf(Object value) + { + for (int i = 0; i < size; i++) + { + if (buffer[i] == value) + { + return i; + } + } + + return -1; + } + /** + * Returns true if this list contains no values. + * O(1) constant time? + * + * @return true if this list contains no values + */ + @Override + public boolean isEmpty() + { + return size == 0; + } + + /** + * Returns the number of values in this list. + * O(1) constant time? + * + * @return the number of values in this list + */ + @Override + public int size() + { + return size; + } + + /** + * Removes all the values from this list. + * The list will be empty after this call returns. + * O(1) constant time + */ + @Override + public void clear() + { + //created a new array + buffer = (E[]) new Object[10]; + + size = 0; + + } + /** + * takes array and creates a new one to make more space + * 0(n) linear time in the worst case because + * of the time to loop through array + * @param newSize bigger array + */ + + private void resize(int newSize) + { + //create new space, separate from the old(buffer) + E[] newBuffer = (E[]) new Object[newSize]; + + //copy everything over from buffer into newBuffer + for (int i = 0; i < buffer.length; i++) + { + newBuffer[i] = buffer[i]; + } + + //set the new space into buffer + buffer = newBuffer; + //old space is no longer "pointed to" and will be cleaned by the garbage collector + } + + /** + * Returns an iterator over elements of type E + * 0(1) constant time returning iterator + * + * @return an Iterator. + */ + @Override + public Iterator iterator() + { + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator + { + //private fields + private int i; + + private ArrayListIterator() + { + i = 0; + } + + /** + * Returns true if the iteration has more elements. + * + * @return true if the iteration has more elements + */ + @Override + public boolean hasNext() + { + return i < size; + } + + /** + * Returns the next element in the iteration. + * + * @return the next element in the iteration + * @throws NoSuchElementException if the iteration has no more elements + */ + @Override + public E next() + { + if (i > size) + { + throw new NoSuchElementException("i is out of bounds"); + } + E currentValue = buffer[i]; + i++; + return currentValue; + } + } + + + //added by java + @Override + public boolean containsAll(Collection c) + { + return false; + } + + @Override + public boolean addAll(Collection c) + { + return false; + } + + @Override + public boolean addAll(int index, Collection c) + { + return false; + } + + @Override + public boolean removeAll(Collection c) + { + return false; + } + + @Override + public boolean retainAll(Collection c) + { + return false; + } + + @Override + public E set(int index, E element) + { + return null; + } + + @Override + public int lastIndexOf(Object o) + { + return 0; + } + + @Override + public ListIterator listIterator() + { + return null; + } + + @Override + public ListIterator listIterator(int index) + { + return null; + } + + @Override + public List subList(int fromIndex, int toIndex) + { + return null; + } + + @Override + public Object[] toArray() + { + return new Object[0]; + } + + @Override + public T[] toArray(T[] a) + { + return null; + } + + @Override + public boolean add(E e) + { + return false; + } + + @Override + public boolean remove(Object o) + { + return false; + } +} \ No newline at end of file diff --git a/src/lists/LinkedBag.java b/src/lists/LinkedBag.java new file mode 100644 index 0000000..4c18d70 --- /dev/null +++ b/src/lists/LinkedBag.java @@ -0,0 +1,88 @@ +/** + * LinkedList implementation of the Bag interface + * + * @author Lillian Nelson + * @version 1.0 + */ +package lists; + +import java.util.Iterator; + +public class LinkedBag implements Iterable +{ + private Node head; // first Node in the list + + private class Node + { + Item item; + Node next; + } + + /** + * adds a new item into the LinkedBag + * 0(1) or constant time in the worst case + * no matter size operations are the same + * + * @param item Item to add + */ + public void add(Item item) + { // Same as push() in Stack + Node oldHead = head; + head = new Node(); + head.item = item; + head.next = oldHead; + } + + /** + * Returns an iterator over elements of type Item + * 0(1) constant time returning iterator + * + * @return an Iterator. + */ + public Iterator iterator() + { + return new LinkedBag.LinkedIterator(); + } + + private class LinkedIterator implements Iterator + { + private LinkedBag.Node current = head; + + /** + * Returns true if the iteration has more elements. + * 0(1) or constant time in the worst case + * return is the same + * @return true if the iteration has more elements + */ + @Override + public boolean hasNext() + { + return current != null; + } + + /** + * Returns the next element in the iteration + * 0(1) or constant time in the worst case + * because no matter size operations are the same + * + * @return the next Item + */ + @Override + public Item next() + { + Item item = (Item) current.item; + current = current.next; + return item; + } + + /** + * Left Blank? + */ + @Override + public void remove() + { + + } + + } +} diff --git a/src/lists/LinkedList.java b/src/lists/LinkedList.java new file mode 100644 index 0000000..857ac60 --- /dev/null +++ b/src/lists/LinkedList.java @@ -0,0 +1,454 @@ +/** + * Abstract LinkedList implementation + * + * @author Lillian Nelson + * @version 1.0 + */ +package lists; +import java.util.*; + +public class LinkedList implements List +{ + // Define the node + private class Node { + int data; + Node next; + } + + // Set up the head + private Node head; + + // set up a size field + private int size; + + // add a constructor + public void LinkedIntList() { + head = null; + size = 0; + } + + /** + * inserts the specified value at the front of the list + * Shifts the value currently at the front of the list and any + * subsequent values to the right. + * O(1) constant time in the worst case because + * regardless of size, operations are the same + * + * @param value value to be inserted + */ + public void addFront(int value) { + //set up a new Node + Node theNewOne = new Node(); + theNewOne.data = value; + + if (head == null) { + // the list is currently empty + head = theNewOne; + } + else { + // the list currently has some nodes in it + theNewOne.next = head; + head = theNewOne; + } + + size++; + + } + + /** + * inserts the specified value at the back of the list (at index size()-1). + * O(1) constant time in the worst case because + * regardless of size, operations are the same + * @param value value to be inserted + */ + public void addBack(int value) + { + Node theNewOne = new Node(); + theNewOne.data = value; + // set a marker + Node current = head; + if (head == null) { + head = theNewOne; + } + else { + // Move to the end + while(current.next != null) { + current = current.next; + } + current.next = theNewOne; + } + size++; + + } + + /** + * Inserts the specified value at the specified position in this list. + * Shifts the value currently at that position (if any) and any subsequent + * values to the right. + * O(1) constant time in the worst case because + * regardless of size, operations are the same + * + * @param index index at which the specified value is to be inserted + * @param value value to be inserted + * @throws IndexOutOfBoundsException if the index is out of range + */ + public void add(int index, int value) + { + if (index == 0) + { + head = new Node(); + head.data = value; + + }else + { + Node current = head; + for (int i = 0; i < index - 1; i++) + { + current = current.next; + } + current.next = new Node(); + current.next.data = value; + size++; + } + } + + /** + * Removes the value located at the front of the list + * Shifts any subsequent values to the left. + * O(1) constant time in the worst case because + * regardless of size, operations are the same + * @return + */ + public Integer removeFront() + { + if (!isEmpty()) + { + head = head.next; + size--; + } + + return null; + } + + /** + * Removes the value located at the back of the list + * 0(n) linear time in the worst case because + * of the time to loop through array + * + * @return + */ + public Integer removeBack() + { + Node beforeLast = head; + while (beforeLast.next.next != null) + { + beforeLast = beforeLast.next; + } + beforeLast.next = null; + return null; + } + + /** + * Removes the value at the specified position in this list. + * Shifts any subsequent values to the left. Returns the value + * that was removed from the list. + * 0(n) linear time in the worst case because + * of the time to loop through array + * + * @param index the index of the value to be removed + * @return the value previously at the specified position + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public Integer remove(int index) + { + if (index == 0) + { + head = head.next; + } + else { + Node current = head; + for (int i = 0; i < index - 1; i++) + { + current = current.next; + + } + Node next = current.next.next; + current.next = next; + size--; + } + return 0; + } + + /** + * Returns the value at the specified position in the list. + * 0(n) linear time in the worst case because + * of the time to loop through array + * + * @param index index of the value to return + * @return the value at the specified position in this list + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public Integer get(int index) + { + Node current = head; + for (int i = 0; i < index; i++) + { + current = current.next; + + } + return current.data; + } + + /** + * Returns true if this list contains the specified value. + * 0(n) linear time in the worst case because + * of the time to loop through array + * + * @param value value whose presence in this list is to be searched for + * @return true if this list contains the specified value + */ + public boolean contains(int value) + { + if (head == null) + { + return false; + } + else + { + Node current = head; + + while (current.next != null) + { + if (current.data == value) + { + return true; + } + current = current.next; + } + return false; + } + } + + /** + * Returns the index of the first occurrence of the specified value + * in this list, or -1 if this list does not contain the value. + * 0(n) linear time in the worst case because + * of the time to loop through array + * + * @param value value to search for + * @return the index of the first occurrence of the specified value in this list + * or -1 if this list does not contain the value + */ + public int indexOf(int value) + { + if (head == null) + { + return -1; + }else + { + Node current = head; + int index = 0; + + while (current != null) + { + if (current.data == value) + { + return index; + } + current = current.next; + index++; + } + return -1; + } + } + + /** + * Returns the size variable + * 0(1) or constant in the worst case because + * No matter the size of list the operations are the same + * + * @return the size of list + */ + @Override + public int size() + { + return size; + } + + /** + * Returns true if this list contains no values. + * 0(1) or constant in the worst case because + * No matter the size of list the operations are the same + * + * @return true if this list contains no values + */ + @Override + public boolean isEmpty() + { + return size == 0; + } + + /** + * Removes all the values from this list. + * The list will be empty after this call returns. + * 0(1) or constant in the worst case because + * No matter the size of list the operations are the same + * + */ + @Override + public void clear() + { + size = 0; + head = null; + } + + /** + * Returns an iterator over elements of type E + * 0(1) or constant in the worst case because + * No matter the size of list the operations are the same + * @return an Iterator. + */ + @Override + public Iterator iterator() + { + return new SinglyLinkedIterator(); + } + + // helper class/type that defined how iterator works + private class SinglyLinkedIterator implements Iterator + { + // private fields + private Node current; + + public SinglyLinkedIterator() { + current = head; + } + + /** + * Returns true if the iteration has more elements. + * + * @return true if the iteration has more elements + */ + @Override + public boolean hasNext() + { + if (current != null) { + return true; + } + else + { + return false; + } + } + + /** + * Returns the next element in the iteration. + * + * @return the next element in the iteration + * @throws NoSuchElementException if the iteration has no more elements + */ + @Override + public Integer next() + { + if (current == null) { + throw new NoSuchElementException("There is no next one to go too!"); + } + else { + int item = current.data; + current = current.next; + return item; + } + } + } + + + //java added these or made the call abstract was have a lot of frustration getting the tests going + + public int indexOf(Object o) + { + return 0; + } + + public int lastIndexOf(Object o) + { + return 0; + } + + public ListIterator listIterator() + { + return null; + } + + public ListIterator listIterator(int index) + { + return null; + } + + public List subList(int fromIndex, int toIndex) + { + return null; + } + + public Integer set(int index, Integer element) + { + return null; + } + + public void add(int index, Integer element) + { + + } + + public Object[] toArray() + { + return new Object[0]; + } + + public T[] toArray(T[] a) + { + return null; + } + + public boolean add(Integer integer) + { + return false; + } + @Override + public boolean contains(Object o) + { + return false; + } + + public boolean remove(Object o) + { + return false; + } + + public boolean containsAll(Collection c) + { + return false; + } + + public boolean addAll(Collection c) + { + return false; + } + + public boolean addAll(int index, Collection c) + { + return false; + } + + public boolean removeAll(Collection c) + { + return false; + } + + public boolean retainAll(Collection c) + { + return false; + } + +} diff --git a/src/lists/LinkedQueue.java b/src/lists/LinkedQueue.java new file mode 100644 index 0000000..3804015 --- /dev/null +++ b/src/lists/LinkedQueue.java @@ -0,0 +1,234 @@ +/** + * LinkedList implementation of the Queue interface + * + * @author Lillian Nelson + * @version 1.0 + */ +package lists; + +import java.util.Collection; +import java.util.Iterator; +import java.util.Queue; + +public class LinkedQueue implements Queue //this was red needed to be abstract? +{ + private Node head; // link to least recently added node + private Node tail; // link to most recently added node + private int size; + + private class Node + { // nested class to define nodes + Item item; + Node next; + } + + /** + * Returns if LinkedQueue is empty + * 0(1) or constant time in the worst case + * no matter size operation is the same + * @return if head is null + */ + public boolean isEmpty() + { + return head == null; + } + + /** + * Returns size of LinkedQueue + * 0(1) or constant time in the worst case + * no matter size the return is the same + * + * @return size + */ + public int size() + { + return size; + } + + /** + * adds a new item into the LinkedQueue + * 0(1) or constant time in the worst case + * no matter size operations are the same + * + * @param item Item to add + */ + public Item enqueue(Item item) + { // Add item to the end of the list + Node oldTail = tail; + tail = new Node(); + tail.item = item; + tail.next = null; + if (isEmpty()) + { + head = tail; + } + else + { + oldTail.next = tail; + } + size++; + return item; + } + /** + * Removes an item from the LinkedQueue + * 0(1) or constant time in the worst case + * no matter size operations stay the same + * + * @return the item removed + */ + public Item dequeue() + { // Remove item from the beginning of the list + Item item = head.item; + head = head.next; + size--; + if (isEmpty()) + { + tail = null; + } + return item; + } + + /** + * Returns an iterator over elements of type Item + * 0(1) constant time returning iterator + * + * @return an Iterator. + */ + public Iterator iterator() + { + return new LinkedQueue.LinkedIterator(); + } + + private class LinkedIterator implements Iterator + { + private LinkedQueue.Node current = head; + + /** + * Returns true if the iteration has more elements. + * 0(1) or constant time in the worst case + * return is the same + * @return true if the iteration has more elements + */ + @Override + public boolean hasNext() + { + return current != null; + } + + /** + * Returns the next element in the iteration + * 0(1) or constant time in the worst case + * because no matter size operations are the same + * + * @return the next Item + */ + @Override + public Item next() + { + Item item = (Item) current.item; + current = current.next; + return item; + } + + /** + * Left Blank? + */ + @Override + public void remove() + { + + } + + } + //added by java + @Override + public Item peek() + { + return head.item; + } + + @Override + public boolean contains(Object o) + { + return false; + } + @Override + public boolean add(Item item) + { + return false; + } + + @Override + public boolean remove(Object o) + { + return false; + } + + @Override + public boolean containsAll(Collection c) + { + return false; + } + + @Override + public boolean addAll(Collection c) + { + return false; + } + + @Override + public boolean removeAll(Collection c) + { + return false; + } + + @Override + public boolean retainAll(Collection c) + { + return false; + } + + @Override + public void clear() + { + + } + + @Override + public boolean offer(Item item) + { + return false; + } + + @Override + public Item remove() + { + return null; + } + + @Override + public Item poll() + { + return null; + } + + @Override + public Item element() + { + return null; + } + + @Override + public Object[] toArray() + { + return new Object[0]; + } + + @Override + public T[] toArray(T[] a) + { + return null; + } + + +} diff --git a/src/stacks/LinkedStack.java b/src/stacks/LinkedStack.java new file mode 100644 index 0000000..a56efb9 --- /dev/null +++ b/src/stacks/LinkedStack.java @@ -0,0 +1,130 @@ +/** + * LinkedList implementation of the Stack interface + * + * @author Lillian Nelson + * @version 1.0 + */ + +package stacks; +import java.util.Iterator; +import java.util.Stack; + +public class LinkedStack extends Stack +{ + // private fields + private Node head; // top of stack (most recently added node) + private int size; // number of items + + private class Node + { // nested class to define nodes + Item item; + Node next; + } + + /** + * Returns if LinkedStack is empty + * 0(1) or constant time in the worst case + * no matter size operations are the same + * @return if head is null + */ + public boolean isEmpty() + { + return head == null; + } + + /** + * Returns size of LinkedStack + * 0(1) or constant time in the worst case + * no matter size the return is same + * + * @return size + */ + public int size() + { + return size; + } + + /** + * pushes a new item into the LinkedStack + * 0(1) or constant time in the worst case + * no matter size operations are the same + * + * @param item Item to push + * @return + */ + public Item push(Item item) + { // Add item to top of stack + Node oldFirst = head; + head = new Node(); + head.item = item; + head.next = oldFirst; + size++; + return item; + } + + /** + * Removes an item from the stack + * 0(1) or constant time in the worst case + * no matter size operations stay the same + * + * @return the item removed + */ + public Item pop() + { // Remove item from top of stack + Item item = head.item; + head = head.next; + size--; + return item; + } + + /** + * Returns an iterator over elements of type Item + * 0(1) constant time returning iterator + * + * @return an Iterator. + */ + public Iterator iterator() + { + return new LinkedIterator(); + } + private class LinkedIterator implements Iterator + { + private LinkedStack.Node current = head; + + /** + * Returns true if the iteration has more elements. + * 0(1) or constant time in the worst case + * return is the same + * @return true if the iteration has more elements + */ + @Override + public boolean hasNext() + { + return current != null; + } + + /** + * Returns the next element in the iteration + * 0(1) or constant time in the worst case + * because no matter size operations are the same + * + * @return the next Item + */ + @Override + public Item next() + { + Item item = (Item) current.item; + current = current.next; + return item; + } + + /** + * Left Blank? + */ + @Override + public void remove() + { + + } + } +} diff --git a/src/stacks/ResizingArrayStack.java b/src/stacks/ResizingArrayStack.java new file mode 100644 index 0000000..928bb25 --- /dev/null +++ b/src/stacks/ResizingArrayStack.java @@ -0,0 +1,134 @@ +/** + * Array implementation of the Stack interface + * + * @author Lillian Nelson + * @version 1.0 + */ +package stacks; +import java.util.Iterator; +import java.util.Stack; + +public class ResizingArrayStack extends Stack +{ + private Item[] array; //stack items + private int size; //number of items + + public ResizingArrayStack() + { + array = (Item[]) new Object[10]; + size = 0; + } + + /** + * Returns if ArrayStack is empty + * 0(1) or constant time in the worst case + * no matter size operations are the same + * + * @return if size is 0 + */ + public boolean isEmpty() + { + return size == 0; + } + + /** + * Returns size of ArrayStack + * 0(1) or constant time in the worst case + * no matter size operations are the same + * + * @return size + */ + public int size() + { + return size; + } + + /** + * Creates a new array to resize + * 0(n) linear time in the worst case + * loops through the array + * + * */ + private void resize() + { //move stack to a new array of size max + Item[] temp = (Item[]) new Object[2 * array.length]; + for (int i = 0; i < size; i++) + { + temp[i] = array[i]; + } + array = temp; + } + + /** + * pushes a new item into the array stack + * 0(1) or constant time in the worst case + * no matter size operations are the same + * + * @param item Item to push + * @return + */ + + public Item push(Item item) + { //add item to top of stack + if (size == array.length) + { + resize(); + } + array[size] = item; + size++; + return item; + } + + /** + * Removes an item from the stack + * 0(1) or constant time in the worst case + * no matter size operations stay the same + * + * @return the item removed + */ + + public Item pop() + { //remove item from top of stack + if (isEmpty()) + { + throw new IndexOutOfBoundsException(0); + } + Item item = array[size - 1]; + array[size - 1] = null; // avoid loitering + size--; + return item; + } + + /** + * Returns an iterator over elements of type Item + * 0(1) constant time returning iterator + * + * @return an Iterator. + */ + public Iterator iterator() + { + return new ResizingArrayIterator(); + } + + private class ResizingArrayIterator implements Iterator + { //support LIFO iteration + private int i = size; + + /** + * Returns true if the iteration has more elements. + * 0(1) or constant time in the worst case + * @return true if the iteration has more elements + */ + @Override + public boolean hasNext() { return i > 0; } + + /** + * Returns the next element in the iteration. + * 0(1) or constant time in the worst case + * @return the next element in the iteration + */ + public Item next() { return array[--i]; } + + } + +} diff --git a/src/test/ArrayListTest.java b/src/test/ArrayListTest.java new file mode 100644 index 0000000..2548a78 --- /dev/null +++ b/src/test/ArrayListTest.java @@ -0,0 +1,151 @@ +package test; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +public class ArrayListTest { + + @Test + void addFront() + { + lists.ArrayList array = new lists.ArrayList<>(); + + for (int i = 0; i < 10; i++) + { + array.addFront(i); + assertEquals(array.get(0), i); + } + + } + + @Test + void addBack() + { + lists.ArrayList array = new lists.ArrayList<>(); + + for (int i = 1; i < 10; i++) + { + array.addBack(i); + assertEquals(array.get(array.size() - 1), i); + } + + } + + @Test + void add() + { + lists.ArrayList array = new lists.ArrayList<>(); + for (int i = 1; i < 10; i++) + { + array.add(i, i+1); + assertEquals(array.get(i), i+1); + } + + } + + @Test + void removeFront() { + lists.ArrayList array = new lists.ArrayList<>(); + + array.addFront(0); + array.addFront(1); + array.removeFront(); + assertEquals(array.size(), 1); + + + } + + @Test + void removeBack() { + lists.ArrayList array = new lists.ArrayList<>(); + + array.addFront(0); + array.addFront(7); + array.removeBack(); + assertEquals(array.size(), 1); + } + + @Test + void remove() { + lists.ArrayList array = new lists.ArrayList<>(); + + array.addFront(1); + array.addFront(2); + array.addFront(3); + array.remove(1); + assertEquals(array.size(), 2); + + } + + @Test + void get() + { + lists.ArrayList array = new lists.ArrayList<>(); + + for (int i = 0; i < 10; i++) + { + array.addFront(i); + assertEquals(array.get(0), i); + } + } + + @Test + void contains() { + lists.ArrayList array = new lists.ArrayList<>(); + + assertFalse(array.contains(4)); + + array.addFront(3); + assertTrue(array.contains(3)); + assertFalse(array.contains(7)); + + } + + @Test + void indexOf() + { + lists.ArrayList array = new lists.ArrayList<>(); + + array.addFront(1); + array.indexOf(1); + } + + @Test + void size() + { + lists.ArrayList array = new lists.ArrayList<>(); + + for (int i = 1; i < 10; i++) + { + array.addBack(i); + assertEquals(array.size(), i); + } + } + @Test + void isEmpty() { + lists.ArrayList array = new lists.ArrayList<>(); + + assertTrue(array.isEmpty()); + + for (int i = 0; i < 10; i++) + { + array.addBack(i); + assertFalse(array.isEmpty()); + } + } + + + @Test + void clear() + { + lists.ArrayList array = new lists.ArrayList<>(); + //not sure how to test + + for (int i = 0; i < 10; i++) + { + array.addBack(i); + + } + array.clear(); + } + +} \ No newline at end of file diff --git a/src/test/LinkedListTest.java b/src/test/LinkedListTest.java new file mode 100644 index 0000000..a07b2da --- /dev/null +++ b/src/test/LinkedListTest.java @@ -0,0 +1,150 @@ +package test; + +import lists.LinkedList; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class LinkedListTest +{ + @Test + void addFront() + { + LinkedList list = new LinkedList<>(); + + for (int i = 0; i < 10; i++) { + list.addFront(i); + assertEquals(list.get(0), i); + } + } + @Test + void addBack() + { + LinkedList list = new LinkedList<>(); + + for (int i = 1; i < 10; i++) + { + list.addBack(i); + assertEquals(list.get(list.size() - 1), i); + } + } + + @Test + void add() + { + //need to fix add method +// LinkedList list = new LinkedList<>(); +// for (int i = 1; i < 10; i++) +// { +// list.add(i, i+1); +// assertEquals(list.get(i), i+1); +// } + + } + @Test + void removeFront() { + LinkedList list = new LinkedList<>(); + + list.addFront(0); + list.addFront(1); + list.removeFront(); + assertEquals(list.size(), 1); + + + } + @Test + void removeBack() { + LinkedList list = new LinkedList<>(); + + list.addFront(0); + list.addFront(7); + list.removeBack(); + assertEquals(list.size(), 2); + } + @Test + void remove() { + LinkedList list = new LinkedList<>(); + + list.addFront(1); + list.addFront(2); + list.addFront(3); + list.remove(1); + assertEquals(list.size(), 2); + + } + @Test + void get() + { + LinkedList list = new LinkedList<>(); + + for (int i = 0; i < 10; i++) + { + list.addFront(i); + assertEquals(list.get(0), i); + } + } + + @Test + void contains() + { + LinkedList list = new LinkedList<>(); + + assertFalse(list.contains(4)); + + list.addBack(3); + assertFalse(list.contains(7)); + + } + + @Test + void indexOf() + { + LinkedList list = new LinkedList<>(); + + list.addFront(1); + list.indexOf(1); + } + + @Test + void size() + { + LinkedList list = new LinkedList<>(); + + for (int i = 1; i < 10; i++) + { + list.addBack(i); + assertEquals(list.size(), i); + } + } + + @Test + void isEmpty() + { + LinkedList list = new LinkedList<>(); + + assertTrue(list.isEmpty()); + + for (int i = 0; i < 10; i++) + { + list.addBack(i); + assertFalse(list.isEmpty()); + } + } + + + @Test + void clear() + { + LinkedList list = new LinkedList<>(); + //not sure how to test + + for (int i = 0; i < 10; i++) + { + list.addBack(i); + + } + list.clear(); + } + // not sure if how to test iterator method + +} \ No newline at end of file diff --git a/src/test/QueueTestClient.java b/src/test/QueueTestClient.java new file mode 100644 index 0000000..813310d --- /dev/null +++ b/src/test/QueueTestClient.java @@ -0,0 +1,32 @@ +package test; /** + * Test client for the LinkedStack + * + * @author Lillian Nelson + * @version 1.0 + */ + +import lists.LinkedQueue; + +import java.util.Scanner; + +public class QueueTestClient +{ + public static void main(String[] args) { + LinkedQueue q = new LinkedQueue<>(); + Scanner in = new Scanner("to be or not to - be - - that - - - is"); + + while (in.hasNext()) + { + String item = in.next(); + if (!item.equals("-")) + { + q.enqueue(item); + } + else if (!q.isEmpty()) + { + System.out.print(q.dequeue() + " "); + } + } + System.out.println("(" + q.size() + " left on the queue)"); + } +} diff --git a/src/test/StackTestClient.java b/src/test/StackTestClient.java new file mode 100644 index 0000000..efc6153 --- /dev/null +++ b/src/test/StackTestClient.java @@ -0,0 +1,32 @@ +package test; /** + * Test client for the LinkedStack + * + * @author Lillian Nelson + * @version 1.0 + */ + +import stacks.LinkedStack; +import stacks.ResizingArrayStack; + +import java.util.Scanner; + +public class StackTestClient +{ + public static void main(String[] args) { + //ResizingArrayStack s = new ResizingArrayStack(); + LinkedStack s = new LinkedStack<>(); + + Scanner in = new Scanner("to be or not to - be - - that - - - is"); + while (in.hasNext()) { + String item = in.next(); + if (item.equals("-")) + { + s.push("item"); + } + else if (!s.isEmpty()) { + System.out.println("s.pop()" + " "); + } + } + System.out.println("(" + s.size() + " left on the stack"); + } +} diff --git a/src/test/Stats.java b/src/test/Stats.java new file mode 100644 index 0000000..d414102 --- /dev/null +++ b/src/test/Stats.java @@ -0,0 +1,29 @@ +/** + * Test client for LinkedBag + * + * @author Lillian Nelson + * @version 1.0 + */ +package test; +import lists.LinkedBag; + +import java.util.Scanner; + +public class Stats +{ + public static void main(String[] args) + { + LinkedBag numbers = new LinkedBag(); + Scanner in = new Scanner("100 99 101 107 109 81 101 90"); + while(in.hasNextLine()) + { + numbers.add(in.nextDouble()); + + } + for (Double n : numbers) + { + System.out.println(n); + } + + } +}