Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Pokedex New
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zak.evans
Pokedex New
Commits
ea865ec8
Commit
ea865ec8
authored
Mar 29, 2023
by
zak.evans
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add new file
parent
94c98830
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
194 additions
and
0 deletions
+194
-0
main.c
main.c
+194
-0
No files found.
main.c
0 → 100644
View file @
ea865ec8
#include <iostream>
#include <string>
using
namespace
std
;
// Pokemon struct
struct
Pokemon
{
string
name
;
int
id
;
string
type
;
string
ability
;
Pokemon
*
next
;
// Pointer to next Pokemon in linked list
};
// Player struct
struct
Player
{
string
name
;
Player
*
next
;
// Pointer to next Player in linked list
Pokemon
*
pokemon
;
// Pointer to the first Pokemon in the linked list of the player's captured Pokemon
};
// Defining the global variables
Pokemon
*
pokemon
=
nullptr
;
Player
*
player
=
nullptr
;
// Function to create a new Pokemon node
Pokemon
*
createPokemon
(
string
name
,
int
id
,
string
type
,
string
ability
)
{
Pokemon
*
newPokemon
=
new
Pokemon
;
newPokemon
->
name
=
name
;
newPokemon
->
id
=
id
;
newPokemon
->
type
=
type
;
newPokemon
->
ability
=
ability
;
newPokemon
->
next
=
nullptr
;
return
newPokemon
;
}
// Function to create a new Player node
Player
*
createPlayer
(
string
name
)
{
Player
*
newPlayer
=
new
Player
;
newPlayer
->
name
=
name
;
newPlayer
->
pokemon
=
nullptr
;
newPlayer
->
next
=
nullptr
;
return
newPlayer
;
}
// Function to add a Pokemon to the Pokemons list
void
addPokemon
(
Pokemon
**
newPokemon
,
Pokemon
**
head
)
{
(
*
newPokemon
)
->
next
=
*
head
;
*
head
=
*
newPokemon
;
}
// Function to add a Player to the Players list
void
addPlayer
(
Player
**
newPlayer
,
Player
**
head
)
{
(
*
newPlayer
)
->
next
=
*
head
;
*
head
=
*
newPlayer
;
}
// Function to add a Pokemon to a Player's Pokemons list
void
addPokemonToPlayer
(
Player
*
player
,
Pokemon
*
newPokemon
)
{
newPokemon
->
next
=
player
->
pokemon
;
player
->
pokemon
=
newPokemon
;
}
// Function to display all the Pokemons
void
displayPokemons
(
Pokemon
*
head
)
{
Pokemon
*
pokemonHead
=
head
;
while
(
pokemonHead
!=
nullptr
)
{
cout
<<
pokemonHead
->
name
<<
" (#"
<<
pokemonHead
->
id
<<
")"
<<
" (#"
<<
pokemonHead
->
type
<<
")"
" (#"
<<
pokemonHead
->
ability
<<
")"
<<
endl
;
pokemonHead
=
pokemonHead
->
next
;
}
}
// Function to display all the Players
void
displayPlayers
(
Player
*
head
)
{
Player
*
playerHead
=
head
;
while
(
playerHead
!=
nullptr
)
{
cout
<<
playerHead
->
name
<<
endl
;
playerHead
=
playerHead
->
next
;
}
}
// Function to display a Player's Pokemons
void
displayPlayerPokemons
(
Player
*
player
)
{
displayPokemons
(
player
->
pokemon
);
}
// Function to find a speicifc Pokemon
Pokemon
*
findPokemon
(
string
name
,
Pokemon
*
head
)
{
Pokemon
*
current
=
head
;
while
(
current
!=
nullptr
)
{
if
(
current
->
name
==
name
)
{
return
current
;
}
current
=
current
->
next
;
}
return
nullptr
;
}
//Function to find a specific player
Player
*
findPlayer
(
string
name
,
Player
*
head
)
{
Player
*
current
=
head
;
while
(
current
!=
nullptr
)
{
if
(
current
->
name
==
name
)
{
return
current
;
}
current
=
current
->
next
;
}
return
nullptr
;
}
// Function to delete a Pokemon list
void
deletePokemonList
(
Pokemon
**
head
)
{
Pokemon
*
current
=
*
head
;
while
(
current
!=
nullptr
)
{
Pokemon
*
next
=
current
->
next
;
delete
current
;
current
=
next
;
}
*
head
=
nullptr
;
}
// Function to delete a Player list
void
deletePlayerList
(
Player
**
head
)
{
Player
*
current
=
*
head
;
while
(
current
!=
nullptr
)
{
Player
*
next
=
current
->
next
;
deletePokemonList
(
&
current
->
pokemon
);
delete
current
;
current
=
next
;
}
*
head
=
nullptr
;
}
// Main function
int
main
()
{
// Create the first few Pokemons
Pokemon
*
bulbasaur
=
createPokemon
(
"Bulbasaur"
,
1
,
"Grass"
,
"Overgrow"
);
Pokemon
*
charmander
=
createPokemon
(
"Charmander"
,
2
,
"Fire"
,
"Blaze"
);
Pokemon
*
squirtle
=
createPokemon
(
"Squirtle"
,
3
,
"Water"
,
"Torrent"
);
Pokemon
*
caterpie
=
createPokemon
(
"Caterpie"
,
4
,
"Bug"
,
"Shield Dusk"
);
// Create the first few Players
Player
*
zak
=
createPlayer
(
"Zak"
);
Player
*
will
=
createPlayer
(
"Will"
);
Player
*
tom
=
createPlayer
(
"Tom"
);
// Adding the Pokemons to the Pokemons list
addPokemon
(
&
bulbasaur
,
&
pokemon
);
addPokemon
(
&
charmander
,
&
pokemon
);
addPokemon
(
&
squirtle
,
&
pokemon
);
addPokemon
(
&
caterpie
,
&
pokemon
);
// Adding the Players to the Players list
addPlayer
(
&
zak
,
&
player
);
addPlayer
(
&
will
,
&
player
);
addPlayer
(
&
tom
,
&
player
);
// Add some of the Pokemons to the Players' Pokemons list
addPokemonToPlayer
(
zak
,
bulbasaur
);
addPokemonToPlayer
(
zak
,
charmander
);
addPokemonToPlayer
(
will
,
squirtle
);
addPokemonToPlayer
(
tom
,
caterpie
);
// Display the Pokemons and Players with their Pokemons
cout
<<
"All Pokemons:
\n
"
;
displayPokemons
(
pokemon
);
cout
<<
endl
;
cout
<<
"All Players:
\n
"
;
displayPlayers
(
player
);
cout
<<
endl
;
cout
<<
"Zak's Pokemons:
\n
"
;
displayPlayerPokemons
(
zak
);
cout
<<
endl
;
cout
<<
"Will's Pokemons:
\n
"
;
displayPlayerPokemons
(
will
);
cout
<<
endl
;
cout
<<
"Tom's Pokemons:
\n
"
;
displayPlayerPokemons
(
tom
);
cout
<<
endl
;
// Cleans up the memory at the end
deletePokemonList
(
&
pokemon
);
deletePlayerList
(
&
player
);
return
0
;
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment