首页 > 代码库 > C++模板实现单向链表

C++模板实现单向链表

模板实现头文件:

#ifndef	SUN_LINKLIST_H#define  SUN_LINKLIST_H#include <iostream>using namespace std ;//#include "List.h"// T : operator= , operator!= , operator== , copy constructor , assign constructor #define SUN_LINKLIST_DEBUG#ifdef	SUN_LINKLIST_DEBUG						//pause #define	PAUSE (system("pause"))#else#define	PAUSE (0)#endiftemplate<typename T> class LinkList ; template<typename T> class LinkNode ; template <typename T> ostream& operator<<(ostream & _cout , const LinkNode<T>& _linknode ) ; template < typename T > class LinkNode //LinkNode type{public:	LinkNode( ) ; 	LinkNode( const T & _value ) ; 	LinkNode( const LinkNode<T>& _linknode ) ; 		virtual ~LinkNode() ;	LinkNode<T>& operator= (const LinkNode<T> & _right) ; 	friend ostream& operator<< <T>(ostream & _cout , const LinkNode<T> &linknode) ; 	friend class LinkList<T> ; private:	T data ; LinkNode<T> * pnext ; } ;template<typename T> LinkNode<T> ::LinkNode( ) :data( )												//!!!default constructor {	pnext = NULL ; }template<typename T> LinkNode<T>::LinkNode(const T & _value) {		data = http://www.mamicode.com/_value ; "link node data : "<<_linknode .data ;															//!!!!T operator << 	return _cout ; }template <typename T>  LinkNode<T> & LinkNode< T > ::operator=(const LinkNode< T > &_right){	data = http://www.mamicode.com/_right .data ; "get : _pos range ERROR"<<endl ; 		PAUSE; 	}	return pln_temp ->data ;										//operator=}template< typename T> int LinkList < T > ::insert(size_t _ins_pos , const T & _value){	LinkNode< T > *pln = NULL ; 	if(_ins_pos == 0 )						//insert to the first place of the link list	{		pln = new LinkNode< T >( _value ) ; 		pln ->pnext = linkhead ; 		linkhead = pln  ;		linklength++ ; 		return 0 ; 	}	pln = linkhead ; 	size_t i = 0 ; 	while( pln != NULL && ( i < (_ins_pos - 1 ) ) ) { pln = pln ->pnext ; i++ ; }		if( pln == NULL)			//insert range  ERROR	{		cout<< "the range of the inset position is ERROR"<<endl ; 		return  -1 ; 	}	LinkNode<T> *pln_temp = new LinkNode<T>(_value) ; 	pln_temp ->pnext = pln ->pnext ; 	pln ->pnext = pln_temp ; 	linklength++ ; 	return 0 ; }template <typename T> int LinkList < T > ::copydata(const LinkList< T > &_ls , size_t _start , size_t _end ) {	linkhead = NULL ;  linklength = 0 ; 		LinkNode< T > *pln = _ls .linkhead ; 	LinkNode< T > *phead = NULL;  		if(_ls .linklength == 0 ) { return 0 ; }						// link list empty return 	linkhead = new LinkNode<T>( *(_ls .linkhead ) ) ;  // not empty	pln = pln ->pnext ; phead = linkhead ; 	linklength++ ; 	while (pln != NULL )	{		phead ->pnext = new LinkNode<T> ( *( pln ) ) ;		phead  = phead ->pnext ; 		pln  = pln ->pnext ; 		linklength++ ; 	}	phead ->pnext = NULL ;										//the pnext of the link end is NULL 	return 0 ; }template <typename T> int LinkList<T> ::remove(size_t _rm_pos ,  T & _value ){	if( linkhead == NULL ||  _rm_pos < 0 || ( linklength - 1 < _rm_pos ) )	{		cout<<"remove : _rm_pos range error"<<endl ; 		return -1;	}	LinkNode<T> *pln_temp = linkhead ; 	if(_rm_pos == 0 )	{		_value = http://www.mamicode.com/pln_temp ->data ;    // !!!!! T operator="replace ERROR"<<endl ; 		return -1;  	}	return 0 ; }template <typename T > LinkList< T > ::~LinkList(){			cout<<"de_constructor"<<endl ;		LinkNode<T> *pln = linkhead ; 	while( linkhead !=NULL)	{		cout<<linkhead->data <<endl ; 		pln = linkhead ; 		linkhead = linkhead ->pnext ; 			delete pln ; 	}} #endif

 测试代码:

#include <iostream>using namespace std ; #include "LinkList.h"int main(int argc , char ** argv){		//测试友员类的访问权限		//LinkList< int > linktest ; 		//int a = 10 ; 		//linktest.funtest(a) ; 		//测试在linknode 中的重载"<<"操作符"函数		LinkNode<int> linknode(20) ;		cout<<linknode<<endl ; 		//测试LinkNode 的赋值操作符		LinkNode< int > linknode1 ; 		linknode1 = linknode ; 		cout<<linknode1<<endl ; 		//测试insert , get 函数		LinkList<int> ls ;		for(int i = 0 ; i <100  ; i++ ) { ls .insert(0 , i ) ; }		for(int i = 0 ; i < ls .get_link_length( ) ; i++ )		{			cout<<ls .get(i)<<endl ; 		}		//测试copy constructor 		cout<<"copy constructor"<<endl ;		LinkList<int> ls2(ls) ; 		for(int i = 0 ; i < ls2.get_link_length() ; i++ ) 		{			cout<<ls2 .get( i )<<endl ; 		}		//测试remove函数		cout<<"test remove"<<endl ; 		int i ;		ls2 .remove(0 , i) ; 		cout<<"i = "<<i<<endl ; 		for(int i = 0 ; i < ls2.get_link_length() ; i++ ) 		{			cout<<ls2 .get( i )<<endl ; 		}		ls2 .remove(1 , i ) ;		cout<<"i = "<<i<<endl ; 		for(int i = 0 ; i < ls2.get_link_length() ; i++ ) 		{			cout<<ls2 .get( i )<<endl ; 		}				//测试find()		cout<<"test find"<<endl ; 		int i1 = 0 ; 		cout<<ls .find( i1 )<<endl ; 		//测试replace		cout<<"replace"<<endl;		int i2 = -3 ; 		ls .replace(99 , i2) ; 		cout<< ls .get(99)<<endl ; 		system("pause") ; 		return 0 ; }