"Completed" PA05

(aka don't want to spend anymore time on it)
+- ArrayDataEngine.c
+- IteratorUtility.c
+- QueueUtility.c
+- StackUtility.c
This commit is contained in:
Charles Descamps 2023-10-04 17:25:56 -07:00
parent a9a217cc45
commit 770212bc8e
5 changed files with 75 additions and 45 deletions

View file

@ -62,16 +62,17 @@ Dependencies: privateCheckForResize, deepCopyData
void copyDataList( StateArrayType *dest, const StateArrayType src ) void copyDataList( StateArrayType *dest, const StateArrayType src )
{ {
int copyIterator; int copyIterator;
privateCheckForResize(dest); dest->size = 0;
// check for resize if the destination capacity is less than source // check for resize if the destination capacity is less than source
if(dest->capacity == src.capacity) if(dest->capacity == src.capacity)
{ {
dest->size = src.size; privateCheckForResize(dest);
// if it is, then size the dest to what source was // if it is, then size the dest to what source was
// copy the data from source to dest using deepCopy // copy the data from source to dest using deepCopy
for(copyIterator = 0; copyIterator < src.size; copyIterator++) for(copyIterator = 0; copyIterator < src.size; copyIterator++)
{ {
deepCopyData(&dest->array[copyIterator], src.array[copyIterator]); deepCopyData(&dest->array[copyIterator], src.array[copyIterator]);
dest->size = dest->size + 1;
} }
} }
// no return, void // no return, void
@ -148,7 +149,8 @@ void displayData( const StateArrayType stateData, const char *subTitle )
// print subtitle (and new line) // print subtitle (and new line)
printf("%s\n", subTitle); printf("%s\n", subTitle);
// for loop (iterates through the array data) // for loop (iterates through the array data)
for(displayIterator = 0; displayIterator < stateData.size; displayIterator++) for(displayIterator = 0; displayIterator < stateData.size;
displayIterator++)
{ {
// print Name, // print Name,
printf("%s, ", stateData.array[displayIterator].name); printf("%s, ", stateData.array[displayIterator].name);
@ -239,25 +241,29 @@ bool insertItemAtIndex( StateArrayType *stateData,
StateDataType toBeInserted, int insertIndex ) StateDataType toBeInserted, int insertIndex )
{ {
int insertIterator; int insertIterator;
// check for resize
privateCheckForResize(stateData);
// check to see if index is within size // 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 // 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 // put the element into element + 1
stateData->array[insertIterator + 1] = stateData->array[insertIterator]; stateData->array[insertIterator + 1] =
stateData->array[insertIterator];
} }
// insert element at index // insert element at index
stateData->array[insertIndex] = toBeInserted; stateData->array[insertIndex] = toBeInserted;
// increment the size by one
stateData->size = stateData->size + 1;
// return true // return true
return true; return true;
} }
// return false // return false
return false; return false;
} } // Needs to iterate from top to bottom, whatever this means.
/* /*
Name: listIsEmpty Name: listIsEmpty
@ -271,14 +277,8 @@ Dependencies: none
*/ */
bool listIsEmpty( const StateArrayType array ) bool listIsEmpty( const StateArrayType array )
{ {
// check to see if size is equal to 0 // return the check to see if the size is equal to 0
if(array.size == 0) return (array.size == 0);
{
// return true since array empty
return true;
}
// return false if not empty
return false;
} }
/* /*
@ -305,12 +305,15 @@ void privateCheckForResize( StateArrayType *stateData )
// 'create' bigger array // 'create' bigger array
stateData->capacity = stateData->capacity * 2; stateData->capacity = stateData->capacity * 2;
// allocate the bigger array // 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 // 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 // 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 the old array
free(stateData->array); free(stateData->array);
@ -341,8 +344,8 @@ void privateCopyString( char *dest, const char *src )
// iterate the number of characters // iterate the number of characters
characterCount = characterCount + 1; characterCount = characterCount + 1;
} }
// add a NULL_CHAR once done. (destStringIterator + 1) // add a NULL_CHAR once done, characterCount is in the right place for \0
dest[characterCount + 1] = NULL_CHAR; dest[characterCount] = NULL_CHAR;
// no return, void // no return, void
} }
@ -365,21 +368,25 @@ bool removeItemAtIndex( StateArrayType *stateData,
{ {
int removalIterator; int removalIterator;
// check to see if index is within size // 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 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. // 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 once done
return true; return true;
} }
// return false if item not removable? // return false if item not removable?
return false; return false;
} } // this for loop has potential out of bounds issue.
/* /*
Name: setItemToEmpty Name: setItemToEmpty

View file

@ -68,8 +68,10 @@ Dependencies: getItemAtIndex
*/ */
bool getFirst( IteratorType *iterator, StateDataType *returned ) bool getFirst( IteratorType *iterator, StateDataType *returned )
{ {
// set currentIndex to 0
iterator->currentIndex = 0;
// return function true/false if value possible // 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 ) bool getLast( IteratorType *iterator, StateDataType *returned )
{ {
// set currentIndex to getListSize
iterator->currentIndex = getListSize(*iterator->iterData);
// return function to see if possible // 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)) if(iterator->currentIndex != getListSize(*iterator->iterData))
{ {
iterator->currentIndex = iterator->currentIndex + 1; iterator->currentIndex = iterator->currentIndex + 1;
return getItemAtIndex(returned, *iterator->iterData, iterator->currentIndex); return getItemAtIndex(returned, *iterator->iterData,
iterator->currentIndex);
} }
// return false // return false
return false; return false;
@ -129,7 +134,8 @@ bool getPrevious( IteratorType *iterator, StateDataType *returned )
if(iterator->currentIndex != 0) if(iterator->currentIndex != 0)
{ {
iterator->currentIndex = iterator->currentIndex - 1; iterator->currentIndex = iterator->currentIndex - 1;
return getItemAtIndex(returned, *iterator->iterData, iterator->currentIndex); return getItemAtIndex(returned, *iterator->iterData,
iterator->currentIndex);
} }
// return false // return false
return false; return false;
@ -151,7 +157,8 @@ bool insertAfterCurrentIndex( IteratorType *iterator,
const StateDataType toBeInserted ) const StateDataType toBeInserted )
{ {
// return function true/false based on possibility // 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 ) const StateDataType toBeInserted )
{ {
// return true/false based on if the function deems worthy // 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 ) 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);
} }

Binary file not shown.

View file

@ -64,8 +64,9 @@ Dependencies: getItemAtIndex, getListSize
*/ */
bool peekFront( StateDataType *returnVal, const QueueType queue ) bool peekFront( StateDataType *returnVal, const QueueType queue )
{ {
// return false (TEMP) // use getListSize to get the 'front' value
return false; // 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 ) bool dequeue( StateDataType *returnVal, QueueType *queue )
{ {
// return false (TEMP) // get the "front" of the queue
return false; // 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 ) bool enqueue( QueueType *queue, const StateDataType toBePushed )
{ {
// return false (TEMP) // return function success/failure based on inputs
return false; return insertItemAtIndex(queue, toBePushed, 0);
} }

View file

@ -63,8 +63,8 @@ Dependencies: getItemAtIndex, getListSize
*/ */
bool peekTop( StateDataType *returnVal, const StackType stackData ) bool peekTop( StateDataType *returnVal, const StackType stackData )
{ {
// return false (TEMP) // return the getItemAtIndex success/failure
return false; return getItemAtIndex(returnVal, stackData, getListSize(stackData));
} }
/* /*
@ -81,8 +81,8 @@ Dependencies: removeItemAtIndex, getListSize
*/ */
bool popStack( StateDataType *returnVal, StackType *stackData ) bool popStack( StateDataType *returnVal, StackType *stackData )
{ {
// return false (TEMP) // return function success/failure based on inputs
return false; return removeItemAtIndex(stackData, returnVal, getListSize(*stackData));
} }
/* /*
@ -98,6 +98,6 @@ Dependencies: insertItemAtIndex, getListSize
*/ */
bool pushStack( StackType *stackData, const StateDataType toBePushed ) bool pushStack( StackType *stackData, const StateDataType toBePushed )
{ {
// return false (TEMP) // return function success/failure based on operation
return false; return insertItemAtIndex(stackData, toBePushed, getListSize(*stackData));
} }