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);
//Create NewPokemonNode function - returns variable of type PokemonNode
struct PokemonNode * NewPokemonNode(char name[], char type[], char ability[] ) {
struct PokemonNode *newNode = NULL; //Create a pointer to a PokemonNode structure, NULL for safety
newNode = malloc(sizeof(struct PokemonNode)); //Assign memory space for PokemonNode
if (newNode != NULL) { //Ensure it exists
strcpy(newNode->name,name); //Set name
strcpy(newNode->type,type); //Set type
strcpy(newNode->ability,ability); //Set ability
newNode->next = NULL; //Set next pointer to NULL
newNode->prev = NULL; //Set prev pointer to NULL
struct PokemonNode *newNode = NULL; //Create a pointer to a PokemonNode structure, NULL for safety
newNode = malloc(sizeof(struct PokemonNode)); //Assign memory space for PokemonNode
if (newNode != NULL) { //Ensure it exists
strcpy(newNode->name,name); //Set name
strcpy(newNode->type,type); //Set type
strcpy(newNode->ability,ability); //Set ability
newNode->next = NULL; //Set next pointer to NULL
newNode->prev = NULL; //Set prev pointer to NULL
}
return newNode;
......@@ -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 (temp == NULL) {
*ptrHead = newNode; //Point the head to the new node
newNode->prev = NULL; //Set the new node prev to NULL
newNode->next = NULL; //Set the new node next to NULL
*ptrHead = newNode; //Point the head to the new node
newNode->prev = NULL; //Set the new node prev to NULL
newNode->next = NULL; //Set the new node next to NULL
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;
}
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
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
}
}
//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;
PokemonNode *temp = *ptrHead;
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
//Traverse list with temp until NULL
while (temp != NULL) {
//string comparison of current node name and target name
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;
}
//Create ListPokemon function, prints a list of pokemon names, returns void
void ListPokemon(Pokedex pokedex) {
PokemonNode **ptrHead = pokedex.ptrToPokemonHead;
PokemonNode *temp = *ptrHead;
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
//Traverse list with temp until NULL
while (temp != NULL) {
printf("Name: %s\n",temp->name);
temp = temp->next;
printf("Name: %s\n",temp->name); //Print the name of the node that temp is currently pointing to
temp = temp->next; //temp increments to the next item in the list
}
}
int main (void) {
PokemonNode *PokemonHead = NULL; //Create head for DLL of pokemon
PlayerNode *PlayerHead = NULL; //Create head for DLL of players
PokemonNode *PokemonHead = NULL; //Create head for DLL of pokemon
PlayerNode *PlayerHead = NULL; //Create head for DLL of players
Pokedex pokedex; //Create a pointer to a Pokedex structure, NULL
pokedex.ptrToPlayerHead = &PlayerHead; //Set pointer value head of Player List
pokedex.ptrToPokemonHead = &PokemonHead; //Set pointer value to head of Pokemon List
Pokedex pokedex; //Create a pointer to a Pokedex structure, NULL
pokedex.ptrToPlayerHead = &PlayerHead; //Set pointer value head of Player List
pokedex.ptrToPokemonHead = &PokemonHead; //Set pointer value to head of Pokemon List
//Tests for function implementation
/*Tests for function implementation*/
//NewPokemonNode("Charmander","Fire","FireBallz");
AddPokemonToList(pokedex,"Charmander","Fire","FireBallz");
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");
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