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

Working ListPokemon function

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