"Finished" PA06

+- LLDataEngine.c
+- PA06_Main
+- QueueUtility.c
This commit is contained in:
Charles Descamps 2023-10-18 15:39:05 -07:00
parent cc4036d8ad
commit bc496e01e4
3 changed files with 127 additions and 5 deletions

View file

@ -172,7 +172,23 @@ Dependencies: deepCopyData, setItemToEmpty
bool getItemAtIndex( StateDataType *returnVal, bool getItemAtIndex( StateDataType *returnVal,
const StateLLType stateData, int listIndex ) const StateLLType stateData, int listIndex )
{ {
// return false (temp) int llIncrementer = 0;
StateDataType *currentPtr = stateData.headPtr;
// while through linked list
while(!listIsEmpty(stateData) && currentPtr != NULL)
{
// look for listIndex
if(listIndex == llIncrementer)
{
// if found, return true, copy data into return
deepCopyData(returnVal, currentPtr);
return true;
}
llIncrementer = llIncrementer + 1;
}
// since not found, return empty value in returnVal.
setItemToEmpty(returnVal);
// return false since not found, or not within bounds.
return false; return false;
} }
@ -221,7 +237,60 @@ Dependencies: privateCreateNodeFromNode
bool insertItemAtIndex( StateLLType *stateData, bool insertItemAtIndex( StateLLType *stateData,
StateDataType toBeInserted, int insertIndex ) StateDataType toBeInserted, int insertIndex )
{ {
// return false (temp) int llIncrementer = 0;
StateDataType *currentPtr = stateData->headPtr;
StateDataType *insertedNode;
// while through linked list
while(!listIsEmpty(*stateData) && currentPtr != NULL)
{
// checks within middle of list
if(insertIndex == llIncrementer)
{
// if found, return true, insert data
// create node given toBeInserted
insertedNode = privateCreateNodeFromNode(toBeInserted);
// move the node's nextPtr to currentPtr->nextPtr
currentPtr->nextPtr = insertedNode->nextPtr;
// move the currentPtr's nextPtr to the node
currentPtr = insertedNode;
return true;
}
llIncrementer = llIncrementer + 1;
}
// need a check for beginning of list
if(insertIndex == 0)
{
if(stateData->headPtr == NULL)
{
// if at beginning of list, but no more data
// create node given toBeInserted
insertedNode = privateCreateNodeFromNode(toBeInserted);
// move the headPtr to node
stateData->headPtr = insertedNode;
return true;
}
// if at beginning of list, if more data in list
// create node given toBeInserted
insertedNode = privateCreateNodeFromNode(toBeInserted);
// need to point the headPtr->nextPtr to the node's nextPtr
stateData->headPtr->nextPtr = insertedNode->nextPtr;
// move the headPtr to node
stateData->headPtr = insertedNode;
return true;
}
// put the end of list check out here?
if(insertIndex == getListSize(*stateData))
{
// if found, return true, append data
// create node given toBeInserted
insertedNode = privateCreateNodeFromNode(toBeInserted);
// move the node's nextPtr to NULL
insertedNode->nextPtr = NULL;
// move the currentPtr's nextPtr to the node
currentPtr = insertedNode;
return true;
}
// return false since not inserted, or not within bounds.
return false; return false;
} }
@ -356,7 +425,60 @@ Dependencies: deepCopyData, free, setItemToEmpty
bool removeItemAtIndex( StateLLType *stateData, bool removeItemAtIndex( StateLLType *stateData,
StateDataType *removedItem, int removeIndex ) StateDataType *removedItem, int removeIndex )
{ {
// return false (temp) int llIncrementer = 0;
StateDataType *currentPtr = stateData->headPtr;
// while through linked list
while(!listIsEmpty(*stateData) && currentPtr != NULL)
{
// checks within middle of list
if(removeIndex == llIncrementer)
{
// if found, return true, insert data
// copy data out of the list
deepCopyData(removedItem, currentPtr);
// move the pointer of currentPtr to the next item
currentPtr->nextPtr = currentPtr->nextPtr->nextPtr;
// free the memory of the removed item.
free(currentPtr->nextPtr);
return true;
}
llIncrementer = llIncrementer + 1;
}
// need a check for beginning of list
if(removeIndex == 0)
{
if(stateData->headPtr == NULL)
{
// if at beginning of list, but no more data
// copy data out of the list
deepCopyData(removedItem, currentPtr);
// free the memory of the removed item.
free(currentPtr->nextPtr);
return true;
}
// if at beginning of list, if more data in list
// copy data out of the list
deepCopyData(removedItem, currentPtr);
// move the pointer of currentPtr to the next item
currentPtr->nextPtr = currentPtr->nextPtr->nextPtr;
// free the memory of the removed item.
free(currentPtr->nextPtr);
return true;
}
// put the end of list check out here?
if(removeIndex == getListSize(*stateData))
{
// if found, return true, append data
// copy data out of the list
deepCopyData(removedItem, currentPtr);
// move the pointer of currentPtr to the next item
currentPtr->nextPtr = currentPtr->nextPtr->nextPtr;
// free the memory of the removed item.
free(currentPtr->nextPtr);
return true;
}
setItemToEmpty(removedItem);
// return false since not inserted, or not within bounds.
return false; return false;
} }

Binary file not shown.

View file

@ -66,7 +66,7 @@ bool peekFront( StateDataType *returnVal, const QueueType queue )
{ {
// use getListSize to get the 'front' value // use getListSize to get the 'front' value
// use getItemAtIndex to 'see' the value at the front. // use getItemAtIndex to 'see' the value at the front.
return getItemAtIndex(returnVal, queue, getListSize(queue) - 1); return getItemAtIndex(returnVal, queue, getListSize(queue));
} }
/* /*
@ -85,7 +85,7 @@ bool dequeue( StateDataType *returnVal, QueueType *queue )
{ {
// get the "front" of the queue // get the "front" of the queue
// remove the value in the queue and return success/failure // remove the value in the queue and return success/failure
return removeItemAtIndex(queue, returnVal, getListSize(*queue) - 1); return removeItemAtIndex(queue, returnVal, getListSize(*queue));
} }
/* /*