printing - Print function in my DoublyLinkedList creates duplicates. C++ -


basically print function print out contents when have 1 player object inserted list. when insert object, adds first element inserted has been overwritten. have tested insert , append functions , dont believe these functions. think print function printing last entered firstname,lastname,level , exp number of nodes in list. here code:

stats.cpp

#include "stats.h" #include <iostream> #include "validators.h" using namespace std;  validators validators2;  stats::stats() {     firstname = "";     secondname = "";     level = 0;     experience = 0; } stats::stats(string firstname,string secondname, int level, int experience) {     firstname = firstname;     secondname = secondname;     level = level;     experience = experience;  }     string stats :: getfirstname()     {         return firstname;     }     string stats :: getsecondname()     {         return secondname;     }     int stats :: getlevel()     {         return level;     }     int stats :: getexperience()     {         return experience;     }     stats stats :: input()     {         string inputfirstname = "please enter first name: ";         firstname = validators2.getstring(inputfirstname);         string inputsecondname = "please enter second name: ";         secondname = validators2.getstring(inputsecondname);         cout<< "please enter level: ";         level = validators2.getnum();         cout<< "please enter experience: ";         experience = validators2.getnum();          stats s1(firstname,secondname,level,experience);         return s1;      } 

main

int main () { //------------------------------------------------------------------------------------------- //  set linkedlist , iterator.  //-------------------------------------------------------------------------------------------     doublylinkedlist<stats> list;     doublylinkedlistiterator<stats> itr = list.getiterator();         list.insert(itr,stats.input());         list.print(itr); } //-------------------------------------------------------------------------------------------- //  name:           doublylinkedlist.h. //  description:    header file use in doublylinkedlist.cpp. //                  doublylinkedlist dynamically growing datastructure , use's //                  nodes , pointers in structure. //-------------------------------------------------------------------------------------------- #ifndef doublylinkedlist_h #define doublylinkedlist_h  #include <iostream> #include "validators.h" #include "stats.h"  using namespace std;  validators validators; stats stats;   //------------------------------------------------------------------------------------------- //  class declarations. //------------------------------------------------------------------------------------------- template<class datatype> class doublylinkedlist; template<class datatype> class doublylinkedlistnode;  template<class datatype> class doublylinkedlistiterator; //------------------------------------------------------------------------------------------- //  class:  doublylinkedlist. //------------------------------------------------------------------------------------------- template <class datatype> class doublylinkedlist {    public: //------------------------------------------------------------------------------------------- //  member vairables. //------------------------------------------------------------------------------------------- doublylinkedlistnode<datatype>* m_head; doublylinkedlistnode<datatype>* m_tail; int m_count; //------------------------------------------------------------------------------------------- //  name:           constructor. //  description:    constructs doublylinkedlist. //------------------------------------------------------------------------------------------- doublylinkedlist() {     //sets m_head default value of 0.  m_head first node in linkedlist.     m_head = 0;     //sets m_tail default value of 0.  m_tail last node in linkedlist.     m_tail = 0;     //sets m_count default value of 0.  m_count count elements in linkedlist.     m_count = 0; } //------------------------------------------------------------------------------------------- //  name:           destructor. //  description:    deletes doublylinkedlist. //------------------------------------------------------------------------------------------- ~doublylinkedlist() {     //temporary node pointer.     doublylinkedlistnode<datatype>* itr = m_head;     doublylinkedlistnode<datatype>* next = 0;      while(itr != 0)     {         //save pointer next node.          next = itr->m_next;         //delete current node.         delete itr;         //make next node current node.         itr = next;     } } int getcount() { return m_count; } // ------------------------------------------------------------------------------------- //  name:           append //  description:    adds node end of list, points newnode //  arguments:      p_data - data store in new node. //                  m_count increased. //  return value:   none. // -------------------------------------------------------------------------------------     void append(datatype p_data)     {         if(m_head == 0)         {             // create new head node.             m_head= m_tail= new doublylinkedlistnode<datatype>;             m_head->m_data= p_data;             m_head->m_next= 0;             m_head->m_prev= 0;         }         else         {             // insert new node after tail , reset tail.             m_tail->insertafter(p_data);             m_tail= m_tail->m_next;         }         m_count++;     }  //------------------------------------------------------------------------------------------- //  name:           print. //  description:    prints elements list along index. //-------------------------------------------------------------------------------------------     void print(doublylinkedlistiterator<datatype> m_itr)     {         for(m_itr.start();m_itr.valid();m_itr.forth())         {             cout << "------------------player------------------\n";             cout << "first name:\t\t" << stats.getfirstname() << "\n";             cout << "second name:\t\t" << stats.getsecondname() << "\n";             cout << "level:\t\t\t" << stats.getlevel() << "\n";             cout << "experience:\t\t" << stats.getexperience() << "\n";             cout << "------------------------------------------\n";         }     } // ---------------------------------------------------------------------------------------------------------------- //  name:           insert //  description:    inserts data before iterator, works whether iterator backwards of forwards //                  through list.inserts @ end of list if iterator invalid. //  arguments:      p_iterator: iterator insert before //                  p_data: data insert //  return value:   none. // ----------------------------------------------------------------------------------------------------------------     void insert(doublylinkedlistiterator<datatype>& p_itr, datatype p_data)     {         if(p_itr.m_node != 0)         {             // insert data before iterator             p_itr.m_node->insertbefore(p_data);              //if iterator @ head of list,             //reset head pointer             if(p_itr.m_node == m_head)             {                 m_head = m_head->m_prev;             }             // increment count             m_count++;         }         else         {             append(p_data);         }     }  //------------------------------------------------------------------------------------------- //  name:           getiterator //  description:    generates iterator pointing towards current head node //  arguments:      none. //  return value:   <datatype> iterator //-------------------------------------------------------------------------------------------     doublylinkedlistiterator<datatype> getiterator()     {         return doublylinkedlistiterator<datatype>(this, m_head);     } }; //------------------------------------------------------------------------------------------- //  class:  doublylinkednode. //------------------------------------------------------------------------------------------- template<class datatype> class doublylinkedlistnode { public: //------------------------------------------------------------------------------------------- //  member vairables. //-------------------------------------------------------------------------------------------     doublylinkedlistnode<datatype>* m_next;     doublylinkedlistnode<datatype>* m_prev;     datatype m_data;  // ---------------------------------------------------------------- //  name:           insertafter //  description:    adds node after current node. //  arguments:      p_data - data store in new node. //  return value:   none. // ----------------------------------------------------------------     void insertafter(datatype p_data)     {         //create new node.         doublylinkedlistnode<datatype>* newnode= new doublylinkedlistnode<datatype>;         newnode->m_data = p_data;         // make new node point next node.         newnode->m_next = m_next;         newnode->m_prev = this;          // make node before it, point new node.         //if(m_next != 0)         //m_next->m_prev= newnode;         m_next = newnode;     } // ---------------------------------------------------------------- //  name:           insertbefore //  description:    adds node before current node. //  arguments:      p_data - data store in new node. //  return value:   none. // ----------------------------------------------------------------     void insertbefore(datatype p_data)     {         //create new node         doublylinkedlistnode<datatype>* newnode = new doublylinkedlistnode<datatype>;         newnode->m_data = p_data;         //set new node pointers         newnode->m_next = this;         newnode->m_prev = m_prev;         //if theres node before it, make point new node         if(m_prev != 0)         m_prev = newnode;     } }; //------------------------------------------------------------------------------------------- //  class:  doublylinkediterator. //------------------------------------------------------------------------------------------- template <class datatype> class doublylinkedlistiterator { public: //------------------------------------------------------------------------------------------- //  member vairables. //-------------------------------------------------------------------------------------------     doublylinkedlistnode<datatype>* m_node;     doublylinkedlist<datatype>* m_list;     doublylinkedlistiterator(doublylinkedlist<datatype>* p_list= 0, doublylinkedlistnode<datatype>* p_node= 0)     {         m_list= p_list;         m_node= p_node;     }  // ------------------------------------------------------------------ //  name:           start //  description:    resets iterator beginning of list. //  arguments:      none. //  return value:   none. // ------------------------------------------------------------------     void start()     {         if(m_list!= 0)             m_node= m_list -> m_head;     }  // ---------------------------------------------------------------- //  name:           end //  description:    resets iterator end of list //  arguments:      none. //  return value:   none. // ----------------------------------------------------------------     void end()     {         if(m_list!= 0)             m_node = m_list->m_tail;     }  // ---------------------------------------------------------------- //  name:           forth //  description:    moves iterator forward 1 position //  arguments:      none. //  return value:   none. // ----------------------------------------------------------------     void forth()     {         if(m_node != 0)         {         m_node = m_node ->m_next;         }     }  // ---------------------------------------------------------------- //  name:           //  description:    moves iterator backward 1 position. //  arguments:      none. //  return value:   none. // ----------------------------------------------------------------     void back()     {         if(m_node!= 0)             m_node = m_node->m_prev;     }   // ---------------------------------------------------------------- //  name:           item //  description:    gets item iterator pointing to. //  arguments:      none. //  return value:   reference data in node. // ----------------------------------------------------------------     datatype& item()     {         return m_node->m_data;     } //----------------------------------------------------------------- //  name:           valid //  description:    determines if node valid. //  arguments:      none. //  return value:   true if valid // ----------------------------------------------------------------     bool valid()     {         return (m_node!= 0);     } };     #endif 

your print loop should this

   for(m_itr.start();m_itr.valid();m_itr.forth())     {         cout << "------------------player------------------\n";         cout << "first name:\t\t" << m_itr.item().getfirstname() << "\n";         cout << "second name:\t\t" << m_itr.item().getsecondname() << "\n";         cout << "level:\t\t\t" << m_itr.item().getlevel() << "\n";         cout << "experience:\t\t" << m_itr.item().getexperience() << "\n";         cout << "------------------------------------------\n";     } 

see how uses iterator values print.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -