rendered paste body#include "List.h"
List::List(Data *ptr) {
this->next = 0;
this->ptr = ptr;
}
List::List(Data *data, List *next) {
this->next = next;
this->ptr = data;
}
List::List(List *old) {
if (old->next)
this->next = new List(old->next);
else
this->next = 0;
}
List::~List() {
if (this->ptr)
delete this->ptr;
if (this->next)
delete this->next;
}
void List::append(List *ins) {
ins->next = this->next;
this->next = ins;
}
#ifdef TESTING
#include <iostream>
class SubData : public Data {
public:
SubData(char ch) : Data () {this->data = ch;}
SubData(SubData *old) : Data() { this->data = old->data; }
char data;
};
class SubList : public List {
public:
SubList(SubList *old) : List(old) {
if (old->next)
this->next = new SubList(old->next);
else
this->next = 0;
if (old->ptr)
this->ptr = new SubData(old->ptr);
}
SubList(SubData *data) : List(data) { this->ptr = data; }
void append(SubList *);
SubData *ptr;
SubList *next;
};
void SubList::append(SubList *ins) {
ins->next = this->next;
this->next = ins;
}
using namespace std;
int main() {
SubList *test, *iterator;
test = new SubList(new SubData('a'));
cout << "\tCreate a sublist\n";
for (int i = 0;i < 10;i++) {
test->append(new SubList(new SubData('b' + i)));
}
iterator = test;
cout << "done.\n\n\tIterate list\n";
do
cout << iterator->ptr->data << ",";
while (iterator = iterator->next);
cout << "\ndone.\n\n\tCopy list\n";
iterator = new SubList(test);
cout << "done.\n\n\tIterate List\n";
delete test;
test = iterator;
do
cout << iterator->ptr->data << ",";
while (iterator = iterator->next);
cout << "\ndone.\n";
delete test;
return 0;
}
#endif