www.pudn.com > 20050921094855818.zip > list.h


/* 
 *  A generic template list class.   
 *  Fairly typical of the list example you would  
 *  find in any c++ book. 
 */ 
#ifndef GENERIC_LIST_H 
#define GENERIC_LIST_H 
 
#include  
#include  
 
template  class List { 
	public: 
				List(int s=0); 
				~List(); 
		void	allocate(int s); 
		void	SetSize(int s); 
		void	Pack(); 
		void	Add(Type); 
		void	AddUnique(Type); 
		int 	Contains(Type); 
		void	Remove(Type); 
		void	DelIndex(int i); 
		Type *	element; 
		int		num; 
		int		array_size; 
		Type	&operator[](int i){assert(i>=0 && i 
List::List(int s){ 
	num=0; 
	array_size = 0; 
	element = NULL; 
	if(s) { 
		allocate(s); 
	} 
} 
 
template  
List::~List(){ 
	delete element; 
} 
 
template  
void List::allocate(int s){ 
	assert(s>0); 
	assert(s>=num); 
	Type *old = element; 
	array_size =s; 
	element = new Type[array_size]; 
	assert(element); 
	for(int i=0;i 
void List::SetSize(int s){ 
	if(s==0) { if(element) delete element;} 
	else {	allocate(s); } 
	num=s; 
} 
template  
void List::Pack(){ 
	allocate(num); 
} 
 
template  
void List::Add(Type t){ 
	assert(num<=array_size); 
	if(num==array_size) { 
		allocate((array_size)?array_size *2:16); 
	} 
	//int i; 
	//for(i=0;i 
int List::Contains(Type t){ 
	int i; 
	int count=0; 
	for(i=0;i 
void List::AddUnique(Type t){ 
	if(!Contains(t)) Add(t); 
} 
 
 
template  
void List::DelIndex(int i){ 
	assert(i 
void List::Remove(Type t){ 
	int i; 
	for(i=0;i