Commit 5e278819 authored by a-j.towse's avatar a-j.towse

Working ListPokemon function

parent 556f2259
No preview for this file type
...@@ -74,29 +74,22 @@ struct PokemonNode * NewPokemonNode(char name[], char type[], char ability[] ) { ...@@ -74,29 +74,22 @@ struct PokemonNode * NewPokemonNode(char name[], char type[], char ability[] ) {
void AddPokemonToList(struct Pokedex pokedex,char name[], char type[], char ability[]){ void AddPokemonToList(struct Pokedex pokedex,char name[], char type[], char ability[]){
PokemonNode *newNode = NewPokemonNode(name,type,ability); //Call NewPokemonNode and store ptr to new node in variable PokemonNode *newNode = NewPokemonNode(name,type,ability); //Call NewPokemonNode and store ptr to new node in variable
PokemonNode **ptrHead = pokedex.ptrToPokemonHead; PokemonNode **ptrHead = pokedex.ptrToPokemonHead; //Create pointer to pointer variable from pokedex pointer
PokemonNode *temp = *ptrHead; //Create temp variable to store current node in list traversal PokemonNode *temp = *ptrHead; //Create temp pointer variable to store current node in list traversal
//check that the node exists first
if (newNode != NULL){ if (newNode != NULL){
//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; 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
while (temp->next != NULL) { return;
//If pokemon already exists in the list, do nothiing
if (strcmp(temp->name,newNode->name) == 0) {
return;
}
temp = temp->next;
printf("%s\n",temp->name);
} }
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
...@@ -117,10 +110,20 @@ struct PokemonNode * FindPokemon(struct Pokedex pokedex, char name[]) { ...@@ -117,10 +110,20 @@ struct PokemonNode * FindPokemon(struct Pokedex pokedex, char name[]) {
} }
temp = temp->next; temp = temp->next;
} }
return NULL;
} }
void ListPokemon(Pokedex pokedex) {
PokemonNode **ptrHead = pokedex.ptrToPokemonHead;
PokemonNode *temp = *ptrHead;
while (temp != NULL) {
printf("Name: %s\n",temp->name);
temp = temp->next;
}
}
int main (void) { int main (void) {
...@@ -131,10 +134,18 @@ int main (void) { ...@@ -131,10 +134,18 @@ int main (void) {
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
//NewPokemonNode("Charmander","Fire","FireBallz"); //Tests for function implementation
//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,"The grass one","Leaf","LeafSomething");
printf("%p\n",FindPokemon(pokedex,"Charmander"));
ListPokemon(pokedex);
printf("%p\n",FindPokemon(pokedex,"Squirtle"));
return 0; return 0;
} }
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