Creating the methods for class List
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';
List lc; // create list lc
lc.push_back( "money" );
lc.push_front( "send" );
cout << "\nlc contains:\n" << lc << '\n';
List ld; // create list ld
cout << "\nld contains nothing:\n" << ld << '\n';
ld.push_front( "hi" );
cout << "ld contains:\n" << ld << '\n';
ld.pop_front( );
cout << "ld contains nothing:\n" << ld << '\n';
ld.push_back( "hello" );
ld.push_back( "Bubba" );
cout << "ld contains:\n" << ld << '\n';
ld.pop_front( );
cout << "ld contains:\n" << ld << '\n';
ld.pop_front( );
cout << "ld contains nothing:\n" << ld << '\n';
List le( ld );
cout << "le contains nothing:\n" << le << '\n';
le.push_back( "last" );
cout << "le contains:\n" << le << '\n';
return 0;
}
List.h
Code:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Node
{
public:
// constructor
Node( const string &, Node *, Node * );
const string &get_word( ) const;// get a const reference to word
Node *get_next( ) const; // get a Node * (value of next)
Node *get_prev( ) const; // get a Node * (value of prev)
Node *set_next( Node * ); // set next to a new value
Node *set_prev( Node * ); // set prev to a new value
private:
string word;
Node *next;
Node *prev;
};
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 & );
// pop a node from the back of list
void pop_back( );
// pop a node from the front of list
void pop_front( );
// member function for the optional part
List &operator =( const List & );
// output a list object
friend ostream &operator <<( ostream &, const List & );
Node *begin( ) const; // pointer to beginning of the list
Node *end( ) const; // pointer to end of the list
class Iterator
{
public:
Iterator( Node * );
// same as word( )
const string &operator *( ) const;
void operator ++( ); // same as next( )
bool operator !=( Node * ) const;
private:
Node *current;
};
private:
Node *head;
Node *tail;
void copy_list( const List & );// copy a linked list
void delete_list( ); // delete a linked list
};
methods.cpp
Code:
#include "List.h"
/**************************************methods for Node**************************************/
// constructor: init. a Node
Node::Node( const string &s, Node * n, Node * p )
{
word = s; // init. word with a copy of s
next = n; // next points to n
prev = p; // prev points to p
}
// return a const ref to word
const string &Node::get_word( ) const
{
return (*this).word;
}
// return a pointer to next node
Node *Node::get_next( ) const
{
return (*this).next;
}
Node *Node::get_prev( ) const
{
return (*this).prev;
}
// sets next to pointer n and return next
Node *Node::set_next( Node * n )
{
next = n; // next now points to n
return (*this).next;
}
// sets prev to pointer p and return prev
Node *Node::set_prev( Node * p )
{
prev = p; // prev now points to p
return (*this).prev;
}
/**************************************methods for List**************************************/
List::List( ) // constructor: init. head and tail
{
cout << "List::List( )\n";
head = tail = 0;
}
List::List( const List &rhs )// copy constructor
{
copy_list( rhs );
}
List::~List( ) // destructor: delete the list
{
cout << "List::~List( )\n";
delete_list( );
}
void List::copy_list( const List &rhs )
{
head = tail = 0;
// copy rhs' list into this
for( Node *p = rhs.begin(); p != end(); p = p->get_next( ) )
{
push_back( p->get_word( ) );
}
}
void List::push_back( const string &s )
{
// p points to a new node
Node *p = new Node( s, 0, tail );
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::pop_back( )
{
if( tail ) // tail points to a node?
{
Node *tmp = tail;
tail = tail->get_prev( );
delete tmp; // delete node to be removed
if( tail == 0 ) // no more elements in the list?
{
head = 0;
}
else
{
tail->set_next( 0 );
}
}
}
void List::delete_list( ) // delete a linked 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_front( const string &s )
{
// p points to a new node
Node *p = new Node( s, 0, head );
if( head == 0 ) // head not pointing to a node?
{
tail = head = p;
}
else
{ // tail->next points to new node
tail->set_next( p );
tail = p; // tail points to last node in the list
}
}
void List::pop_front( )
{
if ( head )
{
Node *tmp = head;
head = head->get_next( );
delete tmp; // delete node to be removed
if( tail == 0 ) // no more elements in the list?
{
head = 0;
}
else
{
tail->set_next( 0 );
}
}
}
// method for the optional
//List &List::operator =( const List &rhs )
//{
// head = tail = 0;
// // . . .
// if( this != &rhs ) // left and right objects are NOT the same?
// {
// delete [ ] this; // delete stack that belongs to left side object
//
// head = rhs.head;
// tail = rhs.tail;
// // now copy right side object to left side object
// for( Node *p = rhs.head; p; p = p->get_next( ) )
// {
// push_back( p->get_word( ) );
// }
// }
//
// return *this; // return a reference to left-hand side object
//}
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;
}
Node *List::begin( ) const // beginning of a linked list
{
return head;
}
Node *List::end( ) const // end of a linked list
{
return 0;
}
/**************************************methods for Iterator**************************************/
// constructor: init. an Iterator
List::Iterator::Iterator( Node * p )
{
current = p; // current points to p
}
// get a const ref to word
const string &List::Iterator::operator *( ) const
{
return current->get_word( );
}
// get a const ref to word
void List::Iterator::operator ++( )
{
if( current )
{
current = current->get_next( );
}
}
// current != p
bool List::Iterator::operator !=( Node * p ) const
{
return current != p;
}
Output should look like this:
List::List( )
la contains:
00000000 hi 00348750
00348990 mom 003487E0
00348750 please 00348870
003487E0 send 00348900
00348870 money 00000000
lb contains:
00000000 hi 00348CA8
00348A98 mom 00348D00
00348CA8 please 00348D90
00348D00 send 00348E20
00348D90 money 00000000
lb contains:
00000000 dear 00348A98
00348CA8 mother 00348D00
00348A98 please 00348D90
00348D00 send 00348E20
00348D90 money 00348EE8
00348E20 Bubba 00000000
List::List( )
lc contains:
00000000 send 00348F78
00349008 money 00000000
List::List( )
ld contains nothing:
ld contains:
00000000 hi 00000000
ld contains nothing:
ld contains:
00000000 hello 00349128
00349098 Bubba 00000000
ld contains:
00000000 Bubba 00000000
ld contains nothing:
le contains nothing:
le contains:
00000000 last 00000000
List::~List( )
deallocating 00349098 with last next is 00000000
List::~List( )
deallocating 00349128 with last next is 00000000
List::~List( )
deallocating 00349008 with send next is 00348F78
deallocating 00348F78 with money next is 00000000
List::~List( )
deallocating 00348CA8 with dear next is 00348A98
deallocating 00348A98 with mother next is 00348D00
deallocating 00348D00 with please next is 00348D90
deallocating 00348D90 with send next is 00348E20
deallocating 00348E20 with money next is 00348EE8
deallocating 00348EE8 with Bubba next is 00000000
List::~List( )
deallocating 00348750 with last next is 00000000