Advertisement
dknight234

linked lists project

May 14th, 2015
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.18 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. /********************************************
  8.             Pokemon Class      
  9.  ********************************************/
  10. class Pokemon {
  11.     private:
  12.         string name;                    //name of the pokemon
  13.         Pokemon *prev;                  //pointer the points to previous pokemon
  14.         Pokemon *next;                  //pointer that points to next pokemon object
  15.    
  16.     public:
  17.         Pokemon(string s); // set pokemon name; set prev and next to null
  18.         Pokemon();
  19.         string getName();               // return the name of the pokemon
  20.         void setName(string);           // set new name to the pokemon
  21.         Pokemon * getPrev();            // return prev pointer
  22.         Pokemon * getNext();            // return next pointer
  23.         void setPrev(Pokemon *ptr);     // pointer prev points to new pokemon
  24.         void setNext(Pokemon *ptr);     // pointer next points to new pokemon
  25.         void displayPokemon(); // display pokemon
  26.         friend class PokemonList;
  27.  
  28. };
  29.  
  30. /********************************************
  31.               Pokemon::Pokemon              
  32.  Constructor that initalizes pokemon name
  33.     set prev poitner to null
  34.     set next pointer to null      
  35.  ********************************************/
  36. Pokemon::Pokemon(string s){
  37.     name = s;
  38.     Pokemon *prev = NULL;
  39.     Pokemon *next = NULL;
  40. }
  41.  
  42. string Pokemon::getName(){
  43.     return name;
  44. }
  45.  
  46. void Pokemon::setName(string){
  47.     name = name;
  48. }
  49.  
  50. Pokemon *Pokemon::getPrev(){
  51.     return prev;
  52. }
  53.  
  54. Pokemon *Pokemon::getNext(){
  55.     return next;
  56. }
  57.  
  58. void Pokemon::setPrev(Pokemon *ptr){
  59.     prev = ptr;
  60. }
  61.  
  62. void Pokemon::setNext(Pokemon *ptr){
  63.     next = ptr;
  64. }
  65.  
  66. /********************************************
  67.               Pokemon::displayPokemon()              
  68.  Display the address that prev holds, address of the current object,
  69.  the address that next holds, and the pokemon name
  70.    
  71.     0x7fe9ba500030 || 0x7fe9ba500000 || 0x7fe9ba500030   Charizard
  72.     0x7fe9ba500000 || 0x7fe9ba500030 || 0x7fe9ba500000   Pikachu
  73.  
  74.     tips: use (this) to display the address of the current pokemon object
  75.  ********************************************/
  76. void Pokemon::displayPokemon() {
  77.     cout << &Pokemon::prev << this << &Pokemon::next << name;
  78. }
  79.  
  80.  
  81. /********************************************
  82.             PokemonList Class      
  83.  ********************************************/
  84. class PokemonList{
  85.     private:
  86.         Pokemon *first;
  87.         int totalPokemon;
  88.  
  89.     public:
  90.         PokemonList();              //set first to null, set total to 0;
  91.         int getTotal();             //return value of totalPokemon
  92.         bool isEmpty();             //check if the list is empty
  93.         Pokemon * getFirst();       //return the first pointer value
  94.         void insert();              //insert a pokemon at the end of the list
  95.         void displayList(Pokemon *current);         //display all pokemon in the list
  96.         friend class Pokemon;
  97. };
  98. //make sure you provide header comments
  99.  
  100. PokemonList::PokemonList(){
  101.     Pokemon *first = NULL;
  102.     totalPokemon = 0;
  103. }
  104.  
  105. int PokemonList::getTotal(){
  106.     return totalPokemon;
  107. }
  108.  
  109. bool PokemonList::isEmpty(){
  110.     if(totalPokemon == 0)
  111.         return true;
  112.     else
  113.         return false;
  114. }
  115.  
  116. Pokemon *PokemonList::getFirst(){
  117.     return first;
  118. }
  119.  
  120. /********************************************
  121.              PokemonList::insert()
  122.  
  123. Ask user to enter the name of pokemon
  124.  
  125. if the list is empty
  126.     dynamically allocate first pointer with new pokemon
  127.     assign first's next pointer to first address
  128.     assigne first's prev pointer to first address
  129.     The circle of life (LION KING THEME)
  130.  
  131. else
  132.     Dyanmically allocate a new pokemon
  133.     Draw out the process of adding a node to the end of double linked-list
  134.     Do crazy pointer manipulation base on the drawing
  135.     (I will go over the process in class so please be there)
  136.  
  137.  ********************************************/
  138. void PokemonList::insert() {
  139.     string poke;
  140.     Pokemon *temp1, *temp2;
  141.  
  142.     cout << "Pokemon Name: ";
  143.     cin >> poke;
  144.  
  145.  
  146.     if(totalPokemon == 0){
  147.         temp1 = new Pokemon;
  148.         temp1->prev = NULL;
  149.         temp1->name = poke;
  150.         temp1->next = first;
  151.         first->prev = temp1;
  152.         first = temp1;
  153.     }
  154.     else{
  155.         temp2 = new Pokemon;
  156.         temp1 = new Pokemon;
  157.         while(temp2->next != NULL){
  158.             temp2 = temp2->next;
  159.         }
  160.         temp2->next = temp1;
  161.         temp1->prev = temp1;
  162.         temp1->next = NULL;
  163.     }
  164.  
  165. }
  166.  
  167. void PokemonList::displayList(Pokemon *current){
  168.     while(current != NULL)
  169.     {
  170.         cout << current->next << endl;
  171.         current = current->next;
  172.     }
  173. }
  174.  
  175. /********************************************
  176.             Main function    
  177.  ********************************************/
  178. int main() {
  179.  
  180.     int choice;
  181.     PokemonList pokeDex;            //create an empty list
  182.     Pokemon *current;               // the current pokemon you look at
  183.  
  184.     do {
  185.         cout << endl << "=====================================" << endl;
  186.         cout << "PokeDex" << endl;
  187.         cout << "=====================================" << endl;
  188.         cout << "Select your option:" << endl;
  189.         cout << "1) Insert new Pokemon" << endl;
  190.         cout << "2) Next Pokemon" << endl;
  191.         cout << "3) Prev Pokemon" << endl;
  192.         cout << "4) List all Pokemons" <<endl;
  193.         cout << "5) Quit" << endl;
  194.         cout << "=====================================" << endl;
  195.  
  196.         //YOUR CODE GOES HERE
  197.         //IF WE HAVE 1 POKEMON IN THE LIST
  198.         //ASSIGNING CURRENT POINTER TO THE FIRST POKEMON
  199.    
  200.         if (pokeDex.getTotal() > 0) {
  201.             cout << endl <<"Current Pokemon" <<endl;
  202.             current->displayPokemon();
  203.         }
  204.         cout << endl << "Please select your choice: ";
  205.         cin >> choice;
  206.  
  207.         switch(choice) {
  208.             case 1:
  209.                 pokeDex.insert();
  210.                 break;
  211.  
  212.             case 2:
  213.                 //IF THE LIST IS NOT EMPTY
  214.                 if(pokeDex.isEmpty() == false){
  215.                     current = current->getNext(); //ASSIGN CURRENT POINTER TO THE NEXT POKEMON
  216.                 }
  217.                 else{
  218.                     cout << "The list is empty";
  219.                 }
  220.  
  221.                 break;
  222.  
  223.             case 3:
  224.                 //IF THE LIST IS NOT EMPTY
  225.                 if(pokeDex.isEmpty() == false){
  226.                     current = current->getPrev();//ASSIGN CURRENT POINTER TO THE PREVIOUS POKEMON
  227.                 }
  228.                 else{
  229.                     cout << "The list is empty";
  230.                 }
  231.                 break;
  232.  
  233.             case 4:
  234.                 //IF THE LIST IS NOT EMPTY
  235.                 if(pokeDex.isEmpty() == false){
  236.                     pokeDex.displayList(current); //DISPLAY ALL POKEMON IN THE LIST
  237.                 }
  238.                 else{
  239.                     cout << "The list is empty";
  240.                 }
  241.                 break;
  242.  
  243.             case 5:
  244.                 cout << "Terminating the super secret Pokemon list" << endl;
  245.                 break;
  246.             default:
  247.                 cout << "Invalid choice!!" << endl;
  248.         }
  249.     } while(choice != 5);
  250.  
  251.  
  252.     return 0;
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement