Commit 8916b496 authored by a-j.towse's avatar a-j.towse

half working addToList

parent 9d1811c4
No preview for this file type
......@@ -10,8 +10,8 @@ A-J Towse
//Create struct Pokedex with a pointer to head of Pokemon and head of Player
typedef struct Pokedex {
struct PokemonNode * ptrToPokemonhead;
struct PlayerNode * ptrToPlayerhead;
struct PokemonNode ** ptrToPokemonHead;
struct PlayerNode ** ptrToPlayerHead;
}Pokedex;
//Create struct PokemonNode with name,type,ability variables
......@@ -36,15 +36,10 @@ typedef struct PlayerNode {
struct PokemonNode pokemonList[];
}PlayerNode;
PokemonNode *head = NULL; //Create head for DLL of pokemon
PlayerNode *head = NULL; //Create head for DLL of players
//NEED TO CREATE VARIABLE OF TYPE POKEDEX
//Define functions for PokemonNode struct
struct PokemonNode * NewPokemonNode(char name[], char type[], char ability[] );
void AddPokemonToList(struct Pokedex *pokedex,char name[], char type[], char ability[]);
struct PokemonNode * FindPokemon(struct Pokedex pokedex, char name);
void AddPokemonToList(struct Pokedex pokedex,char name[], char type[], char ability[]);
struct PokemonNode * FindPokemon(struct Pokedex pokedex, char name[]);
//Define functions for PlayerNode struct
struct PlayerNode * NewPlayerNode(char name);
......@@ -76,24 +71,65 @@ struct PokemonNode * NewPokemonNode(char name[], char type[], char ability[] ) {
}
//Create AddPokemonToList function - returns void
void AddPokemonToList(struct Pokedex *pokedex,char name[], char type[], char ability[]){
void AddPokemonToList(struct Pokedex pokedex,char name[], char type[], char ability[]){
NewPokemonNode(name,type,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
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;
return;
}
/*while (temp->next != NULL) {
//If pokemon already exists in the list, do nothiing
if (strcmp(temp->name,newNode->name) == 0) {
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
}
}
struct PokemonNode * FindPokemon(struct Pokedex pokedex, char name[]) {
PokemonNode *temp = pokedex.ptrToPokemonHead;
while (temp->next != NULL) {
if (strcmp(temp->name,name) == 0) {
return temp;
}
}
}
int main (void) {
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
//NewPokemonNode("Charmander","Fire","FireBallz");
AddPokemonToList(pokedex,"Charmander","Fire","FireBallz");
AddPokemonToList(pokedex,"Squirtle","Water","WaterBlast");
//printf("%p\n",FindPokemon(pokedex,"Charmander"));
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