千月星跡

アイをも求めて彷徨っている孤独なヒーロー

原创 VC++多线程应用--代码清单二:链表收藏

新一篇: VC++多线程应用--代码清单三:事件TASK | 旧一篇: VC++多线程应用--代码清单一:节点

 
#ifndef _NOTE_H
#define _NOTE_H

#pragma once

#include 
".TokenElement.h"

class __declspec(dllexport) CNote
{
public:
    CNote();
    
~CNote();

public:
    US    key;
    CNote
*    header_p;
    CNote
*    tail_p;
    CTokenElement
*    element_p;
}
;

#endif

 

 

#ifndef _LINKLIST_H
#define _LINKLIST_H

#pragma once

#include 
".EventDefine.h"
#include 
".Note.h"

class __declspec(dllexport) CLinkList
{
public:
    CLinkList(
void){ rootnote_p = NULL; };
    
virtual ~CLinkList(void){};

    
    
virtual void Remove(CNote*= 0;
    
    
virtual bool GetHeader(CNote**= 0;
    
    
virtual void Free(void= 0;

protected:
    CNote
*    rootnote_p;
}
;


#endif/* _LINKLIST_H */

循环链表

 

#ifndef _CIRCULARLINKLIST_H
#define _CIRCULARLINKLIST_H

#pragma once

#include 
".LinkList.h"

class __declspec(dllexport) CCircularLinkList
    : CLinkList
{
public:
    CCircularLinkList(
void);
    
~CCircularLinkList(void);

    
/*     インサートheader    */
    
void InsertHeader(CTokenElement*);
    
/*     アペンドtail    */
    
void AppendTail(CTokenElement*);
    
/*     探す            */
    
bool Search(CTokenElement*, CNote**);
    
bool Search(OBJECT, CNote**);
    
/*     削除            */
    
void Remove(CNote*);
    
void Remove(CTokenElement*);
    
void Remove(OBJECT);

    
/*     GetNext        */
    CNote
* GetNext(CNote*);
    
/*     GetPrevious    */
    CNote
* GetPrevious(CNote*);
    
/*     GetHeader        */
    
bool GetHeader(CNote**);
    
/*     GetTail        */
    
bool GetTail(CNote**);
    
/*     押し込み        */
    
void PushIn(CNote*);
    
/*     押し出し        */
    
void PullOut(CNote*);
    
/*    解放            */
    
void Free(void);
}
;


#endif/* _CIRCULARLINKLIST_H */

 

 

#include "StdAfx.h"
#include 
".CircularLinkList.h"

CCircularLinkList::CCircularLinkList(
void)
{
}


CCircularLinkList::
~CCircularLinkList(void)
{
    Free();
}


/*node input linklist*/
void CCircularLinkList::InsertHeader(CTokenElement* element_p)
{
    CNote
* note_p;

    note_p 
= new CNote();

    
if (NULL == rootnote_p){
        rootnote_p 
= note_p;
        note_p
->header_p = note_p;
        note_p
->tail_p = note_p;
        note_p
->element_p = element_p;
    }

    
else{
        note_p
->header_p = rootnote_p->header_p;
        note_p
->tail_p = rootnote_p;
        note_p
->element_p = element_p;

        rootnote_p
->header_p->tail_p = note_p;
        rootnote_p
->header_p = note_p;
        
/*give head note*/
        rootnote_p 
= note_p;
    }


}


void CCircularLinkList::AppendTail(CTokenElement* element_p)
{
    CNote
* note_p;

    note_p 
= new CNote();

    
if (NULL == rootnote_p){
        rootnote_p 
= note_p;
        note_p
->header_p = note_p;
        note_p
->tail_p = note_p;
        note_p
->element_p = element_p;    
    }

    
else {
        note_p
->header_p = rootnote_p->header_p;
        note_p
->tail_p = rootnote_p;
        note_p
->element_p = element_p;

        rootnote_p
->header_p->tail_p = note_p;
        rootnote_p
->header_p = note_p;
    }

}


bool CCircularLinkList::Search(CTokenElement* element_p, CNote** node_dp)
{
    
bool        result;
    CNote
*        note_p;
    OBJECT      instance_p;

    result 
= false;
    instance_p 
= element_p->Get();
    
if (NULL != rootnote_p){
        note_p 
= rootnote_p;
        
while(NULL != note_p){
            
/*check node value*/
            
if (instance_p == note_p->element_p->Get()){
                
*node_dp = note_p;
                result 
= true;
                
break;
            }

            
/*get next*/
            note_p 
= rootnote_p->tail_p;
            
/*one circle passed*/
            
if(rootnote_p == note_p){
                
break;
            }

        }

    }

    
return result;
}


bool CCircularLinkList::Search(OBJECT object_p, CNote** node_dp)
{
    CNote
*    note_p;
    
bool result = false;

    
if (NULL != rootnote_p){
        note_p 
= rootnote_p;
        
while (NULL != note_p){
            
if (object_p == note_p->element_p->Get()){
                
*node_dp = note_p;
                result 
= true;
                
break;
            }

            
/*get next*/
            note_p 
= rootnote_p->tail_p;
            
/*one circle passed*/
            
if (note_p == rootnote_p){
                
break;
            }

        }

    }

    
return result;
}


void CCircularLinkList::Remove(CNote* note_p)
{
    
if(NULL != note_p && NULL != rootnote_p){
        
if (note_p == rootnote_p){
            
if (note_p == rootnote_p->tail_p){
                rootnote_p 
= NULL;
            }

            
else{
                rootnote_p
->header_p->tail_p = note_p->tail_p;
                rootnote_p
->tail_p->header_p = note_p->header_p;
                rootnote_p 
= note_p->tail_p;
            }

        }

        
else{
            note_p
->header_p->tail_p = note_p->tail_p;
            note_p
->tail_p->header_p = note_p->header_p;
        }

        
/*remove the node*/
        delete note_p
->element_p;
        delete note_p;
    }
    
}


void CCircularLinkList::Remove(CTokenElement* element_p)
{
    CNote
*    note_p;
    
    
if ( Search(element_p,&note_p) ){
        Remove(note_p);
    }

}


void CCircularLinkList::Remove(OBJECT object_p)
{
    CNote
*    note_p;
    
    
if ( Search(object_p,&note_p) ){
        Remove(note_p);
    }

}


CNote
* CCircularLinkList::GetNext(CNote* node_p)
{
    
return node_p->tail_p;
}


CNote
* CCircularLinkList::GetPrevious(CNote* node_p)
{
    
return node_p->header_p;
}


bool CCircularLinkList::GetHeader(CNote** node_dp)