chain.h

来自「中国移动的短信网关程序」· C头文件 代码 · 共 94 行

H
94
字号
#ifndef CHAIN_H_LINSH_2004_02_24
#define CHAIN_H_LINSH_2004_02_24

#include <assert.h>
#include <list>

using namespace std;

template <class _Tp>
class Chain
{
public:
	int nChainLen;
	Chain(){nChainLen= 0;}
	~Chain();
	
	void Insert(int index,  _Tp * pNode);
	void Append( _Tp * pNode);
	_Tp * Find(int index);
	_Tp* Delete(int index);
	int Search(const _Tp* pNode);

protected:
	list<_Tp *>  m_list;
	typedef list<_Tp *>::iterator NODE_ITEM;
	
};

#include <algorithm>

template <class _Tp>
Chain<_Tp>::~Chain()
{
	list< _Tp*>::iterator iter;
	for ( iter = m_list.begin(); iter !=m_list.end(); iter ++ )
		delete ( *iter );
}

template <class _Tp>
void Chain<_Tp>::Insert(int index,  _Tp * pNode)
{
	list<_Tp* >::iterator iter = m_list.begin();
	advance( iter, index);
	m_list.insert( iter, pNode );
	nChainLen++;
}

template <class _Tp>
void Chain<_Tp>::Append( _Tp * pNode)
{
	m_list.push_back( pNode );
	nChainLen++;
}

template <class _Tp>
_Tp * Chain<_Tp>::Find(int index)
{
	if(!nChainLen)
		return NULL;
	list< _Tp*>::iterator iter = m_list.begin();
	advance( iter, index );
	return *iter;
}

template <class _Tp>
_Tp* Chain<_Tp>::Delete(int index)
{
	list< _Tp*>::iterator iter = m_list.begin();
	advance( iter, index );
	assert( iter != m_list.end() );
	_Tp* pVal = *iter;
	m_list.erase( iter );
	nChainLen--;
	return pVal;
}

template <class _Tp>
int Chain<_Tp>::Search(const _Tp* pNode)
{
	list< _Tp*>::iterator iter;
	int i = 0;
	for ( iter = m_list.begin(); iter != m_list.end(); iter ++ )
	{
		if( *iter == pNode )
			return i;
		i++;
	}
	return -1;
}



#endif

⌨️ 快捷键说明

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