arraylist.h

来自「用你的语音Modem实现像电话一样通话的程序」· C头文件 代码 · 共 120 行

H
120
字号
#ifndef ARRAYLIST_H#define ARRAYLIST_H// designed for lists of track numberstemplate<class TYPE>class ArrayList{public:	ArrayList();	virtual ~ArrayList();			TYPE append(TYPE value);		remove();          // remove from end	remove(TYPE value);       // remove item containing	remove_number(int number);     // remove item numbered	remove_all();	          	sort();		TYPE* values;	int total, available;};template<class TYPE>ArrayList<TYPE>::ArrayList(){	total = 0;	available = 1;	values = new TYPE[available];}template<class TYPE>ArrayList<TYPE>::~ArrayList(){	delete [] values;}template<class TYPE>TYPE ArrayList<TYPE>::append(TYPE value)            // add to end of list{	if(total+1 > available) 	{		available *= 2;		TYPE* newvalues = new TYPE[available];		for(int i = 0; i < total; i++) newvalues[i] = values[i];		delete [] values;		values = newvalues;	}		values[total++] = value;	return value;}template<class TYPE>ArrayList<TYPE>::remove(TYPE value)                   // remove value from anywhere in list{	static int in, out;	for(in = 0, out = 0; in < total;)	{		if(values[in] != value) values[out++] = values[in++];		else 		{ 			in++; 		}	}	total = out;}template<class TYPE>ArrayList<TYPE>::remove(){	total--;}template<class TYPE>ArrayList<TYPE>::remove_number(int number)                   // remove value from anywhere in list{	static int in, out;		for(in = 0, out = 0; in < total;)	{		if(in != number) values[out++] = values[in++];		else  in++;       // need to delete it here	}	total = out;}template<class TYPE>ArrayList<TYPE>::remove_all(){	total = 0;}template<class TYPE>ArrayList<TYPE>::sort()                    // sort from least to greatest value{	int result = 1;	TYPE temp;	while(result)	{		result = 0;		for(int i = 0, j = 1; j < total; i++, j++)		{			if(values[j] < values[i])			{				temp = values[i];				values[i] = values[j];				values[j] = temp;				result = 1;			}		}	}}#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?