I don't know if it can be usefull but I post what I did with the same exercise of the same book. If I remember, there was a problem with Display and for returning an array, so I replaced it by a vector.
Code:
#include <iostream>
using namespace std;
#include <string>
#include <vector>
template<class T>
class List
{
public:
List():head(0),tail(0),theCount(0){}
virtual~List();
void insert( T value );
void append( T value );
int is_present( T value) const;
int is_empty( ) const{ return head == 0; }
int count() const { return theCount; }
void List_Display();
T List_getValues()const;
vector<T> List_getValues_array()const;
void List_Count() { cout<<" Count is : "<<theCount<<endl;}
int List_Count1()const { return theCount;}
template<class T>
friend int operator== (const T& lhs,const T& rhs );
private:
class ListCell
{
public:
ListCell( T value, ListCell * cell = 0): val(value),
next(cell) {}
T val;
ListCell * next;
T getValue() const { return val; }
void display(ListCell * pt)
{cout<<" Value is : "<<pt->getValue()<<endl; }
};
ListCell * head;
ListCell * tail;
int theCount;
};
template<class T>
List<T> :: ~List()
{
ListCell * pt = head;
while(pt)
{
ListCell * tmp = pt;
pt =pt->next;
delete tmp;
}
head = 0;
tail = 0;
}
template<class T>
void List<T> :: insert(T value )
{
ListCell * pt = new ListCell( value, head );
if( head==0) tail = pt;
head = pt;
theCount++;
}
template<class T>
void List<T> :: append(T value )
{
ListCell * pt = new ListCell( value);
if( head==0) head = pt;
else {tail->next = pt;}
tail = pt;
pt->next = 0;
theCount++;
}
template<class T>
int List<T> :: is_present( T value ) const
{
if(head==0) return 0;
if(head->val==value) return 1; ListCell * pt = head->next;
for(;pt != tail; pt = pt->next )
if(pt->val==value) return 1;
return 0;
}
template<class T>
void List<T> :: List_Display()
{
ListCell * pt = head;
while(pt!=tail->next)
{
cout<<" Value is : "<<pt->getValue()<<endl;
pt = pt->next;
}
}
template<class T>
T List<T> :: List_getValues()const
{
T value;
T value_array[100];
ListCell * pt = head;
while(pt!=tail->next)
{
value += pt->getValue();
pt = pt->next;
}
for(int i=0;i<=theCount;i++){if(pt!=tail->next){value_array[i]=pt->getValue();pt=pt->next;}}
return value;
//return value_array;
}
template<class T>
vector<T> List<T> :: List_getValues_array()const
{
vector<T>val_vect;
T value_array[100];
ListCell * pt = head;
for( int i=0; i<=theCount; i++ )
{
if(pt!=tail->next)
{ value_array[i] = pt->getValue();
val_vect.push_back(value_array[i]);
cout<<" "<<value_array[i];
pt = pt->next; }
}
//return value_array[0];
return val_vect;
}
template<class T>
int operator== (const T& lhs,const T& rhs )
{//compare lengths first
T value1; T value2;
if( lhs.theCount != rhs.theCount )
return 0;//lengths differ
if( lhs.List_getValues()!= rhs.List_getValues() )
return 0;
return 1;//if they dont differ , they must match
}
class cat
{ };
int main()
{
List<string> test;
List<cat> test1;
List<int> test2;
test.List_Count();
test.append("qw");
test.List_Count();
test.insert("yu");test.List_Count();
test.append("az");test.List_Count();
test.insert("kim");test.List_Count();
test.List_Display();
cout<<'\n';
test2.List_Count();
test2.append(786);test2.List_Count();
test2.append(9000);test2.List_Count();
test2.List_Display();
cout<<'\n';
List<string> test3;
test3 = test;
test3.List_Display();
if( test3 == test ) cout<< " \n1111 ";
else cout<<" \n0000 ";
cout<<test.List_getValues();
vector<string>t = test.List_getValues_array();
vector<string>t3 = test3.List_getValues_array();
cout<<'\n';
for( int i = 0;
i < (test.List_Count1());
i++ )
cout<<t[i]<<" ";
if( t == t3 ) cout<< " test = test3 ...\n";
getchar(); return 0;
}