Alright I''m copying this from a book and for some reason the linked list example is not working! When I try to do a simple iteration through the list it becomes infinite meaning that a NULL end pointer is not being assigned to the last element. What is wrong? Did I make a typo or is this the most worthless book example ever?!
class Link
{
public:
// Constructors / Destructors
Link ();
Link (int);
Link (const Link &);
~Link ();
// Mutators
void SetValue(int);
// Operations
void operator= (const Link &);
// Data
Link * next;
int data
};
void
Link::operator =(const Link & copyfrom)
{
next = copyfrom.next;
SetValue(copyfrom.data);
}
Link::Link(const Link & copy)
{
next = copy.next;
SetValue(copyfrom.data);
}
class myList
{
public:
// Constructors / Destructors
myList ();
~myList ();
// Methods
void Add (Link &);
void Delete (int);
bool Empty ();
void Print (ostream &);
// Data
typedef Link * Iterator;
Link * firstLink;
};
myList::myList()
{
firstLink = 0;
}
void
myList::Add(Link & add)
{
Link * newLink = &add
if(Empty())
{
firstLink = newLink;
}
else
{
newLink->next = firstLink;
firstLink = newLink;
}
}
// below causes a infinite loop because no null is found!
myList::Print(ostream & out)
{
for(Link * i = firstLink; i != 0; i->next)
{
out << i->data << endl;
}
}
I''ve included only the implementations that I think have the error but the code looks fine. What is wrong? I appreciate anyone who took the time to read this lengthy post. Thank you.