www.pudn.com > my2cadtest07261930.rar > list.h, change:2006-07-25,size:3841b


#ifndef _CLIST_ 
#define _CLIST_ 
 
template<class Type> class CList; 
template<class Type> 
class ListItem 
{ 
	 friend class CList<Type>; 
public: 
	ListItem( Type newone ) 
		:node(newone),next(NULL) 
	{ 
	} 
public: 
    Type node; 
    ListItem *next; 
}; 
 
template < class Type > 
class CList 
{ 
private: 
    int m_iCount; 
    ListItem<Type> *m_head; 
    ListItem<Type> *m_tail; 
    ListItem<Type> *m_tmp; 
     
    void CreateListItem(Type newone) 
    { 
        m_tmp = new ListItem<Type>(newone); 
    } 
public: 
    CList(): m_head(NULL),m_tail(NULL),m_iCount(0),m_saved(false),m_edited(false){} 
	~CList() 
	{ 
		DeleteAll(); 
	} 
     
    int GetCount()const  
	{  
		return m_iCount; 
	} 
     
	void Increase() 
	{  
		m_iCount++;  
	} 
    void Decrease()  
    { 
        if( m_iCount > 0 ) 
        { 
            m_iCount--; 
        } 
         
    } 
     
    void AddTail(Type newone) 
    { 
        CreateListItem(newone); 
         
        if( m_head == NULL ) 
        { 
            m_head = m_tail = m_tmp; 
        } 
        else 
        { 
            m_tail->next = m_tmp; 
            m_tail = m_tmp; 
        } 
        Increase(); 
    } 
     
    void AddHead(const Type newone) 
    { 
        CreateListItem(newone); 
        m_tmp->next = m_head; 
        m_head = m_tmp; 
        Increase(); 
    } 
 
    void InsertNode(Type newone,int iPosition) 
    { 
        ListItem<Type> *ptmp = m_head;  
        CreateListItem(newone); 
        for(int iPos = 0; 
        iPos! = iPosition&&ptmp!=NULL; iPos++,ptmp = ptmp->next ); 
    	if( ptmp != NULL ) 
	    { 
		    m_tmp->next = ptmp->next; 
		    ptmp->next = m_tmp; 
    	} 
        Increase(); 
    } 
 
    ListItem<Type> *GetNode( int iPosition) 
    { 
        ListItem<Type> *ptmp = m_head; 
        for(int iPos = 0; iPos != iPosition && ptmp!=NULL;  
        iPos++,ptmp = ptmp->next ); 
         
        return ptmp; 
 
    } 
 
	ListItem<Type>& operator[](int iPosition)   
	{	 
        return *GetNode(iPosition); 
	} 
 
	bool operator == ( CList<Type>& rhs ) 
	{ 
		bool result( true ); 
		int iCount = rhs.GetCount(); 
        for( int i = 0 ; i < iCount ; i++ ) 
        { 
			if ( !( GetNode(i)->node == rhs[i].node) ) 
			{ 
				result = false; 
				break; 
			}             
        } 
		return result; 
 
	} 
 
    CList<Type>& operator = ( CList<Type>& rhs ) 
    { 
        DeleteAll(); 
        int iCount = rhs.GetCount(); 
        for( int i = 0 ; i < iCount ; i++ ) 
        { 
            AddTail(rhs[i].node); 
        } 
        return *this; 
 
    } 
 
    void DeleteNode(int iPosition) 
    { 
        ListItem<Type> *ptmp = m_head; 
        ListItem<Type> *prv  = m_head; 
        for( int iPos = 0; iPos != iPosition && ptmp!=NULL;  
        prv = ptmp, ptmp = ptmp->next) 
        { 
            iPos++; 
        } 
         
        if( ptmp == NULL )      //没找到相应的节点 
        { 
            return; 
        } 
 
        if( ptmp == m_head ) 
        { 
            m_head = ptmp->next; 
        } 
        else 
        { 
            prv->next = ptmp->next; 
           
        } 
        delete ptmp; 
        Decrease(); 
		 
    } 
 
    void DeleteAll() 
    { 
		int iCount = m_iCount; 
        for ( int i =  0 ; i < iCount ; i++ ) 
        { 
          DeleteNode(0); 
        } 
        
    } 
 
   bool GetEditStatus() 
   { 
     return m_edited; 
   } 
   bool GetSaveStatus() 
   { 
     return m_saved; 
   } 
   void SetEditStatus(bool bEdit) 
   { 
     m_edited=bEdit; 
   } 
   void SetSaveStatus(bool bSave) 
   { 
     m_saved=bSave; 
   } 
 
   void SetbgColor(COLORREF &bgcolor) 
   { 
      m_bgcolor=bgcolor; 
   } 
   COLORREF GetbgColor() 
   { 
     return m_bgcolor; 
   } 
 
private: 
	bool m_edited; 
	bool m_saved; 
    COLORREF m_bgcolor; 
	 
}; 
#endif