diff --git a/.idea/misc.xml b/.idea/misc.xml index 6f29fee..ea71d50 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/src/ArrayIntList.java b/src/ArrayIntList.java new file mode 100644 index 0000000..1d47aea --- /dev/null +++ b/src/ArrayIntList.java @@ -0,0 +1,296 @@ +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class ArrayIntList implements IntList{ + + // internal (private) representation + private int[] buffer; + private int size; + private final static int INITIAL_CAPACITY = 10; + + public ArrayIntList(){ + buffer = new int[INITIAL_CAPACITY]; + size = 0; + } + + + /** + * Prepends (inserts) the specified value at the front of the list (at index 0). + * Shifts the value currently at the front of the list (if any) and any + * subsequent values to the right. + * + * @param value value to be inserted + */ + @Override + public void addFront(int value) { + // check to see if we still have room (capacity) + if (size == buffer.length){ + resize(2 * buffer.length); + } + + // open spot at index 0 where value will be saved + // shift everything over to the right by 1 position + for (int i = size; i >= 1; i--) { + buffer[i] = buffer[i - 1]; + } + + // put value in position [0] + buffer[0] = value; + + } + + /** + * Appends (inserts) the specified value at the back of the list (at index size()-1). + * + * @param value value to be inserted + */ + @Override + public void addBack(int value) { + // check to see if we still have room (capacity) + if (size == buffer.length){ + // make the newSize twice the existing capacity + resize(2 * buffer.length); + } + + buffer[size] = value; + 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. + * + * @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 + */ + @Override + public void add(int index, int value) { + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException("Index out of range"); + } + + // shift elements to the right + for (int i = size; i > index ; i--) { + buffer[i] = buffer[i - 1]; + } + + // insert new value + buffer[index] = value; + size++; + } + + /** + * Removes the value located at the front of the list + * (at index 0), if it is present. + * Shifts any subsequent values to the left. + */ + @Override + public void removeFront() { + if(size > 0) { + for (int i = 0; i < size - 1; i++) { + buffer[i] = buffer[i + 1]; + } + + buffer[size - 1] = 0; + size--; + } + + } + + /** + * Removes the value located at the back of the list + * (at index size()-1), if it is present. + */ + @Override + public void removeBack() { + if(size == 0){ + throw new IllegalStateException("Already Empty!"); + } + // size--; + buffer[--size] = 0; + + } + + /** + * 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. + * + * @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 int remove(int index) { + if (index < 0 || index > size){ + throw new IndexOutOfBoundsException("Index is out of range"); + } + + int removedValue = buffer[index]; + + // shift elements to the left + for (int i = index; i < size; i++) { + buffer[i] = buffer[i + 1]; + } + + // clear the last element and decrement size + buffer[size - 1] = 0; + size--; + + return removedValue; + } + + /** + * Returns the value at the specified position in the list. + * + * @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 int get(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Index out of range"); + } + + return buffer[index]; + } + + /** + * Returns true if this list contains the specified value. + * + * @param value value whose presence in this list is to be searched for + * @return true if this list contains the specified value + */ + @Override + public boolean contains(int 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. + * + * @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 + */ + @Override + public int indexOf(int value) { + for (int i = 0; i < size; i++) { + if (buffer[i] == value) { + return i; + } + } + return -1; + } + + /** + * Returns true if this list contains no values. + * + * @return true if this list contains no values + */ + @Override + public boolean isEmpty() { + return size == 0; + } + + /** + * Returns the number of values in this list. + * + * @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. + */ + @Override + public void clear() { + buffer = new int[10]; + size = 0; + + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + // return new instance of the helper class + return new ArrayIntListIterator(); + } + + public class ArrayIntListIterator implements Iterator { + private int currentPosition; + + public ArrayIntListIterator(){ + currentPosition = 0; + } + + @Override + public boolean hasNext() { + if (currentPosition< size()){ + return true; + } + else { + return false; + } + } + + public Integer next(){ + if (!hasNext()){ + throw new NoSuchElementException(); + } + int value = get(currentPosition); + currentPosition++; + return value; + } + } + + private void resize(int newSize){ + // new array that is the new size + int[] temp = new int[newSize]; + + // copy over values from existing buffer + for (int i = 0; i < size; i++) { + temp[i] = buffer[i]; + } + + // make the switchover + buffer = temp; + } + + @Override + public String toString(){ + if (size ==0){ + return "[]"; + } + StringBuilder sb = new StringBuilder(); + sb.append("["); + sb.append(buffer[0]); + + for (int i = 1; i < size; i++) { + sb.append(", "); + sb.append(buffer[i]); + } + + sb.append("]"); + return sb.toString(); + + } +} diff --git a/src/LinkedIntList.java b/src/LinkedIntList.java new file mode 100644 index 0000000..7ab3610 --- /dev/null +++ b/src/LinkedIntList.java @@ -0,0 +1,305 @@ +import java.util.Iterator; +public class LinkedIntList implements IntList{ + // helper inner/nested class + public class Node{ + int data; // holds the data value + Node next; // holds address of next node + + public Node(){ + data = 0; + next = null; + } + + public Node(int data, Node next){ + this.data = data; + this.next = next; + } + } // end of class Node + + // fields for LinkedIntList class + private Node head; // address of first node in list + private int size; + + public LinkedIntList(){ + head = null; + size = 0; + } + + + /** + * Prepends (inserts) the specified value at the front of the list (at index 0). + * Shifts the value currently at the front of the list (if any) and any + * subsequent values to the right. + * + * @param value value to be inserted + */ + @Override + public void addFront(int value) { + // if list is empty + if (head == null){ + head = new Node(value, null); + } + // if list not empty + else { + head = new Node(value, head); + } + + size++; + } + + /** + * Appends (inserts) the specified value at the back of the list (at index size()-1). + * + * @param value value to be inserted + */ + @Override + public void addBack(int value) { + // if list is empty + if (head == null) { + head = new Node(value, null); + } else { + Node current = head; + // Traverse to the last node + while (current.next != null) { + current = current.next; + } + // Add the new node at the end + current.next = new Node(value, null); + } + + 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. + * + * @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 + */ + @Override + public void add(int index, int value) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Index is out of range"); + } + + // If adding at the front, use addFront method + if (index == 0) { + addFront(value); + } else { + Node newNode = new Node(value, null); + Node current = head; + + // Traverse to the node before the specified index + for (int i = 0; i < index - 1; i++) { + current = current.next; + } + + // Insert the new node in between + newNode.next = current.next; + current.next = newNode; + + size++; + } + } + + /** + * Removes the value located at the front of the list + * (at index 0), if it is present. + * Shifts any subsequent values to the left. + */ + @Override + public void removeFront() { + if (head != null) { + head = head.next; + size--; + } + } + + /** + * Removes the value located at the back of the list + * (at index size()-1), if it is present. + */ + @Override + public void removeBack() { + if (head != null) { + // If there's only one node, remove it + if (head.next == null) { + head = null; + } else { + Node current = head; + // Traverse to the second-to-last node + while (current.next.next != null) { + current = current.next; + } + // Remove the last node + current.next = null; + } + + size--; + } + } + + /** + * 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. + * + * @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 int remove(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index is out of range"); + } + + int removedValue; + + // If removing at the front, use removeFront method + if (index == 0) { + removedValue = head.data; + removeFront(); + } else { + Node current = head; + // Traverse to the node before the specified index + for (int i = 0; i < index - 1; i++) { + current = current.next; + } + // Get the value to be removed + removedValue = current.next.data; + // Remove the node + current.next = current.next.next; + size--; + } + + return removedValue; + } + + /** + * Returns the value at the specified position in the list. + * + * @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 int get(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index is out of range"); + } + + Node current = head; + // Traverse to the specified index + for (int i = 0; i < index; i++) { + current = current.next; + } + + return current.data; + } + + /** + * Returns true if this list contains the specified value. + * + * @param value value whose presence in this list is to be searched for + * @return true if this list contains the specified value + */ + @Override + public boolean contains(int value) { + Node current = head; + // Traverse the list to find the value + while (current != 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. + * + * @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 + */ + @Override + public int indexOf(int value) { + Node current = head; + // Traverse the list to find the index of the value + for (int i = 0; i < size; i++) { + if (current.data == value) { + return i; + } + current = current.next; + } + return -1; + } + + /** + * Returns true if this list contains no values. + * + * @return true if this list contains no values + */ + @Override + public boolean isEmpty() { + return size == 0; + } + + /** + * Returns the number of values in this list. + * + * @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. + */ + @Override + public void clear() { + head = null; + size = 0; + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return null; + } + + public String toString(){ + if (head == null){ + return "[]"; + } + + StringBuilder sb = new StringBuilder(); + sb.append("["); + + Node current = head; + while (current.next != null){ + sb.append(current.data); + sb.append(", "); + + current = current.next; + } + sb.append(current.data); + sb.append("]"); + return sb.toString(); + } + +} diff --git a/src/Main.java b/src/Main.java index 930198c..36c32d4 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,15 +1,103 @@ -//TIP To Run code, press or -// click the icon in the gutter. +import java.util.Iterator; + public class Main { public static void main(String[] args) { - //TIP Press with your caret at the highlighted text - // to see how IntelliJ IDEA suggests fixing it. - System.out.printf("Hello and welcome!"); + // instance of ArrayIntList + IntList list1 = new ArrayIntList(); + IntList list2 = new LinkedIntList(); + + // test add method + list1.add(0, 44); + list1.add(1, 77); + list1.add(2, 84); + + // add 3 ints to the front of the list + list1.addFront(67); + list1.addFront(33); + list1.addFront(26); + + // add 3 ints to the back of the list + list1.addBack(42); + list1.addBack(82); + list1.addBack(97); + + // print the list + System.out.println("List after adding elements: " + list1); + + // test remove back + list1.removeBack(); + + // test remove front + list1.removeFront(); + + // test remove int at index specified + list1.remove(2); + list1.remove(0); + list1.remove(1); + + // print the list + System.out.println("List after removing elements: " + list1); + + // test get method + int valueAtIndex1 = list1.get(1); + System.out.println("Value at Index 1: " + valueAtIndex1); + + // test contains method + boolean contains97 = list1.contains(97); + System.out.println("Contains 97? " + contains97); + + // test indexOf method + int indexOf26 = list1.indexOf(26); + System.out.println("Index of 26? " + indexOf26); + + // test isEmpty method + boolean isEmpty = list1.isEmpty(); + System.out.println("Is the list empty? " +isEmpty); + + // test size method + int listSize = list1.size(); + System.out.println("Size of the list: " + listSize); + + // test clear method + list1.clear(); + System.out.println("List after clearing: " + list1); - for (int i = 1; i <= 5; i++) { - //TIP Press to start debugging your code. We have set one breakpoint - // for you, but you can always add more by pressing . - System.out.println("i = " + i); + + for (int value : list1) { + System.out.println(value); + } + + System.out.println("-----------"); + + Iterator itr = list1.iterator(); + while (itr.hasNext()){ + int value = itr.next(); + System.out.println(value); } + + + // Linked List short test + + System.out.println("------------------------"); + + list2.addFront(9); + list2.addFront(7); + list2.addFront(4); + list2.addBack(18); + + System.out.println(list2); + + // short way: + for (int value : list2){ + System.out.println(value); + } + + // long way + Iterator itr2 = list2.iterator(); + while (itr2.hasNext()){ + System.out.println(itr2.next()); + } + + } } \ No newline at end of file