模板实现头文件:
#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) #endif template<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 = _value ; pnext = NULL ; } template <typename T> LinkNode<T> ::LinkNode(const LinkNode<T> &_linknode ) { data = _linknode .data ; //!!!!operator == pnext = NULL ; } template <typename T> LinkNode< T > ::~LinkNode(){ } ; template <typename T> ostream & operator<< (ostream & _cout , const LinkNode<T>& _linknode ) { _cout<<"link node data : "<<_linknode .data ; //!!!!T operator << return _cout ; } template <typename T> LinkNode<T> & LinkNode< T > ::operator=(const LinkNode< T > &_right) { data = _right .data ; pnext = _right .pnext ; return *this ; } template< typename T > class LinkList { private: LinkNode<T> *linkhead ; size_t linklength ; LinkNode <T> *find_ins_pos(size_t _ins_pos ) ; int copydata( const LinkList<T> & _ls , size_t _start , size_t _end ) ; public: LinkList( ); LinkList( const LinkList<T> & _linklist ) ; LinkList<T> & operator= ( const LinkList<T> & _linklist ) ; ~LinkList() ; int insert(size_t _ins_pos , const T & _value) ; int remove(size_t _ins_pos , T & _value ) ; int find (const T & _value) const ; int replace(size_t _re_pos , const T & _value); T & get(size_t _pos ) const ; bool isempty( ) { return linklength == 0 } ; int get_link_length( ) const { return linklength ; } ; }; template <typename T> LinkList<T>::LinkList( ) { linkhead = NULL ; linklength = 0 ; } template <typename T> LinkList<T> ::LinkList(const LinkList<T> & _ls) { linkhead = NULL ; linklength = 0 ; copydata(_ls , 0 , _ls .get_link_length( ) ) ; } template <typename T> T &LinkList<T> ::get(size_t _pos ) const { LinkNode<T> *pln_temp = linkhead ; size_t i = 0 ; while(pln_temp != NULL && i < _pos ) { pln_temp = pln_temp ->pnext ; i++ ; } if(pln_temp == NULL ) { cout<<"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 = pln_temp ->data ; // !!!!! T operator= linkhead = linkhead ->pnext ; delete pln_temp ; linklength-- ; return 0 ; } size_t i = 0 ; pln_temp = linkhead ; while(pln_temp != NULL && ( i < (_rm_pos - 1) ) ) { pln_temp = pln_temp ->pnext ; i++ ; } LinkNode<T> *pln_temp1 = pln_temp ->pnext ; pln_temp ->pnext = pln_temp1 ->pnext ; _value = pln_temp1 ->data ; delete pln_temp1 ; linklength-- ; return 0 ; } template <typename T> int LinkList<T> ::find(const T & value ) const { int i = 0 ; LinkNode<T> *pln = linkhead ; while(pln != NULL && pln->data != value ) //!!!!!operator!=() of T { pln = pln ->pnext ; i++ ; } if( pln!=NULL ) return i ; else return -1; } template <typename T> int LinkList<T> ::replace(size_t _re_pos , const T & _value) { T a_temp ; if(remove(_re_pos , a_temp) != 0 || insert(_re_pos , _value ) != 0 ) { cout<<"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 ; }
C++模板实现单向链表
时间: 2024-10-31 17:38:55