"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:
parent
a9a217cc45
commit
770212bc8e
5 changed files with 75 additions and 45 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue