Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
6739797
Implementing functions in ArrayIntList.java
Rob-P-Smith Jan 9, 2024
8b043a9
Implementing functions in ArrayIntList.java v2
Rob-P-Smith Jan 9, 2024
c86c340
Implementing functions in ArrayIntList.java, added iterator sub-class.
Rob-P-Smith Jan 9, 2024
a3e0e8f
Added Doubly Linked List
Rob-P-Smith Jan 16, 2024
4df1e73
Implementing methods.
Rob-P-Smith Jan 19, 2024
85c5584
Finished ArrayIntList class and added test shell.
Rob-P-Smith Jan 19, 2024
46e9cae
Working through fence post error.
Rob-P-Smith Jan 20, 2024
e643270
Working tests, all pass.
Rob-P-Smith Jan 20, 2024
89a69a7
ArrayIntList implemented fully with custom to string. Tests all items…
Rob-P-Smith Jan 20, 2024
134dd47
ArrayIntList implemented fully with custom to string. Tests all items…
Rob-P-Smith Jan 20, 2024
7ca4572
ArrayIntList implemented fully with custom to string. Tests all items…
Rob-P-Smith Jan 20, 2024
52b9d38
Updated tests to account for edge cases of throwing exception when th…
Rob-P-Smith Jan 20, 2024
823d3e3
Updated tests to account for edge cases of throwing exception when th…
Rob-P-Smith Jan 20, 2024
36e2f5f
Updated tests to account for edge cases of throwing exception when th…
Rob-P-Smith Jan 20, 2024
36ba8e1
Starting on LinkedList Class
Rob-P-Smith Jan 20, 2024
8936fed
Working on Linked List implementation. Added addBack() and toString()
Rob-P-Smith Jan 20, 2024
cc4652f
Merge remote-tracking branch 'origin/main'
Rob-P-Smith Jan 21, 2024
0da8780
Finished Contains, indexOf, get, size, clear
Rob-P-Smith Jan 21, 2024
c3efa55
Finished remove()
Rob-P-Smith Jan 21, 2024
5d0c834
Finished add()
Rob-P-Smith Jan 21, 2024
7930d14
Rebuilding the Tests and Lost Work...
Rob-P-Smith Jan 21, 2024
75f26cf
Working on DoublyLinkedLIst implementation..again
Rob-P-Smith Jan 21, 2024
05b7343
Added indexOf() and contains()
Rob-P-Smith Jan 22, 2024
fcdb55e
All Complete, pending debugging check and refactor.
Rob-P-Smith Jan 22, 2024
f10b0e6
All Complete.
Rob-P-Smith Jan 22, 2024
4a0786e
Refining my code a bit.
Rob-P-Smith Jan 22, 2024
29b4bf3
Converting applicable methods to recursive methods instead of iterative.
Rob-P-Smith Jan 23, 2024
c0ef879
Adding recursive add at index, in progress.
Rob-P-Smith Jan 23, 2024
76c20aa
Adding recursive add at index, in progress. Disabled and re-enabled i…
Rob-P-Smith Jan 23, 2024
a4d9cfc
Fixing iterator sub class.
Rob-P-Smith Jan 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions IntListReview.iml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,25 @@
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library" scope="TEST">
<library name="JUnit5.8.1">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.8.1/junit-jupiter-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.8.1/junit-jupiter-api-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.8.1/junit-platform-commons-1.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.8.1/junit-jupiter-params-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.8.1/junit-jupiter-engine-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.8.1/junit-platform-engine-1.8.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
287 changes: 287 additions & 0 deletions src/ArrayIntList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,287 @@
import java.util.Iterator;
import java.util.NoSuchElementException;

/**
* Implementation of an arraylist using hte IntList interface for 333 week 1-3
* @author Rob Smith
* @version 1.0
*/
public class ArrayIntList implements IntList {

private int size;
private int[] buffer;

public ArrayIntList() {
this.size = 0;
this.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) {
if (size != 0) {
doubleSize();
for (int i = size; i >= 1; i--) {
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) {
doubleSize();
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) {
doubleSize();
if (index < 0 || index > size || size==0) {
throw new IndexOutOfBoundsException("Index ouf of bounds. NOPE!");
} else {
for (int i = size; i > index; i--) {
buffer[i] = buffer[i - 1];
}
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) {
throw new NoSuchElementException("Cannot remove element from empty array");
} else {
for (int i = 0; i < size; i++) {
buffer[i] = buffer[i + 1];
}
}
buffer[size] = 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 NoSuchElementException("Cannot remove element from empty array");
} else {
buffer[size - 1] = 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) {
int value = 0;
if (index < size - 1 && index >= 0) {
value = buffer[index];
for (int i = index; i < size; i++) {
buffer[i] = buffer[i + 1];
}
buffer[size] = 0;
size--;
} else if (index == size - 1) {
value = buffer[index];
buffer[index] = 0;
size--;
} else if (index < 0) {
throw new IndexOutOfBoundsException("Index cannot be less than 0.");
} else if (index > size) {
throw new IndexOutOfBoundsException("Index cannot be greater than array length");
}
return value;
}

/**
* 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) {
return buffer[index];
} else {
throw new IndexOutOfBoundsException("Index cannot be outside the size of the array.");
}
}

/**
* 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) {
int index = -1;
for (int i = 0; i < size; i++) {
if (buffer[i] == value) {
index = i;
return index;
}
}
return index;
}

/**
* 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() {
int length = buffer.length;
int[] buffer = new int[length];
size = 0;
}

/**
* Helper method that doubles the array size using the resize method.
*/
private void doubleSize() {
if (size == buffer.length) {
resize(buffer.length * 2);
}
}

/**
* Grows and Shrinks the array as required by data size.
*/
private void resize(int newSize) {
int[] newBuffer = new int[newSize];
System.arraycopy(buffer, 0, newBuffer, 0, buffer.length);
buffer = newBuffer;
}

/**
* Custom toString for this arraylist implementation that returns a pretty print display of the contents
* @return a string of the contents of the arraylist
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append('[');
if (size > 0) {
for (int i = 0; i < size-1; i++){
sb.append(buffer[i]);
sb.append(',');
sb.append(' ');
}
sb.append(buffer[size-1]);
sb.append(']');
} else {
return "null";
}
return sb.toString();
}

/**
* Returns an iterator over elements of type {@code T}.
*
* @return an Iterator.
*/
@Override
public Iterator<Integer> iterator() {
return new IntListIterator();
}

private class IntListIterator implements Iterator<Integer> {
private int i;

private IntListIterator() {
this.i = 0;
}

public boolean hasNext() {
return i < size;
}

public Integer next() {
int currentValue = buffer[i];
i++;
return currentValue;
}
}
}
Loading