"Finished" PA06
+- LLDataEngine.c +- PA06_Main +- QueueUtility.c
This commit is contained in:
parent
cc4036d8ad
commit
bc496e01e4
3 changed files with 127 additions and 5 deletions
|
|
@ -172,7 +172,23 @@ Dependencies: deepCopyData, setItemToEmpty
|
|||
bool getItemAtIndex( StateDataType *returnVal,
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
@ -221,7 +237,60 @@ Dependencies: privateCreateNodeFromNode
|
|||
bool insertItemAtIndex( StateLLType *stateData,
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
@ -356,7 +425,60 @@ Dependencies: deepCopyData, free, setItemToEmpty
|
|||
bool removeItemAtIndex( StateLLType *stateData,
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue