diff --git a/PA06-1018/LLDataEngine.c b/PA06-1018/LLDataEngine.c index 21c4f1b..e32ed5a 100644 --- a/PA06-1018/LLDataEngine.c +++ b/PA06-1018/LLDataEngine.c @@ -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; } diff --git a/PA06-1018/PA06_Main b/PA06-1018/PA06_Main index 2b67098..211bcd8 100755 Binary files a/PA06-1018/PA06_Main and b/PA06-1018/PA06_Main differ diff --git a/PA06-1018/QueueUtility.c b/PA06-1018/QueueUtility.c index 39c842b..5023663 100644 --- a/PA06-1018/QueueUtility.c +++ b/PA06-1018/QueueUtility.c @@ -66,7 +66,7 @@ bool peekFront( StateDataType *returnVal, const QueueType queue ) { // use getListSize to get the 'front' value // 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 // remove the value in the queue and return success/failure - return removeItemAtIndex(queue, returnVal, getListSize(*queue) - 1); + return removeItemAtIndex(queue, returnVal, getListSize(*queue)); } /*