Got close to finishing
++ IteratorUtility.c ++ LLDataEngine.c ++ QueueUtility.c ++ StackUtility.c +- PA06_Main.c +- Makefile for PA06
This commit is contained in:
parent
635ce9ffad
commit
b7cb3dc9e0
8 changed files with 899 additions and 88 deletions
385
PA06-1018/LLDataEngine.c
Normal file
385
PA06-1018/LLDataEngine.c
Normal file
|
|
@ -0,0 +1,385 @@
|
|||
// header files
|
||||
#include "LLDataEngine.h"
|
||||
|
||||
/* Data Structures
|
||||
typedef struct StateDataStruct
|
||||
{
|
||||
char name[ STD_STR_LEN ];
|
||||
int population;
|
||||
double growth;
|
||||
char majority;
|
||||
struct StateDataStruct *nextPtr;
|
||||
} StateDataType;
|
||||
|
||||
typedef struct StateLLStruct
|
||||
{
|
||||
StateDataType *headPtr;
|
||||
} StateLLType;
|
||||
Data Structures */
|
||||
|
||||
// function implementations
|
||||
|
||||
/*
|
||||
Name: clearDataStructure
|
||||
Process: deallocates memory for the linked list data structure
|
||||
and encapsulated state data using helper function,
|
||||
returns NULL to calling function
|
||||
Function input/parameters: pointer to state linked list array (StateLLType *)
|
||||
Function output/parameters: none
|
||||
Function output/returned: NULL
|
||||
Device input/---: none
|
||||
Device output/---: none
|
||||
Dependencies: free, clearDataStructureHelper
|
||||
*/
|
||||
StateLLType *clearDataStructure( StateLLType *llPtr )
|
||||
{
|
||||
// call clearDataStructureHelper with headPtr
|
||||
privateClearDataStructureHelper(llPtr->headPtr);
|
||||
// free the headPtr
|
||||
free(llPtr);
|
||||
// return NULL
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
Name: copyDataList
|
||||
Process: deallocates destination linked list nodes if it is not empty,
|
||||
then if source list is not empty,
|
||||
copies all nodes from source list to destination list,
|
||||
if source list is empty, copied list will also be configured as empty
|
||||
Function input/parameters: pointer to source state data linked list type
|
||||
(const StateDataType *)
|
||||
Function output/parameters: pointer to destination state data array type
|
||||
(StateLLType *)
|
||||
Function output/returned: none
|
||||
Device input/---: none
|
||||
Device output/---: none
|
||||
Dependencies: clearDataStructureHelper, privateCreateNodeFromNode
|
||||
*/
|
||||
void copyDataList( StateLLType *dest, const StateLLType *src )
|
||||
{
|
||||
// no return, void
|
||||
}
|
||||
|
||||
/*
|
||||
Name: createDataStructure
|
||||
Process: dynamically creates instance of state linked list type
|
||||
along with initializing the linked list within
|
||||
Function input/parameters: none
|
||||
Function output/parameters: none
|
||||
Function output/returned: pointer to state linked list type (StateLLType *)
|
||||
Device input/---: none
|
||||
Device output/---: none
|
||||
Dependencies: malloc, sizeof (C keyword)
|
||||
*/
|
||||
StateLLType *createDataStructure()
|
||||
{
|
||||
// allocate and create StateLLType using malloc and size of
|
||||
StateLLType *dataStruct;
|
||||
dataStruct = malloc(sizeof(StateLLType));
|
||||
// set the head pointer to null
|
||||
dataStruct->headPtr = NULL;
|
||||
// return StateLLType
|
||||
return dataStruct;
|
||||
}
|
||||
|
||||
/*
|
||||
Name: deepCopyData
|
||||
Process: copies all members of a StateDataType node into another one
|
||||
Function input/parameters: state data type item (const StateDataType *)
|
||||
Function output/parameters: pointer to state data item (StateDataType *)
|
||||
Function output/returned: none
|
||||
Device input/---: none
|
||||
Dependencies: privateCopyString
|
||||
Device output/---: none
|
||||
*/
|
||||
void deepCopyData( StateDataType *dest, const StateDataType *src )
|
||||
{
|
||||
// *dest.population = src.population (for each piece)
|
||||
dest->population = src->population;
|
||||
dest->growth = src->growth;
|
||||
dest->majority = src->majority;
|
||||
// privateCopyString for the string
|
||||
// privateCopyString(*dest.name, src.name)
|
||||
privateCopyString(dest->name, src->name);
|
||||
// no return, void
|
||||
}
|
||||
|
||||
/*
|
||||
Name: displayData
|
||||
Process: displays comma-delimited data items in linked list
|
||||
if linked list is not empty,
|
||||
otherwise displays "Data not found - Display aborted",
|
||||
allows for subtitle to be added to "Display Data - "
|
||||
Function input/parameters: state linked list type (const StateLLType),
|
||||
subtitle (const char *)
|
||||
Function output/parameters: none
|
||||
Function output/returned: none
|
||||
Device input/---: none
|
||||
Device output/monitor: data displayed as specified
|
||||
Dependencies: printf, listIsEmpty
|
||||
*/
|
||||
void displayData( const StateLLType stateData, const char *subTitle )
|
||||
{
|
||||
// create a working pointer
|
||||
StateDataType *currentPtr = stateData.headPtr;
|
||||
// check to see if the list is not empty
|
||||
if(!listIsEmpty(stateData))
|
||||
{
|
||||
printf("Display Data - ");
|
||||
printf("%s\n", subTitle);
|
||||
// while through the linked list
|
||||
// get the data out of the first node
|
||||
printf("%s, ", currentPtr->name);
|
||||
printf("%d, ", currentPtr->population);
|
||||
printf("%f, ", currentPtr->growth);
|
||||
printf("%c\n", currentPtr->majority);
|
||||
while(currentPtr != NULL)
|
||||
{
|
||||
// move the pointer
|
||||
currentPtr = currentPtr->nextPtr;
|
||||
// get the data out of the current node
|
||||
printf("%s, ", currentPtr->name);
|
||||
printf("%d, ", currentPtr->population);
|
||||
printf("%f, ", currentPtr->growth);
|
||||
printf("%c\n", currentPtr->majority);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// otherwise, display:
|
||||
// print Data Not Found
|
||||
printf("Data not found - Display aborted\n");
|
||||
}
|
||||
// no return, void
|
||||
}
|
||||
|
||||
/*
|
||||
Name: getItemAtIndex
|
||||
Process: checks for index within bounds,
|
||||
acquires and returns the value as a parameter,
|
||||
and returns true,
|
||||
otherwise sets data in parameter to empty and returns false
|
||||
Function input/parameters: state array type (const StateLLType),
|
||||
list index (int)
|
||||
Function output/parameters: value if found,
|
||||
otherwise, value set to empty (StateDataType *)
|
||||
Function output/returned: Boolean result of operation (bool)
|
||||
Device input/---: none
|
||||
Device output/---: none
|
||||
Dependencies: deepCopyData, setItemToEmpty
|
||||
*/
|
||||
bool getItemAtIndex( StateDataType *returnVal,
|
||||
const StateLLType stateData, int listIndex )
|
||||
{
|
||||
// return false (temp)
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
Name: getListSize
|
||||
Process: calculates the linked list size
|
||||
Function input/parameters: state array type (const StateLLType)
|
||||
Function output/parameters: none
|
||||
Function output/returned: size of the list (int)
|
||||
Device input/---: none
|
||||
Device output/---: none
|
||||
Dependencies: none
|
||||
*/
|
||||
int getListSize( const StateLLType stateData )
|
||||
{
|
||||
int listCounter = 0;
|
||||
StateDataType *currentPtr = stateData.headPtr;
|
||||
// check to see if the headPtr
|
||||
// while through the linked list. count up if next not null.
|
||||
while(currentPtr != NULL)
|
||||
{
|
||||
// listCounter increment
|
||||
listCounter = listCounter + 1;
|
||||
// move the pointer
|
||||
currentPtr = currentPtr->nextPtr;
|
||||
}
|
||||
// return listCounter
|
||||
return listCounter;
|
||||
}
|
||||
|
||||
/*
|
||||
Name: insertItemAtIndex
|
||||
Process: checks for index in bounds, inserts item at index, returns true,
|
||||
if index not in bounds, returns false,
|
||||
must also insert item at index = size to all for appending data
|
||||
Function input/parameters: pointer to state array type (StateLLType *),
|
||||
StateDataType item to insert (const StateDataType),
|
||||
index for insertion (int)
|
||||
Function output/parameters: updated pointer to state array type
|
||||
(StateLLType *)
|
||||
Function output/returned: Boolean result of specified process (bool)
|
||||
Device input/---: none
|
||||
Device output/---: none
|
||||
Dependencies: privateCreateNodeFromNode
|
||||
*/
|
||||
bool insertItemAtIndex( StateLLType *stateData,
|
||||
StateDataType toBeInserted, int insertIndex )
|
||||
{
|
||||
// return false (temp)
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
Name: listIsEmpty
|
||||
Process: returns true if list is empty, false otherwise
|
||||
Function input/parameters: state linked list type (const StateLLType)
|
||||
Function output/parameters: none
|
||||
Function output/returned: Boolean result of test as specified (bool)
|
||||
Device input/---: none
|
||||
Device output/---: none
|
||||
Dependencies: none
|
||||
*/
|
||||
bool listIsEmpty( const StateLLType array )
|
||||
{
|
||||
// return the check of headPtr == null
|
||||
return array.headPtr == NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
Name: privateClearDataStructureHelper
|
||||
Process: recursively iterates through linked list,
|
||||
removing nodes from bottom to top
|
||||
Function input/parameters: pointer to state data linked list (StateDataType *)
|
||||
Function output/parameters: none
|
||||
Function output/returned: NULL
|
||||
Device input/---: none
|
||||
Device output/---: none
|
||||
Dependencies: free, clearDataStructureHelper (recursively)
|
||||
*/
|
||||
StateDataType *privateClearDataStructureHelper( StateDataType *nodePtr )
|
||||
{
|
||||
// check if node's nextPtr not null
|
||||
if(nodePtr->nextPtr != NULL)
|
||||
{
|
||||
// if it is not null, call the next recursion.
|
||||
nodePtr->nextPtr = privateClearDataStructureHelper(nodePtr->nextPtr);
|
||||
}
|
||||
// free the node using free
|
||||
free(nodePtr);
|
||||
// return NULL
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
Name: privateCreateNodeFromData
|
||||
Process: creates linked list node provided member data
|
||||
Function input/parameters: name (const char *), population (int),
|
||||
growth (double), majority (char)
|
||||
Function output/parameters: none
|
||||
Function output/returned: pointer to created node (StateDataType *)
|
||||
Device input/---: none
|
||||
Device output/---: none
|
||||
Dependencies: malloc, privateCopyString, keyword sizeof
|
||||
*/
|
||||
StateDataType *privateCreateNodeFromData( const char *inName,
|
||||
int inPop, double inGrowth,
|
||||
char inMajority )
|
||||
{
|
||||
// allocate node memory using malloc and size of
|
||||
StateDataType *workingNode;
|
||||
workingNode = malloc(sizeof(StateDataType));
|
||||
// copy string into node
|
||||
privateCopyString(workingNode->name, inName);
|
||||
// copy other data pieces into node
|
||||
workingNode->population = inPop;
|
||||
workingNode->growth = inGrowth;
|
||||
workingNode->majority = inMajority;
|
||||
// set nextPtr to null
|
||||
workingNode->nextPtr = NULL;
|
||||
// return node
|
||||
return workingNode;
|
||||
}
|
||||
|
||||
/*
|
||||
Name: privateCreateNodeFromNode
|
||||
Process: creates linked list node provided another node
|
||||
Function input/parameters: other node (const StateDataType)
|
||||
Function output/parameters: none
|
||||
Function output/returned: pointer to created node (StateDataType *)
|
||||
Device input/---: none
|
||||
Device output/---: none
|
||||
Dependencies: one line of code - privateCreateNodeFromData
|
||||
*/
|
||||
StateDataType *privateCreateNodeFromNode( const StateDataType source )
|
||||
{
|
||||
// return the node
|
||||
return privateCreateNodeFromData(source.name, source.population,
|
||||
source.growth, source.majority);
|
||||
}
|
||||
|
||||
/*
|
||||
Name: privateCopyString
|
||||
Process: copies string character for character up to NULL_CHAR of source string
|
||||
Function input/parameters: source string to be copied (const char *)
|
||||
Function output/parameters: destination string (char *)
|
||||
Function output/returned: none
|
||||
Device input/---: none
|
||||
Device output/---: none
|
||||
Dependencies: none
|
||||
*/
|
||||
void privateCopyString( char *dest, const char *src )
|
||||
{
|
||||
int characterCount = 0;
|
||||
// use a while loop, check for !NULL_CHAR
|
||||
while(src[characterCount] != NULL_CHAR)
|
||||
{
|
||||
// copy the character accessed into dest string
|
||||
dest[characterCount] = src[characterCount];
|
||||
// iterate the number of characters
|
||||
characterCount = characterCount + 1;
|
||||
}
|
||||
// add a NULL_CHAR once done, characterCount is in the right place for \0
|
||||
dest[characterCount] = NULL_CHAR;
|
||||
// no return, void
|
||||
}
|
||||
|
||||
/*
|
||||
Name: removeItemAtIndex
|
||||
Process: removes node from linked list at index,
|
||||
returns true if found, false otherwise
|
||||
Function input/parameters: pointer to state linked list type (StateLLType *),
|
||||
index to remove (int)
|
||||
Function output/parameters: pointer to updated state linked list type
|
||||
(StateLLType *),
|
||||
pointer to removed item (StateDataType *)
|
||||
Function output/returned: Boolean result of removal operation (bool)
|
||||
Device input/---: none
|
||||
Device output/---: none
|
||||
Dependencies: deepCopyData, free, setItemToEmpty
|
||||
*/
|
||||
bool removeItemAtIndex( StateLLType *stateData,
|
||||
StateDataType *removedItem, int removeIndex )
|
||||
{
|
||||
// return false (temp)
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
Name: setItemToEmpty
|
||||
Process: sets state data type struct to empty or default values,
|
||||
including empty string and majority 'x'
|
||||
Function input/parameters: pointer to state data value (StateDataType *)
|
||||
Function output/parameters: pointer to updated state data value
|
||||
(StateDataType *)
|
||||
Function output/returned: none
|
||||
Device input/---: none
|
||||
Device output/---: none
|
||||
Dependencies: none
|
||||
*/
|
||||
void setItemToEmpty( StateDataType *data )
|
||||
{
|
||||
// set the data's name to ""
|
||||
data->name[0] = NULL_CHAR;
|
||||
// set the data's population and growth to 0
|
||||
data->population = 0;
|
||||
data->growth = 0.0;
|
||||
// set the data's majority to 'x'
|
||||
data->majority = 'x';
|
||||
// no return, void
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue