Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- using namespace std;
- /********************************************
- Pokemon Class
- ********************************************/
- class Pokemon {
- private:
- string name; //name of the pokemon
- Pokemon *prev; //pointer the points to previous pokemon
- Pokemon *next; //pointer that points to next pokemon object
- public:
- Pokemon(string s); // set pokemon name; set prev and next to null
- Pokemon();
- string getName(); // return the name of the pokemon
- void setName(string); // set new name to the pokemon
- Pokemon * getPrev(); // return prev pointer
- Pokemon * getNext(); // return next pointer
- void setPrev(Pokemon *ptr); // pointer prev points to new pokemon
- void setNext(Pokemon *ptr); // pointer next points to new pokemon
- void displayPokemon(); // display pokemon
- friend class PokemonList;
- };
- /********************************************
- Pokemon::Pokemon
- Constructor that initalizes pokemon name
- set prev poitner to null
- set next pointer to null
- ********************************************/
- Pokemon::Pokemon(string s){
- name = s;
- Pokemon *prev = NULL;
- Pokemon *next = NULL;
- }
- string Pokemon::getName(){
- return name;
- }
- void Pokemon::setName(string){
- name = name;
- }
- Pokemon *Pokemon::getPrev(){
- return prev;
- }
- Pokemon *Pokemon::getNext(){
- return next;
- }
- void Pokemon::setPrev(Pokemon *ptr){
- prev = ptr;
- }
- void Pokemon::setNext(Pokemon *ptr){
- next = ptr;
- }
- /********************************************
- Pokemon::displayPokemon()
- Display the address that prev holds, address of the current object,
- the address that next holds, and the pokemon name
- 0x7fe9ba500030 || 0x7fe9ba500000 || 0x7fe9ba500030 Charizard
- 0x7fe9ba500000 || 0x7fe9ba500030 || 0x7fe9ba500000 Pikachu
- tips: use (this) to display the address of the current pokemon object
- ********************************************/
- void Pokemon::displayPokemon() {
- cout << &Pokemon::prev << this << &Pokemon::next << name;
- }
- /********************************************
- PokemonList Class
- ********************************************/
- class PokemonList{
- private:
- Pokemon *first;
- int totalPokemon;
- public:
- PokemonList(); //set first to null, set total to 0;
- int getTotal(); //return value of totalPokemon
- bool isEmpty(); //check if the list is empty
- Pokemon * getFirst(); //return the first pointer value
- void insert(); //insert a pokemon at the end of the list
- void displayList(Pokemon *current); //display all pokemon in the list
- friend class Pokemon;
- };
- //make sure you provide header comments
- PokemonList::PokemonList(){
- Pokemon *first = NULL;
- totalPokemon = 0;
- }
- int PokemonList::getTotal(){
- return totalPokemon;
- }
- bool PokemonList::isEmpty(){
- if(totalPokemon == 0)
- return true;
- else
- return false;
- }
- Pokemon *PokemonList::getFirst(){
- return first;
- }
- /********************************************
- PokemonList::insert()
- Ask user to enter the name of pokemon
- if the list is empty
- dynamically allocate first pointer with new pokemon
- assign first's next pointer to first address
- assigne first's prev pointer to first address
- The circle of life (LION KING THEME)
- else
- Dyanmically allocate a new pokemon
- Draw out the process of adding a node to the end of double linked-list
- Do crazy pointer manipulation base on the drawing
- (I will go over the process in class so please be there)
- ********************************************/
- void PokemonList::insert() {
- string poke;
- Pokemon *temp1, *temp2;
- cout << "Pokemon Name: ";
- cin >> poke;
- if(totalPokemon == 0){
- temp1 = new Pokemon;
- temp1->prev = NULL;
- temp1->name = poke;
- temp1->next = first;
- first->prev = temp1;
- first = temp1;
- }
- else{
- temp2 = new Pokemon;
- temp1 = new Pokemon;
- while(temp2->next != NULL){
- temp2 = temp2->next;
- }
- temp2->next = temp1;
- temp1->prev = temp1;
- temp1->next = NULL;
- }
- }
- void PokemonList::displayList(Pokemon *current){
- while(current != NULL)
- {
- cout << current->next << endl;
- current = current->next;
- }
- }
- /********************************************
- Main function
- ********************************************/
- int main() {
- int choice;
- PokemonList pokeDex; //create an empty list
- Pokemon *current; // the current pokemon you look at
- do {
- cout << endl << "=====================================" << endl;
- cout << "PokeDex" << endl;
- cout << "=====================================" << endl;
- cout << "Select your option:" << endl;
- cout << "1) Insert new Pokemon" << endl;
- cout << "2) Next Pokemon" << endl;
- cout << "3) Prev Pokemon" << endl;
- cout << "4) List all Pokemons" <<endl;
- cout << "5) Quit" << endl;
- cout << "=====================================" << endl;
- //YOUR CODE GOES HERE
- //IF WE HAVE 1 POKEMON IN THE LIST
- //ASSIGNING CURRENT POINTER TO THE FIRST POKEMON
- if (pokeDex.getTotal() > 0) {
- cout << endl <<"Current Pokemon" <<endl;
- current->displayPokemon();
- }
- cout << endl << "Please select your choice: ";
- cin >> choice;
- switch(choice) {
- case 1:
- pokeDex.insert();
- break;
- case 2:
- //IF THE LIST IS NOT EMPTY
- if(pokeDex.isEmpty() == false){
- current = current->getNext(); //ASSIGN CURRENT POINTER TO THE NEXT POKEMON
- }
- else{
- cout << "The list is empty";
- }
- break;
- case 3:
- //IF THE LIST IS NOT EMPTY
- if(pokeDex.isEmpty() == false){
- current = current->getPrev();//ASSIGN CURRENT POINTER TO THE PREVIOUS POKEMON
- }
- else{
- cout << "The list is empty";
- }
- break;
- case 4:
- //IF THE LIST IS NOT EMPTY
- if(pokeDex.isEmpty() == false){
- pokeDex.displayList(current); //DISPLAY ALL POKEMON IN THE LIST
- }
- else{
- cout << "The list is empty";
- }
- break;
- case 5:
- cout << "Terminating the super secret Pokemon list" << endl;
- break;
- default:
- cout << "Invalid choice!!" << endl;
- }
- } while(choice != 5);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement