diff --git a/PA05-1004/ArrayDataEngine.c b/PA05-1004/ArrayDataEngine.c index 9e6eefd..bf5f7bb 100644 --- a/PA05-1004/ArrayDataEngine.c +++ b/PA05-1004/ArrayDataEngine.c @@ -62,16 +62,17 @@ Dependencies: privateCheckForResize, deepCopyData void copyDataList( StateArrayType *dest, const StateArrayType src ) { int copyIterator; - privateCheckForResize(dest); + dest->size = 0; // check for resize if the destination capacity is less than source if(dest->capacity == src.capacity) { - dest->size = src.size; + privateCheckForResize(dest); // if it is, then size the dest to what source was // copy the data from source to dest using deepCopy for(copyIterator = 0; copyIterator < src.size; copyIterator++) { deepCopyData(&dest->array[copyIterator], src.array[copyIterator]); + dest->size = dest->size + 1; } } // no return, void @@ -148,7 +149,8 @@ void displayData( const StateArrayType stateData, const char *subTitle ) // print subtitle (and new line) printf("%s\n", subTitle); // for loop (iterates through the array data) - for(displayIterator = 0; displayIterator < stateData.size; displayIterator++) + for(displayIterator = 0; displayIterator < stateData.size; + displayIterator++) { // print Name, printf("%s, ", stateData.array[displayIterator].name); @@ -239,25 +241,29 @@ bool insertItemAtIndex( StateArrayType *stateData, StateDataType toBeInserted, int insertIndex ) { int insertIterator; - // check for resize - privateCheckForResize(stateData); // check to see if index is within size - if(insertIndex < getListSize(*stateData) && insertIndex >= 0) + if(insertIndex <= getListSize(*stateData) && insertIndex >= 0) { + // check for resize + privateCheckForResize(stateData); // shift elements right of index to right - for(insertIterator = insertIndex; insertIterator < getListSize(*stateData); insertIterator++) + for(insertIterator = insertIndex; insertIterator < + getListSize(*stateData); insertIterator++) { // put the element into element + 1 - stateData->array[insertIterator + 1] = stateData->array[insertIterator]; + stateData->array[insertIterator + 1] = + stateData->array[insertIterator]; } // insert element at index stateData->array[insertIndex] = toBeInserted; + // increment the size by one + stateData->size = stateData->size + 1; // return true return true; } // return false return false; -} +} // Needs to iterate from top to bottom, whatever this means. /* Name: listIsEmpty @@ -271,14 +277,8 @@ Dependencies: none */ bool listIsEmpty( const StateArrayType array ) { - // check to see if size is equal to 0 - if(array.size == 0) - { - // return true since array empty - return true; - } - // return false if not empty - return false; + // return the check to see if the size is equal to 0 + return (array.size == 0); } /* @@ -305,12 +305,15 @@ void privateCheckForResize( StateArrayType *stateData ) // 'create' bigger array stateData->capacity = stateData->capacity * 2; // allocate the bigger array - biggerStateArray = (StateDataType *)malloc(sizeof(StateDataType) * stateData->capacity); + biggerStateArray = (StateDataType *)malloc(sizeof(StateDataType) * + stateData->capacity); // copy the new array stuff into the old space, use loop - for(resizeIterator = 0; resizeIterator < stateData->size; resizeIterator++) + for(resizeIterator = 0; resizeIterator < stateData->size; + resizeIterator++) { // use deep copy to copy the pieces of smaller array - deepCopyData(&biggerStateArray[resizeIterator], stateData->array[resizeIterator]); + deepCopyData(&biggerStateArray[resizeIterator], + stateData->array[resizeIterator]); } // free the old array free(stateData->array); @@ -341,8 +344,8 @@ void privateCopyString( char *dest, const char *src ) // iterate the number of characters characterCount = characterCount + 1; } - // add a NULL_CHAR once done. (destStringIterator + 1) - dest[characterCount + 1] = NULL_CHAR; + // add a NULL_CHAR once done, characterCount is in the right place for \0 + dest[characterCount] = NULL_CHAR; // no return, void } @@ -365,21 +368,25 @@ bool removeItemAtIndex( StateArrayType *stateData, { int removalIterator; // check to see if index is within size - if(removeIndex < getListSize(*stateData) && removeIndex >= 0) + if(removeIndex <= getListSize(*stateData) && removeIndex >= 0) { // for loop (iterate through list starting at the index) - for(removalIterator = removeIndex; removalIterator < getListSize(*stateData); removalIterator++) + for(removalIterator = removeIndex; removalIterator < + getListSize(*stateData) - 1; removalIterator++) { // shift the next element to current element. - stateData->array[removalIterator] = stateData->array[removalIterator + 1]; + stateData->array[removalIterator] = + stateData->array[removalIterator + 1]; } + // decrement the size of the array + stateData->size = stateData->size - 1; // return true once done return true; } // return false if item not removable? return false; -} +} // this for loop has potential out of bounds issue. /* Name: setItemToEmpty diff --git a/PA05-1004/IteratorUtility.c b/PA05-1004/IteratorUtility.c index 7f88975..beeddf5 100644 --- a/PA05-1004/IteratorUtility.c +++ b/PA05-1004/IteratorUtility.c @@ -68,8 +68,10 @@ Dependencies: getItemAtIndex */ bool getFirst( IteratorType *iterator, StateDataType *returned ) { + // set currentIndex to 0 + iterator->currentIndex = 0; // return function true/false if value possible - return getItemAtIndex(returned, *iterator->iterData, 0); + return getItemAtIndex(returned, *iterator->iterData, iterator->currentIndex); } /* @@ -85,8 +87,10 @@ Dependencies: getItemAtIndex, getListSize */ bool getLast( IteratorType *iterator, StateDataType *returned ) { + // set currentIndex to getListSize + iterator->currentIndex = getListSize(*iterator->iterData); // return function to see if possible - return getItemAtIndex(returned, *iterator->iterData, getListSize(*iterator->iterData)); + return getItemAtIndex(returned, *iterator->iterData, iterator->currentIndex); } /* @@ -106,7 +110,8 @@ bool getNext( IteratorType *iterator, StateDataType *returned ) if(iterator->currentIndex != getListSize(*iterator->iterData)) { iterator->currentIndex = iterator->currentIndex + 1; - return getItemAtIndex(returned, *iterator->iterData, iterator->currentIndex); + return getItemAtIndex(returned, *iterator->iterData, + iterator->currentIndex); } // return false return false; @@ -129,7 +134,8 @@ bool getPrevious( IteratorType *iterator, StateDataType *returned ) if(iterator->currentIndex != 0) { iterator->currentIndex = iterator->currentIndex - 1; - return getItemAtIndex(returned, *iterator->iterData, iterator->currentIndex); + return getItemAtIndex(returned, *iterator->iterData, + iterator->currentIndex); } // return false return false; @@ -151,7 +157,8 @@ bool insertAfterCurrentIndex( IteratorType *iterator, const StateDataType toBeInserted ) { // return function true/false based on possibility - return insertItemAtIndex(iterator->iterData, toBeInserted, iterator->currentIndex + 1); + return insertItemAtIndex(iterator->iterData, toBeInserted, + iterator->currentIndex + 1); } /* @@ -170,7 +177,8 @@ bool insertAtCurrentIndex( IteratorType *iterator, const StateDataType toBeInserted ) { // return true/false based on if the function deems worthy - return insertItemAtIndex(iterator->iterData, toBeInserted, iterator->currentIndex); + return insertItemAtIndex(iterator->iterData, toBeInserted, + iterator->currentIndex); } /* @@ -188,5 +196,18 @@ Dependencies: removeItemAtIndex, getListSize */ bool removeAtCurrentIndex( IteratorType *iterator, StateDataType *removedItem ) { - return removeItemAtIndex(iterator->iterData, removedItem, iterator->currentIndex); + // need to check if the last element is being removed + if(iterator->currentIndex == getListSize(*iterator->iterData)) + { + // move the currentIndex back one, + // return removeItemAtIndex with currentIndex + 1 + iterator->currentIndex = iterator->currentIndex - 1; + return removeItemAtIndex(iterator->iterData, removedItem, + iterator->currentIndex + 1); + // gives the effect like it removed, + // but doesn't screw up the rest of the program. + } + // otherwise, remove the item + return removeItemAtIndex(iterator->iterData, removedItem, + iterator->currentIndex); } \ No newline at end of file diff --git a/PA05-1004/PA05_Main b/PA05-1004/PA05_Main index 13fb324..8670ef5 100755 Binary files a/PA05-1004/PA05_Main and b/PA05-1004/PA05_Main differ diff --git a/PA05-1004/QueueUtility.c b/PA05-1004/QueueUtility.c index 800195d..b0dd100 100644 --- a/PA05-1004/QueueUtility.c +++ b/PA05-1004/QueueUtility.c @@ -64,8 +64,9 @@ Dependencies: getItemAtIndex, getListSize */ bool peekFront( StateDataType *returnVal, const QueueType queue ) { - // return false (TEMP) - return false; + // use getListSize to get the 'front' value + // use getItemAtIndex to 'see' the value at the front. + return getItemAtIndex(returnVal, queue, getListSize(queue)); } /* @@ -82,8 +83,9 @@ Dependencies: removeItemAtIndex, getListSize */ bool dequeue( StateDataType *returnVal, QueueType *queue ) { - // return false (TEMP) - return false; + // get the "front" of the queue + // remove the value in the queue and return success/failure + return removeItemAtIndex(queue, returnVal, getListSize(*queue)); } /* @@ -99,6 +101,6 @@ Dependencies: insertItemAtIndex */ bool enqueue( QueueType *queue, const StateDataType toBePushed ) { - // return false (TEMP) - return false; + // return function success/failure based on inputs + return insertItemAtIndex(queue, toBePushed, 0); } \ No newline at end of file diff --git a/PA05-1004/StackUtility.c b/PA05-1004/StackUtility.c index 359558b..ae97a1a 100644 --- a/PA05-1004/StackUtility.c +++ b/PA05-1004/StackUtility.c @@ -63,8 +63,8 @@ Dependencies: getItemAtIndex, getListSize */ bool peekTop( StateDataType *returnVal, const StackType stackData ) { - // return false (TEMP) - return false; + // return the getItemAtIndex success/failure + return getItemAtIndex(returnVal, stackData, getListSize(stackData)); } /* @@ -81,8 +81,8 @@ Dependencies: removeItemAtIndex, getListSize */ bool popStack( StateDataType *returnVal, StackType *stackData ) { - // return false (TEMP) - return false; + // return function success/failure based on inputs + return removeItemAtIndex(stackData, returnVal, getListSize(*stackData)); } /* @@ -98,6 +98,6 @@ Dependencies: insertItemAtIndex, getListSize */ bool pushStack( StackType *stackData, const StateDataType toBePushed ) { - // return false (TEMP) - return false; + // return function success/failure based on operation + return insertItemAtIndex(stackData, toBePushed, getListSize(*stackData)); } \ No newline at end of file