All pastes #33644 Raw Edit

Someone

public text v1 · immutable
#33644 ·published 2005-12-14 13:06 UTC
rendered paste body
#include <assert.h>

template<class T>
class Link
{
public:
	Link*	prev, next;
	T	value;
public:
	Link(const T& v, Link* p = NULL, Link* n = NULL) : value(v), prev(p), next(n)
	{
	};
};

template<class T>
class CDList
{
protected:
	Link*	head, tail;
public:
	CDList(T value) : head(NULL), tail(NULL)
	{
		add(value);
	}

	void add(T value)
	{
		Link* element = new Link(value, tail, null);
		if(head == NULL)
			head = element;

		if(tail != NULL)
			tail->next = element;
		else
			tail = element;
	}

	T& getHead()
	{
		// because we return a reference, we can't return null if the list is empty
		// need to add an isEmpty() func.
		assert(head != NULL);

		if(head != NULL)
			return head->value;
	}

	void printBack()
	{
		Link* iterator = tail;
		while(iterator != NULL)
		{
			std::cout << iterator << " ";
			iterator = iterator->prev;
		}
	}
};