Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

all 3 intList implementations #22

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
69f4791
Added started code, finish some of the easy methods
kayleyyoung253 Jan 9, 2024
098b848
Completed the remove(index) method
kayleyyoung253 Jan 9, 2024
aea0417
Started DoublyLinkedIntList, filled in private fields, constructor, a…
kayleyyoung253 Jan 16, 2024
61cd5c2
Started test file for ArrayIntList to test all methods
kayleyyoung253 Jan 23, 2024
af05fb1
Completed Get, contains, indexOf, isEmpty, and size methods.
kayleyyoung253 Jan 23, 2024
d0b7971
Completed tests for all methods in ArrayIntList.java
kayleyyoung253 Jan 24, 2024
15614b7
Corrected isEmpty() method
kayleyyoung253 Jan 24, 2024
7c78c9d
Added header comment
kayleyyoung253 Jan 25, 2024
fe7fa88
Completed add,addFront,and addBack method
kayleyyoung253 Jan 25, 2024
7fdbfb9
Completed clear,size,isEmpty,indexOf and the rest of the methods
kayleyyoung253 Jan 26, 2024
af79618
Completed all methods in DoublyLinkedIntList.java, including Add, add…
kayleyyoung253 Jan 26, 2024
1c976bf
Completed junit tests for all methods in DoublyLinkedIntList.java
kayleyyoung253 Jan 26, 2024
f102dd9
corrected out of bounds exception for add() method
kayleyyoung253 Jan 26, 2024
aa87fe4
corrected addfront method
kayleyyoung253 Jan 26, 2024
94c51ca
created junit tests for all methods
kayleyyoung253 Jan 26, 2024
2b6231d
corrected methods that did not pass juint tests
kayleyyoung253 Jan 26, 2024
cb83115
used to further test methods not passing juint tests
kayleyyoung253 Jan 26, 2024
379f3db
added iml file
kayleyyoung253 Jan 26, 2024
19abf4e
ignore
kayleyyoung253 Jan 26, 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
1 change: 1 addition & 0 deletions .idea/.gitignore

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

1 change: 0 additions & 1 deletion .idea/misc.xml

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

27 changes: 27 additions & 0 deletions IntListReview.iml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,35 @@
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library" scope="TEST">
<library name="JUnit4">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.13.1/junit-4.13.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<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>
293 changes: 293 additions & 0 deletions src/ArrayIntList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,293 @@
import java.util.Iterator;
import java.util.NoSuchElementException;
/*
Kayley Young

resize an array implementation of the IntList interface
*/
public class ArrayIntList implements IntList {

//fields
private int size;
private int[] buffer;

public ArrayIntList() {
//initialize my fields
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--) {
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) {
if (size == buffer.length){
resize(size + 2);
}
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){
throw new IndexOutOfBoundsException("Index cannot be negative");
}
if (size == buffer.length){
resize(size + 2);
}

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(!isEmpty()) {
for (int i = 0; i <= size - 2; i++) {
buffer[i] = buffer[i + 1];
}
//zero out the far right since everything was shifted to the left
buffer[size -1] = 0;
size--;
}else{
throw new IndexOutOfBoundsException("Array is empty, nothing to remove");
}
}

/**
* Removes the value located at the back of the list
* (at index size()-1), if it is present.
*/
@Override
public void removeBack() {
if (!isEmpty()){
buffer[size-1] = 0;
size--;
}else{
throw new IndexOutOfBoundsException("Array is empty, nothing to remove");
}
}

/**
* 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){
throw new IndexOutOfBoundsException("Index cannot be negative");
}
else if (index >= size){
throw new IndexOutOfBoundsException("Index is higher than size");
}

//save a copy of the value to be removed so we can return it later
int copyOfRemovedValue = buffer[index];

//shift values to the left
for (int i = index; i <= size -1; i++){
buffer[i] = buffer[i+1];
}
buffer[size - 1] = 0;
// don't forget to decrement size
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) {
if(index < 0){
throw new IndexOutOfBoundsException("Index cannot be negative");
}
else if (index >= size){
throw new IndexOutOfBoundsException("Index is higher than size");
}

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 < buffer.length; 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 < buffer.length; 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;
}

private void resize(int newSize){
//create new space, separate from the old space (buffer)
int[] newBuffer = new int[newSize];

//copy everything over from buffer into newBuffer
for (int i = 0; i < buffer.length; i++) {
newBuffer[i] = buffer[i];
}
//
buffer = newBuffer;

// the old space is no longer "pointed to" and will eventually
// be cleaned up by the garbage collector
}
/**
* Returns an iterator over elements of type {@code T}.
*
* @return an Iterator.
*/
@Override
public Iterator<Integer> iterator() {
return new IntListIterator();
}


//create a private helper
private class IntListIterator implements Iterator<Integer> {

private int i;
private void IntListIterator(){
i = 0;
}


/**
* Returns {@code true} if the iteration has more elements.
* (In other words, returns {@code true} if {@link #next} would
* return an element rather than throwing an exception.)
*
* @return {@code 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 Integer next() {
if (i > size){
throw new NoSuchElementException("i is now out of bounds");
}
int currentValue = buffer[i];
i++;
return currentValue;
}

}

}

Loading