Commit ed2565c1 authored by a-j.towse's avatar a-j.towse

All commented to date

parent a8969b0a
...@@ -56,15 +56,15 @@ void ListPlayers(struct Pokedex pokedex); ...@@ -56,15 +56,15 @@ void ListPlayers(struct Pokedex pokedex);
//Create NewPokemonNode function - returns variable of type PokemonNode //Create NewPokemonNode function - returns variable of type PokemonNode
struct PokemonNode * NewPokemonNode(char name[], char type[], char ability[] ) { struct PokemonNode * NewPokemonNode(char name[], char type[], char ability[] ) {
struct PokemonNode *newNode = NULL; //Create a pointer to a PokemonNode structure, NULL for safety struct PokemonNode *newNode = NULL; //Create a pointer to a PokemonNode structure, NULL for safety
newNode = malloc(sizeof(struct PokemonNode)); //Assign memory space for PokemonNode newNode = malloc(sizeof(struct PokemonNode)); //Assign memory space for PokemonNode
if (newNode != NULL) { //Ensure it exists if (newNode != NULL) { //Ensure it exists
strcpy(newNode->name,name); //Set name strcpy(newNode->name,name); //Set name
strcpy(newNode->type,type); //Set type strcpy(newNode->type,type); //Set type
strcpy(newNode->ability,ability); //Set ability strcpy(newNode->ability,ability); //Set ability
newNode->next = NULL; //Set next pointer to NULL newNode->next = NULL; //Set next pointer to NULL
newNode->prev = NULL; //Set prev pointer to NULL newNode->prev = NULL; //Set prev pointer to NULL
} }
return newNode; return newNode;
...@@ -82,65 +82,68 @@ void AddPokemonToList(struct Pokedex pokedex,char name[], char type[], char abil ...@@ -82,65 +82,68 @@ void AddPokemonToList(struct Pokedex pokedex,char name[], char type[], char abil
//If the list is empty, point head to new node //If the list is empty, point head to new node
if (temp == NULL) { if (temp == NULL) {
*ptrHead = newNode; //Point the head to the new node *ptrHead = newNode; //Point the head to the new node
newNode->prev = NULL; //Set the new node prev to NULL newNode->prev = NULL; //Set the new node prev to NULL
newNode->next = NULL; //Set the new node next to NULL newNode->next = NULL; //Set the new node next to NULL
return; return;
} }
if (FindPokemon(pokedex,newNode->name) != NULL) { //Call FindPokemon to check if the pokemon eixists in the list already if (FindPokemon(pokedex,newNode->name) != NULL) { //Call FindPokemon to check if the pokemon eixists in the list already
return; return;
} }
temp->next = newNode; //Set the next pointer of the old last node to new node temp->next = newNode; //Set the next pointer of the old last node to new node
newNode->prev = temp; //Set the prev pointer of the new node to the old last node newNode->prev = temp; //Set the prev pointer of the new node to the old last node
} }
} }
//Create Findpokemon function, returns pointer to the node if found, NULL (nil) if not found
struct PokemonNode * FindPokemon(struct Pokedex pokedex, char name[]) {
struct PokemonNode * FindPokemon(struct Pokedex pokedex, char name[]) { /*Add comments to these two functions*/ PokemonNode **ptrHead = pokedex.ptrToPokemonHead; //Create pointer to pointer variable from pokedex pointer
PokemonNode *temp = *ptrHead; //Create temp pointer variable to store current node in list traversal
PokemonNode **ptrHead = pokedex.ptrToPokemonHead;
PokemonNode *temp = *ptrHead;
//Traverse list with temp until NULL
while (temp != NULL) { while (temp != NULL) {
//string comparison of current node name and target name
if (strcmp(temp->name,name) == 0) { if (strcmp(temp->name,name) == 0) {
return temp; return temp; //If string comparison is true (0) return address of current node
} }
temp = temp->next; temp = temp->next; //temp increments to the next item in the list
} }
return NULL; return NULL;
} }
//Create ListPokemon function, prints a list of pokemon names, returns void
void ListPokemon(Pokedex pokedex) { void ListPokemon(Pokedex pokedex) {
PokemonNode **ptrHead = pokedex.ptrToPokemonHead; PokemonNode **ptrHead = pokedex.ptrToPokemonHead; //Create pointer to pointer variable from pokedex pointer
PokemonNode *temp = *ptrHead; PokemonNode *temp = *ptrHead; //Create temp pointer variable to store current node in list traversal
//Traverse list with temp until NULL
while (temp != NULL) { while (temp != NULL) {
printf("Name: %s\n",temp->name); printf("Name: %s\n",temp->name); //Print the name of the node that temp is currently pointing to
temp = temp->next; temp = temp->next; //temp increments to the next item in the list
} }
} }
int main (void) { int main (void) {
PokemonNode *PokemonHead = NULL; //Create head for DLL of pokemon PokemonNode *PokemonHead = NULL; //Create head for DLL of pokemon
PlayerNode *PlayerHead = NULL; //Create head for DLL of players PlayerNode *PlayerHead = NULL; //Create head for DLL of players
Pokedex pokedex; //Create a pointer to a Pokedex structure, NULL Pokedex pokedex; //Create a pointer to a Pokedex structure, NULL
pokedex.ptrToPlayerHead = &PlayerHead; //Set pointer value head of Player List pokedex.ptrToPlayerHead = &PlayerHead; //Set pointer value head of Player List
pokedex.ptrToPokemonHead = &PokemonHead; //Set pointer value to head of Pokemon List pokedex.ptrToPokemonHead = &PokemonHead; //Set pointer value to head of Pokemon List
//Tests for function implementation /*Tests for function implementation*/
//NewPokemonNode("Charmander","Fire","FireBallz"); //NewPokemonNode("Charmander","Fire","FireBallz");
AddPokemonToList(pokedex,"Charmander","Fire","FireBallz"); AddPokemonToList(pokedex,"Charmander","Fire","FireBallz");
AddPokemonToList(pokedex,"Squirtle","Water","WaterBlast"); AddPokemonToList(pokedex,"Squirtle","Water","WaterBlast");
AddPokemonToList(pokedex,"Squirtle","Water","WaterBlast"); //Testing that it doesnt add a duplicate AddPokemonToList(pokedex,"Squirtle","Water","WaterBlast"); //Testing that it doesnt add a duplicate
AddPokemonToList(pokedex,"The grass one","Leaf","LeafSomething"); AddPokemonToList(pokedex,"The grass one","Leaf","LeafSomething");
printf("%p\n",FindPokemon(pokedex,"Charmander")); printf("%p\n",FindPokemon(pokedex,"Charmander"));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment