All pastes #734231 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-10-12 14:49 UTC
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);
}

List::~List() {	
	if (this->ptr)
		delete this->ptr;
	
	if (this->next)
		delete this->next;
}

void List::append(List *next) {
	next->next = this->next;
	this->next = next;
}

void List::setData(Data *ptr) {
	this->ptr = ptr;
}

#ifdef TESTING

#include <iostream>

class SubData : public Data {

public:
	SubData(char ch) {this->data = ch;}
	SubData(SubData *old) { this->data = old->data; } 
	char data;

};

class SubList : public List {

public:
	SubList(SubList *);
	SubList(SubData *);

	SubData *ptr;
	SubList *next;
	SubList *prev;
};

SubList::SubList(SubList *old) {
	if (old->next)
		this->next = new SubList(old->next);
	
	if (old->ptr)
		this->ptr = new SubData(old->ptr);
}

using namespace std;

int main() {
	SubList *test, *iterator;
	
	test = new SubList(new SubData('a'));
	
	for (int i = 0;i < 10;i++) {
		test->append(new SubList(new SubData('b' + i)));
	}
	
	do
		cout << iterator->ptr->data;
	while (iterator = iterator->next);
	
	delete test;
	
	return 0;
}

#endif