JList - jues
获取函数的真实地址
C/C++指针笔记(1)

JList

jues posted @ 2013年8月20日 10:03 in 笔记(原创) , 1050 阅读

    以前经常用QT,发现里面的QList很好用,今天自己也写了一个方便在别的平台使用,在此分享一下,欢迎测试,转载和使用请注明头部作者相关信息。

如有错漏欢迎指出,谢谢!

 

/************************************************************************
function    :    JList
writer    :    jues
mail    :    jue_s@live.cn
home    :    http://jues.i11r.com
date    :    2013-08-20

 
    example    :    JList<type> test;
    
      eg1        :   JList<int>  test;
      eg2        :   JList<struct>  test;
      eg2        :   JList<class>  test;
      
************************************************************************/

// jlist.h: interface for the JList class.
//
//////////////////////////////////////////////////////////////////////
#include <string.h>

/**/
template <typename T>
class JList  
{
private:
    struct T_dat
    {
        T m_t;
        T_dat *p_up;
        T_dat *p_down;
    };
    private:
        T_dat *m_first;
        T_dat *m_last;
        unsigned long  m_count;
        
    public:
        /* init data */
        void init( void )
        {
            this->m_first = NULL;
            this->m_last = NULL;
            this->m_count = 0;
        }
        JList()    {  
            this->init();
        }
        ~JList(){  
            this->clear();
        };
    private:
        /* get index value */
        T_dat * _getAt( const unsigned long &index )
        {
            unsigned long i;
            T_dat *p_t;
            /**/
            
            if ( this->m_count <= index )
                return NULL;
            /* find */
            i = 0;
            p_t = this->m_first;
            while( index >= i )
            {
                p_t = p_t->p_down;
                i++;
            }
            return p_t;
        }
    public:
        /* add new data for class */
        unsigned long add( JList<T> &ts )
        {
            unsigned long count;
            unsigned long index;
            /**/
            count = ts.m_count;
            index = 0;
            while( count > index  )
            {
                this->add( ts.getAt( index ) );
                index++;
            }
            return this->m_count;
        }
        /* add new data for value*/
        unsigned long add( const T &t )
        {
            /* first one add */
            if ( NULL == this->m_last )
            {
                this->m_first = new T_dat;
                this->m_first->p_up = NULL;
                this->m_last = this->m_first;
                this->m_count = 1;
            }
            else
            {
                this->m_last->p_down = new T_dat;
                this->m_last->p_down->p_up = this->m_last;
                this->m_last = this->m_last->p_down;
                this->m_count++;
            }
            /* last add */
            this->m_last->m_t = t;
            this->m_last->p_down = NULL;
            /* return add  */
            return this->m_count;
        }
        /* get count */
        unsigned long getCount( void )
        {
            return this->m_count;
        }
        /* get index value */
        T getAt( const unsigned long &index )
        {
            T_dat *p_t;
            T t;
            /**/
            memset( &t,0,sizeof( t ) );
            p_t = this->_getAt( index );
            /* not find */
            if ( NULL == p_t )
                return t;
            /* finded */
            return ( p_t->m_t );
        }
        /* remove index value */
        void remove( const unsigned long &index )
        {
            T_dat *p_t;
            /**/
            p_t = this->_getAt( index );
            /* not find */
            if ( NULL == p_t )
                return;
            
            p_t->p_up->p_down = p_t->p_down;
            p_t->p_down->p_up = p_t->p_up;
            delete  p_t ;
            this->m_count--;
        }
        /* clear all */
        void clear( void )
        {
            T_dat *p_t;
            /**/
            while( NULL != this->m_last )
            {
                p_t = this->m_last;
                this->m_last = this->m_last->p_up;
                delete p_t ;
            }
            this->init();
        }
        /* find first one */
        T*  findFirst( const T &t )
        {
            T_dat *p_t;
            /**/
            p_t = this->m_first;
            while ( NULL != p_t )
            {
                if ( 0 == memcmp( &t,&( p_t->m_t ),sizeof( T ) ) )
                    return &( p_t->m_t );
                p_t = p_t->p_down;
            }
            return NULL;
        }
        /* find last one */
        T*  findLast( const T &t )
        {
            T_dat *p_t;
            /**/
            p_t = this->m_last;
            while ( NULL != p_t )
            {
                if ( 0 == memcmp( &t,&( p_t->m_t ),sizeof( T ) ) )
                    return &( p_t->m_t );
                p_t = p_t->p_up;
            }
            return NULL;
        }
};


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter