diff --git a/src/ArrayIntList.java b/src/ArrayIntList.java new file mode 100644 index 0000000..f9ef93f --- /dev/null +++ b/src/ArrayIntList.java @@ -0,0 +1,199 @@ +import java.util.Iterator; + +public class ArrayIntList implements IntList{ + + //fields + + private int size; + + private int[] buffer; + + public ArrayIntList() { + size = 0; + buffer = new int[10]; + } + + /** + * 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) { + for (int i = size; i >= 1; i--) { // count 5, 4, 3, 2, 1 + buffer[i] = buffer[i - 1]; + } + buffer[0] = value; + 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) { + //TODO check to see if we are full - if so, we need to create a larger buffer. + + if (size == buffer.length) { + resize(buffer.length * 2); + } + + + buffer[size + 1] = 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) { + + } + + /** + * 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() { + + } + + /** + * Removes the value located at the back of the list + * (at index size()-1), if it is present. + */ + @Override + public void removeBack() { + + } + + /** + * 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) { + //first check index if its valid + if (index < 0) { + throw new IndexOutOfBoundsException("Index cannot be negative"); + + } + else if (index >= size) { + throw new IndexOutOfBoundsException("Index out of range"); + } + + int copyOfRemovedValue = buffer[index]; + + for (int i = index; i < size - 1; i++) { + buffer[i] = buffer[i + 1]; + } + buffer[size - 1] = 0; + size--; + + return copyOfRemovedValue; + } + + /** + * 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) { + return 0; + } + + /** + * 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) { + 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) { + return 0; + } + + /** + * Returns true if this list contains no values. + * + * @return true if this list contains no values + */ + @Override + public boolean isEmpty() { + return false; + } + + /** + * Returns the number of values in this list. + * + * @return the number of values in this list + */ + @Override + public int size() { + return 0; + } + + /** + * 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; + } + + public void resize(int newSize) { + int[] newBuffer = new int[newSize]; + for (int i = 0; i < size; i++) { + newBuffer[i] = buffer[i]; + } + buffer = newBuffer; + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return null; + } + +} + diff --git a/src/DoublyLinkedIntList.java b/src/DoublyLinkedIntList.java new file mode 100644 index 0000000..d2b09c1 --- /dev/null +++ b/src/DoublyLinkedIntList.java @@ -0,0 +1,204 @@ +import java.util.Iterator; + +public class DoublyLinkedIntList implements IntList { +// private fields + + private class Node { + int data; + + Node next; // Address of the node "after" this one in line. + Node prev; // Address of the Node "before" this one in line. + + public Node() { + next = null; + prev = null; + } + } + + private Node pre; + private Node post; + private int size; + + public DoublyLinkedIntList() { + // an empty node that have two sentinel (dummy) nodes as bookends + pre = new Node(); + post = new Node(); + + pre.next = post; + post.prev = pre; + 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) { + //TODO by thursday 1/18 + } + + /** + * 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) { + Node theLastOne = post.prev; + + // Set up new Node and fill out (data, prev, next). + + Node theNewOne = new Node(); + theNewOne.data = value; + theNewOne.next = post; + theNewOne.prev = theLastOne; + + // Go to th end of the list sentinel ad update its rev + post.prev = theNewOne; + + // Go to the node before the new one, and update its next. + theLastOne.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. + * + * @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) { + + } + + /** + * 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() { + //TODO by thursday 1/18 + } + + /** + * 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) { + // Set up a temp variable for convenience. + Node theOneToRemove = post.prev; + + theOneToRemove.prev.next = post; + post.prev = theOneToRemove.prev; + + // Optional to clean up + theOneToRemove.next = null; + theOneToRemove.prev = null; + theOneToRemove.data = 0; + + 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) { + return 0; + } + + /** + * 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) { + return 0; + } + + /** + * 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) { + 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) { + return 0; + } + + /** + * Returns true if this list contains no values. + * + * @return true if this list contains no values + */ + @Override + public boolean isEmpty() { + return false; + } + + /** + * Returns the number of values in this list. + * + * @return the number of values in this list + */ + @Override + public int size() { + return 0; + } + + /** + * Removes all the values from this list. + * The list will be empty after this call returns. + */ + @Override + public void clear() { + + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return null; + } +} diff --git a/src/LinkedIntList.java b/src/LinkedIntList.java new file mode 100644 index 0000000..a5186c3 --- /dev/null +++ b/src/LinkedIntList.java @@ -0,0 +1,59 @@ +/** + * The LinkedIntList class represents a singly linked list of integer values. + * It provides methods to add elements to the front of the list. + * + * @author Braeodn Billingsley + */ +public class LinkedIntList { + private class Node { + int data; + Node next; + + } + + //set up the head Node; + private Node head; + + // set up the size field. + private int size; + + // add a constructor to initialize fields. + + /** + * The LinkedIntList class represents a singly linked list of integer values. + * It provides methods to add elements to the front and end of the list. + */ + public LinkedIntList() { + head = null; + size = 0; + } + + /** + * Adds an element to the front of the linked list. + * + * @param value the value to be added to the front of the list + */ + public void addFront(int value) { + // set up a new node. + Node newOne = new Node(); + if (head == null) { + // The list is currently empty + head = newOne; // set the new start of the linkedlist + size++; + + } else { + // The list currently has some nodes in it. + newOne.next = head; // link the new head to the old head to add to the original list. + head = newOne; // set head to the front of the list again. + } + } + + /** + * Adds an element to the end of the linked list. + * + * @param value the value to be added to the list + */ + public void addBack(int value) { + + } +} diff --git a/src/Main.java b/src/Main.java index 930198c..346c0be 100644 --- a/src/Main.java +++ b/src/Main.java @@ -10,6 +10,12 @@ public static void main(String[] args) { //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); + + IntList firstList; + + ArrayIntList secondList = new ArrayIntList(); + + IntList thirdList = new ArrayIntList(); } } } \ No newline at end of file