Please help check an old code:
#include <iostream>
using namespace std;
template <class C>
class List {
//Linked list of C.
template<class U>
friend class ListItr;
private:
class ListEl {
public:
C val;
ListEl* next;
ListEl(const C& s, ListEl* n) : val(s) { next = n;}
};
public:
List() { head = tail = NULL; size = 0;}
~List() {
if(head) {
ListEl *p=head;
for(ListEl *q=p->next;q;p=q,q=p->next)
delete p;
delete p;
}
}
void add(const C& s) {
size++;
if(head)
tail = tail->next = new ListEl(s,NULL);
else
head = tail = new ListEl(s,NULL);
}
int getSize() const { return size; }
private:
ListEl *head, *tail;
int size;
};
template <class U>
class ListItr {
// List iterator.
private:
List::ListEl<U> * current; // Here: why not work ??
public:
ListItr(List<U>& list) { current = list.head; }
int more() { return current!=NULL; }
U& next() { U& v = current->val; current = current->next; return v;
}
};
// Simple example uses type int
main()
{
List<int> L;
L.add(0); // Insert a new element
L.add(0); // Insert a new element
L.add(5);
L.add(6);
cout << endl;
return 0;
}
Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.
Please review the rules, which you agreed to when you registered, if you have not already done so.
Thank You.
The UNIX and Linux Forums.