Got close to finishing

++ IteratorUtility.c
++ LLDataEngine.c
++ QueueUtility.c
++ StackUtility.c
+- PA06_Main.c
+- Makefile for PA06
This commit is contained in:
Charles Descamps 2023-10-17 20:48:30 -07:00
parent 635ce9ffad
commit b7cb3dc9e0
8 changed files with 899 additions and 88 deletions

213
PA06-1018/IteratorUtility.c Normal file
View file

@ -0,0 +1,213 @@
// header files
#include "IteratorUtility.h"
/* Data Structures
typedef struct IteratorStruct
{
StateArrayType *iterData;
int currentIndex;
} IteratorType;
Data Structures */
// function implementation
/*
Name: clearIterator
Process: clears the StateDataType sub-structure, and then the IteratorType
Function input/parameters: pointer to IteratorType (IteratorType *)
Function output/parameters: none
Function output/returned: NULL
Device input/---: none
Device output/---: none
Dependencies: clearDataStructure, free
*/
IteratorType *clearIterator( IteratorType *iterator )
{
// clear the encapsulated data structure
clearDataStructure(iterator->iterData);
// clear the rest of the data structure
free(iterator);
// return NULL
return NULL;
}
/*
Name: createIterator
Process: initializes the IteratorType,
and then the StateDataType sub-structure
Function input/parameters: initial capacity (int)
Function output/parameters: none
Function output/returned: pointer to initialized IteratorType (IteratorType *)
Device input/---: none
Device output/---: none
Dependencies: malloc, createDataStructure
*/
IteratorType *createIterator( int initialCapacity )
{
IteratorType *iteratorDataStructure;
// create the entire data structure
iteratorDataStructure = malloc(sizeof(IteratorType));
iteratorDataStructure->currentIndex = 0;
// create the encapsulated data structure
iteratorDataStructure->iterData = createDataStructure(initialCapacity);
// return IteratorType
return iteratorDataStructure;
}
/*
Name: getFirst
Process: gets first value in iterator, returns true,
returns false otherwise
Function input/parameters: pointer to IteratorType (IteratorType *)
Function output/parameters: return value (StateDataType *)
Function output/returned: Boolean result of operation
Device input/---: none
Device output/---: none
Dependencies: getItemAtIndex
*/
bool getFirst( IteratorType *iterator, StateDataType *returned )
{
// set currentIndex to 0
iterator->currentIndex = 0;
// return function true/false if value possible
return getItemAtIndex(returned, *iterator->iterData, iterator->currentIndex);
}
/*
Name: getLast
Process: gets last value in iterator, returns true,
returns false otherwise
Function input/parameters: pointer to IteratorType (IteratorType *)
Function output/parameters: return value (StateDataType *)
Function output/returned: Boolean result of operation
Device input/---: none
Device output/---: none
Dependencies: getItemAtIndex, getListSize
*/
bool getLast( IteratorType *iterator, StateDataType *returned )
{
// set currentIndex to getListSize
iterator->currentIndex = getListSize(*iterator->iterData);
// return function to see if possible
return getItemAtIndex(returned, *iterator->iterData, iterator->currentIndex);
}
/*
Name: getNext
Process: gets next value in iterator if available, returns true,
otherwise, sets return value to empty, returns false
Function input/parameters: pointer to IteratorType (IteratorType *)
Function output/parameters: return value (StateDataType *)
Function output/returned: Boolean result of operation
Device input/---: none
Device output/---: none
Dependencies: getItemAtIndex, getListSize
*/
bool getNext( IteratorType *iterator, StateDataType *returned )
{
// check to see if the currentIndex value is not size.
if(iterator->currentIndex != getListSize(*iterator->iterData))
{
iterator->currentIndex = iterator->currentIndex + 1;
return getItemAtIndex(returned, *iterator->iterData,
iterator->currentIndex);
}
// return false
return false;
}
/*
Name: getPrevious
Process: gets previous value in iterator if available, returns true,
otherwise, sets return value to empty, returns false
Function input/parameters: pointer to IteratorType (IteratorType *)
Function output/parameters: return value (StateDataType *)
Function output/returned: Boolean result of operation
Device input/---: none
Device output/---: none
Dependencies: getItemAtIndex
*/
bool getPrevious( IteratorType *iterator, StateDataType *returned )
{
// check to see if the currentIndex value is not zero.
if(iterator->currentIndex != 0)
{
iterator->currentIndex = iterator->currentIndex - 1;
return getItemAtIndex(returned, *iterator->iterData,
iterator->currentIndex);
}
// return false
return false;
}
/*
Name: insertAfterCurrentIndex
Process: inserts value immediately after currentIndex, if possible, returns true,
otherwise, returns false
Function input/parameters: pointer to IteratorType (IteratorType *),
value to be inserted (const StateDataType)
Function output/parameters: none
Function output/returned: Boolean result of operation
Device input/---: none
Device output/---: none
Dependencies: insertItemAtIndex, getListSize
*/
bool insertAfterCurrentIndex( IteratorType *iterator,
const StateDataType toBeInserted )
{
// return function true/false based on possibility
return insertItemAtIndex(iterator->iterData, toBeInserted,
iterator->currentIndex + 1);
}
/*
Name: insertAtCurrentIndex
Process: inserts value at currentIndex, if possible, returns true
otherwise, returns false
Function input/parameters: pointer to IteratorType (IteratorType *),
value to be inserted (const StateDataType)
Function output/parameters: none
Function output/returned: Boolean result of operation
Device input/---: none
Device output/---: none
Dependencies: insertItemAtIndex
*/
bool insertAtCurrentIndex( IteratorType *iterator,
const StateDataType toBeInserted )
{
// return true/false based on if the function deems worthy
return insertItemAtIndex(iterator->iterData, toBeInserted,
iterator->currentIndex);
}
/*
Name: removeAtCurrentIndex
Process: removes value at current index if possible, returns true,
otherwise, sets return value to empty, returns false
Function input/parameters: pointer to IteratorType (IteratorType *),
value to be remove (StateDataType *)
Function output/parameters: pointer to updated IteratorType (IteratorType *),
pointer to item removed (StateDataType *)
Function output/returned: Boolean result of operation
Device input/---: none
Device output/---: none
Dependencies: removeItemAtIndex, getListSize
*/
bool removeAtCurrentIndex( IteratorType *iterator, StateDataType *removedItem )
{
// need to check if the last element is being removed
if(iterator->currentIndex == getListSize(*iterator->iterData))
{
// move the currentIndex back one,
// return removeItemAtIndex with currentIndex + 1
iterator->currentIndex = iterator->currentIndex - 1;
return removeItemAtIndex(iterator->iterData, removedItem,
iterator->currentIndex + 1);
// gives the effect like it removed,
// but doesn't screw up the rest of the program.
}
// otherwise, remove the item
return removeItemAtIndex(iterator->iterData, removedItem,
iterator->currentIndex);
}

385
PA06-1018/LLDataEngine.c Normal file
View 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
}

View file

@ -1,19 +1,19 @@
CC = gcc -Wall CC = gcc -Wall
SOURCES = PA05_Main.c ArrayDataEngine.c IteratorUtility.c QueueUtility.c StackUtility.c Console_IO_Utility.c File_Input_Utility.c File_Output_Utility.c SOURCES = PA06_Main.c LLDataEngine.c IteratorUtility.c QueueUtility.c StackUtility.c Console_IO_Utility.c File_Input_Utility.c File_Output_Utility.c
OBJECTS = PA05_Main.o ArrayDataEngine.o IteratorUtility.o QueueUtility.o StackUtility.o Console_IO_Utility.o File_Input_Utility.o File_Output_Utility.o OBJECTS = PA06_Main.o LLDataEngine.o IteratorUtility.o QueueUtility.o StackUtility.o Console_IO_Utility.o File_Input_Utility.o File_Output_Utility.o
# Uses the template structure defined in the TypicalUtils folder # Uses the template structure defined in the TypicalUtils folder
all: PA05_Main all: PA06_Main
PA05_Main: $(OBJECTS) PA06_Main: $(OBJECTS)
$(CC) $(OBJECTS) -o PA05_Main $(CC) $(OBJECTS) -o PA06_Main
PA05_Main.o: PA05_Main.c PA06_Main.o: PA06_Main.c
$(CC) -c PA05_Main.c -o PA05_Main.o $(CC) -c PA06_Main.c -o PA06_Main.o
ArrayDataEngine.o: ArrayDataEngine.c LLDataEngine.o: LLDataEngine.c
$(CC) -c ArrayDataEngine.c -o ArrayDataEngine.o $(CC) -c LLDataEngine.c -o LLDataEngine.o
QueueUtility.o: QueueUtility.c QueueUtility.o: QueueUtility.c
$(CC) -c QueueUtility.c -o QueueUtility.o $(CC) -c QueueUtility.c -o QueueUtility.o
@ -34,13 +34,13 @@ File_Output_Utility.o: File_Output_Utility.c
$(CC) -c File_Output_Utility.c -o File_Output_Utility.o $(CC) -c File_Output_Utility.c -o File_Output_Utility.o
clean: clean:
rm -f $(OBJECTS) PA05_Main PA05_Main.out rm -f $(OBJECTS) PA06_Main PA06_Main.out
clean_objects: clean_objects:
rm -f $(OBJECTS) rm -f $(OBJECTS)
run: run:
./PA05_Main ./PA06_Main
run_win: run_win:
PA05_Main PA06_Main

BIN
PA06-1018/PA06_Main Executable file

Binary file not shown.

View file

@ -4,7 +4,7 @@
#include "StackUtility.h" #include "StackUtility.h"
#include "QueueUtility.h" #include "QueueUtility.h"
// local constants for program // local constants for program, unneeded?
const int INITIAL_NUM_STATES = 10; const int INITIAL_NUM_STATES = 10;
// function prototypes // function prototypes
@ -18,8 +18,8 @@ int main()
// initialize variables // initialize variables
StateLLType *wkgLL; StateLLType *wkgLL;
StateDataType tempData; StateDataType tempData;
IteratorType *iterator; //IteratorType *iterator;
StackType *stack; //StackType *stack;
QueueType *queue; QueueType *queue;
int index, testLimit, testStart = 0; int index, testLimit, testStart = 0;
@ -28,120 +28,122 @@ int main()
showTitle(); showTitle();
// set up data // set up data
wkgLL = createDataStructure( INITIAL_NUM_STATES ); wkgLL = createDataStructure();
iterator = createIterator( INITIAL_NUM_STATES ); //iterator = createIterator();
stack = createStack( INITIAL_NUM_STATES ); //stack = createStack();
queue = createQueue( INITIAL_NUM_STATES ); queue = createQueue();
// Upload data // Upload data
if( uploadStateData( wkgLL, "StateData.csv" ) ) if( uploadStateData( wkgLL, "StateData.csv" ) )
{ {
// Testing for iterators // Testing for iterators
printf( "\nIterator Test Bed ---------------------------------\n\n" ); // printf( "\nIterator Test Bed ---------------------------------\n\n" );
copyDataList( iterator->iterData, wkgLL ); // copyDataList( iterator->iterData, wkgLL );
printf( "Iterate across list, first to last\n" ); // printf( "Iterate across list, first to last\n" );
getFirst( iterator, &tempData ); // getFirst( iterator, &tempData );
printf( "First name: %s\n", tempData.name ); // printf( "First name: %s\n", tempData.name );
while( getNext( iterator, &tempData ) ) // while( getNext( iterator, &tempData ) )
{ // {
printf( "Next name: %s\n", tempData.name ); // printf( "Next name: %s\n", tempData.name );
} // }
printf( "\nIterate across list, last to first\n" ); // printf( "\nIterate across list, last to first\n" );
getLast( iterator, &tempData ); // getLast( iterator, &tempData );
printf( "Last name: %s\n", tempData.name ); // printf( "Last name: %s\n", tempData.name );
while( getPrevious( iterator, &tempData ) ) // while( getPrevious( iterator, &tempData ) )
{ // {
printf( "Prior name: %s\n", tempData.name ); // printf( "Prior name: %s\n", tempData.name );
} // }
// Testing for Stacks // // Testing for Stacks
printf( "\nStack Test Bed ------------------------------------\n\n" ); // printf( "\nStack Test Bed ------------------------------------\n\n" );
printf( "Push items on stack:\n" ); // printf( "Push items on stack:\n" );
testLimit = 10; // testLimit = 10;
for( index = testStart; index < testLimit; index++ ) // for( index = testStart; index < testLimit; index++ )
{ // {
getItemAtIndex( &tempData, *wkgLL, index ); // getItemAtIndex( &tempData, *wkgLL, index );
printf( "Pushing: %s\n", tempData.name ); // printf( "Pushing: %s\n", tempData.name );
pushStack( stack, tempData ); // pushStack( stack, tempData );
} // }
printf( "\nCurrent stack: " ); // printf( "\nCurrent stack: " );
testStart = 0; // testStart = 0;
for( index = testStart; index < getListSize( *stack ); index++ ) // for( index = testStart; index < getListSize( *stack ); index++ )
{ // {
getItemAtIndex( &tempData, *stack, index ); // getItemAtIndex( &tempData, *stack, index );
printf( "%s | ", tempData.name ); // printf( "%s | ", tempData.name );
} // }
printf( "<- Top of stack\n\nPop items from stack:\n" ); // printf( "<- Top of stack\n\nPop items from stack:\n" );
testLimit = 5; // testLimit = 5;
for( index = testStart; index < testLimit; index++ ) // for( index = testStart; index < testLimit; index++ )
{ // {
popStack( &tempData, stack ); // popStack( &tempData, stack );
printf( "Popping: %s\n", tempData.name ); // printf( "Popping: %s\n", tempData.name );
} // }
printf( "\nCurrent stack: " ); // printf( "\nCurrent stack: " );
for( index = testStart; index < getListSize( *stack ); index++ ) // for( index = testStart; index < getListSize( *stack ); index++ )
{ // {
getItemAtIndex( &tempData, *stack, index ); // getItemAtIndex( &tempData, *stack, index );
printf( "%s | ", tempData.name ); // printf( "%s | ", tempData.name );
} // }
printf( "<- Top of stack\n\nPush more items on stack:\n" ); // printf( "<- Top of stack\n\nPush more items on stack:\n" );
testStart = 10; testLimit = 15; // testStart = 10; testLimit = 15;
for( index = testStart; index < testLimit; index++ ) // for( index = testStart; index < testLimit; index++ )
{ // {
getItemAtIndex( &tempData, *wkgLL, index ); // getItemAtIndex( &tempData, *wkgLL, index );
printf( "Pushing: %s\n", tempData.name ); // printf( "Pushing: %s\n", tempData.name );
pushStack( stack, tempData ); // pushStack( stack, tempData );
} // }
printf( "\nCurrent stack: " ); // printf( "\nCurrent stack: " );
testStart = 0; // testStart = 0;
for( index = testStart; index < getListSize( *stack ); index++ ) // for( index = testStart; index < getListSize( *stack ); index++ )
{ // {
getItemAtIndex( &tempData, *stack, index ); // getItemAtIndex( &tempData, *stack, index );
printf( "%s | ", tempData.name ); // printf( "%s | ", tempData.name );
} // }
printf( "<- Top of stack\n\nPopping until empty:\n" ); // printf( "<- Top of stack\n\nPopping until empty:\n" );
while( popStack( &tempData, stack ) ) // while( popStack( &tempData, stack ) )
{ // {
printf( "Popping: %s\n", tempData.name ); // printf( "Popping: %s\n", tempData.name );
} // }
// Testing for Queues // Testing for Queues
printf( "\nQueue Test Bed ------------------------------------\n\n" ); printf( "\nQueue Test Bed ------------------------------------\n\n" );
printf( "Enqueue items:\n" ); printf( "Enqueue items:\n" );
//printf("\nList Size: %d\n", getListSize(*queue));
testStart = 0; testLimit = 10; testStart = 0; testLimit = 10;
for( index = testStart; index < testLimit; index++ ) for( index = testStart; index < testLimit; index++ )
{ {
@ -152,6 +154,8 @@ int main()
enqueue( queue, tempData ); enqueue( queue, tempData );
} }
//printf("\nList Size: %d\n", getListSize(*queue));
printf( "\nCurrent queue: Rear -> " ); printf( "\nCurrent queue: Rear -> " );
testStart = 0; testStart = 0;
@ -234,9 +238,9 @@ int main()
wkgLL = clearDataStructure( wkgLL ); wkgLL = clearDataStructure( wkgLL );
iterator = clearIterator( iterator ); //iterator = clearIterator( iterator );
stack = clearStack( stack ); //stack = clearStack( stack );
queue = clearQueue( queue ); queue = clearQueue( queue );

106
PA06-1018/QueueUtility.c Normal file
View file

@ -0,0 +1,106 @@
// header files
#include "QueueUtility.h"
/* Data Structures
typedef struct StateArrayStruct
{
StateDataType *array;
int size, capacity;
} QueueType; (aka StateArrayType)
Data Structures*/
// function implementations
/*
Name: clearQueue
Process: clears QueueType, returns NULL
Function input/parameters: pointer to queue type (QueueType *)
Function output/parameters: none
Function output/returned: NULL
Device input/---: none
Device output/---: none
Dependencies: clearDataStructure
*/
QueueType *clearQueue( QueueType *queue )
{
// clear the entire data structure
clearDataStructure(queue);
// return NULL
return NULL;
}
/*
Name: createQueue
Process: creates QueueType, initializes front index to zero,
rear index to -1, returns pointer to data structure
Function input/parameters: initial capacity (int)
Function output/parameters: none
Function output/returned: pointer to queue type (QueueType *)
Device input/---: none
Device output/---: none
Dependencies: createDataStructure
*/
QueueType *createQueue()
{
// create the data structure
QueueType *queueDataStructure;
queueDataStructure = createDataStructure();
// return QueueType
return queueDataStructure;
}
/*
Name: peekFront
Process: captures value at front of queue
returns true if successful and sets return value,
otherwise, sets return value to empty, returns false
Function input/parameters: pointer to queue type (QueueType *)
Function output/parameters: pointer to return value (StateDataType *)
Function output/returned: Boolean result of operation
Device input/---: none
Device output/---: none
Dependencies: getItemAtIndex, getListSize
*/
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);
}
/*
Name: dequeue
Process: captures value at front of queue, removes and returns value,
returns true if successful,
otherwise, sets return value to empty, returns false
Function input/parameters: pointer to queue type (QueueType *)
Function output/parameters: pointer to return value (StateDataType *)
Function output/returned: Boolean result of operation
Device input/---: none
Device output/---: none
Dependencies: removeItemAtIndex, getListSize
*/
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);
}
/*
Name: enqueue
Process: sets value at rear of queue
returns true if successful, false otherwise
Function input/parameters: pointer to queue type (QueueType *)
Function output/parameters: value to be pushed (const StateDataType)
Function output/returned: Boolean result of operation
Device input/---: none
Device output/---: none
Dependencies: insertItemAtIndex
*/
bool enqueue( QueueType *queue, const StateDataType toBePushed )
{
// return function success/failure based on inputs
return insertItemAtIndex(queue, toBePushed, 0);
}

View file

@ -32,7 +32,7 @@ Device input/---: none
Device output/---: none Device output/---: none
Dependencies: createDataStructure Dependencies: createDataStructure
*/ */
QueueType *createQueue( int initialCapacity ); QueueType *createQueue();
/* /*
Name: peekFront Name: peekFront

103
PA06-1018/StackUtility.c Normal file
View file

@ -0,0 +1,103 @@
// header files
#include "StackUtility.h"
/* Data Structures
typedef struct StateArrayStruct
{
StateDataType *array;
int size, capacity;
} StackType; (aka StateArrayType)
Data Structures*/
// function implementations
/*
Name: clearStack
Process: clears StackType, returns NULL
Function input/parameters: pointer to stack type (StackType *)
Function output/parameters: none
Function output/returned: NULL
Device input/---: none
Device output/---: none
Dependencies: clearDataStructure
*/
StackType *clearStack( StackType *stackData )
{
// clear the entire data structure
clearDataStructure(stackData);
// return NULL
return NULL;
}
/*
Name: createStack
Process: creates StackType, returns pointer to it
Function input/parameters: initial capacity (int)
Function output/parameters: none
Function output/returned: pointer to stack type (StackType *)
Device input/---: none
Device output/---: none
Dependencies: createDataStructure
*/
StackType *createStack( int initialCapacity )
{
// create the data structure
StackType *stackDataStructure;
stackDataStructure = createDataStructure(initialCapacity);
// return StackType
return stackDataStructure;
}
/*
Name: peekTop
Process: captures value at top of stack, returns,
returns true if successful,
otherwise, sets return value to empty, returns false
Function input/parameters: pointer to stack type (StackType *)
Function output/parameters: pointer to return value (StateDataType *)
Function output/returned: Boolean result of operation (bool)
Device input/---: none
Device output/---: none
Dependencies: getItemAtIndex, getListSize
*/
bool peekTop( StateDataType *returnVal, const StackType stackData )
{
// return the getItemAtIndex success/failure
return getItemAtIndex(returnVal, stackData, getListSize(stackData));
}
/*
Name: popStack
Process: captures value at top of stack, removes and returns,
returns true if successful,
otherwise, sets return value to empty, returns false
Function input/parameters: pointer to stack type (StackType *)
Function output/parameters: pointer to return value (StateDataType *)
Function output/returned: Boolean result of operation
Device input/---: none
Device output/---: none
Dependencies: removeItemAtIndex, getListSize
*/
bool popStack( StateDataType *returnVal, StackType *stackData )
{
// return function success/failure based on inputs
return removeItemAtIndex(stackData, returnVal, getListSize(*stackData));
}
/*
Name: pushStack
Process: sets value at top of stack,
returns true if successful, false otherwise
Function input/parameters: pointer to stack type (StackType *)
Function output/parameters: value to be pushed (const StateDataType)
Function output/returned: Boolean result of operation
Device input/---: none
Device output/---: none
Dependencies: insertItemAtIndex, getListSize
*/
bool pushStack( StackType *stackData, const StateDataType toBePushed )
{
// return function success/failure based on operation
return insertItemAtIndex(stackData, toBePushed, getListSize(*stackData));
}