Within methods.cpp I am working on the method for push_front for class List. The strings are being displayed backwards and all the string values from push_back disappear.
I'm not sure if (tail) is pointing to the wrong value which is causing the rest of the output from push_back to disappear.
It is supposed to look something like this:
List::List( )
la contains:
00348970 hi next: 00348750
00348750 mom next: 003487D8
003487D8 please next: 00348860
00348860 send next: 003488E8
003488E8 money next: 00000000
Instead it is displays "mom" first then "hi" and everything else disappears.
List.h
Code:#include <iostream> #include <iomanip> #include <string> using namespace std; class Node { public: // constructor Node( const string &, Node * ); const string &get_word( ) const;// get a const reference to word Node *get_next( ) const; // get a Node * (value of next) Node *set_next( Node * ); // set next to a new value private: string word; Node *next; }; class List { public: List( ); // constructor List( const List & ); // copy constructor ~List( ); // destructor // push a node to the back of list void push_back( const string & ); // push a node to the front of list void push_front( const string & ); void pop_front( ); // pop first element of list // output a list object friend ostream &operator <<( ostream &, const List & ); private: Node *head; Node *tail; };
main.cpp
Code:#include "List.h" int main( ) { List la; // create list la la.push_front( "mom" ); la.push_back( "please" ); la.push_back( "send" ); la.push_back( "money" ); la.push_front( "hi" ); cout << "\nla contains:\n" << la << '\n'; List lb( la ); // copy list la to lb cout << "lb contains:\n" << lb << '\n'; lb.pop_front( ); lb.pop_front( ); lb.push_front( "mother" ); lb.push_front( "dear" ); lb.push_back( "Bubba" ); cout << "lb contains:\n" << lb << '\n'; return 0; }
methods.cpp
Code:#include "List.h" /*******************************methods for Node*******************************/ // constructor: init. a Node Node::Node( const string &s, Node * p ) { word = s; // init. word with a copy of s next = p; // next points to p } // return a const ref to word const string &Node::get_word( ) const { return word; } // return a pointer to next node Node *Node::get_next( ) const { return next; } // sets next to pointer p and return next Node *Node::set_next( Node * p ) { next = p; // next now points to p return next; } /*******************************methods for List*******************************/ List::List( ) // constructor: init. head and tail { cout << "List::List( )\n"; head = tail = 0; } List::List( const List & ) { } List::~List( ) // destructor: deallocate the list { cout << "List::~List( )\n"; for( Node *p = head; p; ) { Node *tmp = p; // remember current pointer p = p->get_next( ); // advance p to the next Node delete tmp; // deallocate tmp cout << "deallocated\t" << tmp << "\tnext is\t" << p << '\n'; } } void List::push_back( const string &s ) { // p points to a new node Node *p = new Node( s, 0 ); if( tail == 0 ) // tail not pointing to a node yet? { head = tail = p; // head & tail point to new node in the list } else { // tail->next points to new node tail->set_next( p ); tail = p; // tail points to last node in the list } } void List::push_front( const string &s ) { Node *p = new Node( s, 0 ); if( tail == 0 ) { head = tail = p; } else { head->set_next( p ); tail = p; } } void List::pop_front( ) { } ostream &operator <<( ostream &out, const List &list ) { for( Node *p = list.head; p != 0; p = p->get_next( ) ) { cout << p << '\t' << setw( 8 ) << p->get_word( ) << '\t' << "next:" << '\t' << p->get_next( ) << '\n'; } return out; }