Introduction to OOP: Chapter 15 : Case Study: Container Classes [next] [previous] [audio] [real] [text]

A Template List Class

template <class T>
class List {
		// constructors
	List();

		// operations
	void Add(T v)
		{ firstElement = new Link<T>(v, firstElement); }

	int isEmpty()
		{ return firstElement == 0; }

	T First()
		{ return firstElement->value; }

	void RemoveFirst()
		{ Link<T> * p = firstElement;
		  firstElement = p->next;
		  delete p; }

	int Includes(T v)
		{ for (Link<T> * p = firstElement; p; p = p->next)
			if (v == p->value) 
				return 1;
		  return 0; }
};

template <class T>
class Link {
	// constructor
	Link (T v, Link * n) : value(v), next(n) { }

	// data fields
	Link * next;
	T value;
};
Intro OOP, Chapter 15, Slide 15