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)
{
    
bool result = false;

    
if (NULL != rootnote_p){
        
*node_dp = rootnote_p;
        result 
= true;
    }

    
return result;
}


bool CCircularLinkList::GetTail(CNote** node_dp)
{
    
bool result = false;

    
if (NULL != rootnote_p){
        
*node_dp = rootnote_p->tail_p;
        result 
= true;
    }

    
return result;
}


void CCircularLinkList::PushIn(CNote* node_p)
{
    
if (NULL == rootnote_p){
        rootnote_p 
= node_p;
        rootnote_p
->header_p = node_p;
        rootnote_p
->tail_p = node_p;
        
//rootnote_p->element_p = node_p->element_p;
    }

    
else{
        node_p
->header_p = rootnote_p->header_p;
        node_p
->tail_p = rootnote_p;
        rootnote_p
->header_p->tail_p = node_p;
        rootnote_p
->header_p = node_p;

    }

}


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

            
else{
                
//node_p->header_p = rootnote_p->tail_p->header_p;
                
//node_p->tail_p = rootnote_p->header_p->tail_p;
                node_p->tail_p->header_p = node_p->header_p;
                node_p
->header_p->tail_p = node_p->tail_p;
                rootnote_p 
= rootnote_p->tail_p;
            }

        }

        
else{
            
//node_p->header_p = node_p->tail_p->header_p;
            
//node_p->tail_p = node_p->header_p->tail_p;
            node_p->tail_p->header_p = node_p->header_p;
            node_p
->header_p->tail_p = node_p->tail_p;
        }

    }

}


void CCircularLinkList::Free(void)
{
    CNote
*    note_p;

    note_p 
= rootnote_p;
    
while (NULL != note_p){
        Remove(note_p);
        
//note_p = note_p->tail_p;
        note_p = rootnote_p;
    }

}

顺序链表

#include "StdAfx.h"
#include 
".OrdinalLinkList.h"

COrdinalLinkList::COrdinalLinkList(
void)
{
}


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


void COrdinalLinkList::Insert(US key, CTokenElement* element_p)
{
    CNote
* note_p;
    CNote
* temp_p;

    
if (Search(key,&note_p)){
        
/*the same key value*/
        delete note_p
->element_p;
        note_p
->element_p = element_p;
    }

    
else{
        note_p 
= new CNote();
        note_p
->element_p = element_p;
        note_p
->key = key;
        
if (NULL == rootnote_p){
            
/*node is null*/
            note_p
->header_p = NULL;
            note_p
->tail_p = NULL;
            rootnote_p 
= note_p;
        }

        
else if( key < rootnote_p->key ){
            
/*Input In LinkList's Head*/
            note_p
->tail_p = rootnote_p;
            rootnote_p
->header_p = note_p;
            rootnote_p 
= note_p;
            note_p
->header_p = NULL;
        }

        
else{
            
/*not Input In LinkList's Head*/
            temp_p 
= rootnote_p;
            
while (NULL != temp_p){
                
if (key < temp_p->key){
                    note_p
->header_p = temp_p->header_p;
                    note_p
->tail_p = temp_p;
                    temp_p
->header_p->tail_p = note_p;
                    temp_p
->header_p = note_p;
                }

                
////else if (key >= temp_p->key){
                
////    /*not find input position*/
                
////    break;
                
////}

                else if (NULL == temp_p->tail_p){
                    
/*input in end position,because key is bigest*/
                    note_p
->header_p = temp_p;
                    note_p
->tail_p = NULL;
                    temp_p
->tail_p = note_p;
                    
break;
                }

                
/*Go to Next Node*/
                temp_p 
= temp_p->tail_p;
            }

        }

    }

        
}


void COrdinalLinkList::Remove(CNote* node_p)
{
    
if (NULL != rootnote_p && NULL != node_p){
        
if (rootnote_p == node_p){
            
if (NULL == rootnote_p->tail_p){
                
/*only one node*/
                rootnote_p 
= NULL;
            }

            
else{
                
/*delete hander node*/
                rootnote_p 
= rootnote_p->tail_p;
                node_p
->header_p = NULL;
                node_p
->tail_p = NULL;
                rootnote_p
->header_p = NULL;
            }

        }

        
else{
            
/*delete node in LinkList*/
            node_p
->header_p->tail_p = node_p->tail_p;
            
if (NULL != node_p->tail_p){
                node_p
->tail_p->header_p = node_p->header_p;
            }

        }

        delete node_p
->element_p;
        delete node_p;
    }

}


void COrdinalLinkList::Remove(US key)
{
    CNote
* note_p;

    
if (Search(key, &note_p)){
        Remove(note_p);
    }
 
}


bool COrdinalLinkList::Search(US key, CNote** node_dp)
{
    CNote
* note_p;
    
bool result = false;
    
    
if (NULL != rootnote_p){
        note_p 
= rootnote_p;
        
while(note_p != NULL){
            
if (key == note_p->key){
                
*node_dp = note_p;
                result 
= true;
                
break;
            }

            note_p 
= note_p->tail_p;
        }

    }


    
return result;
}


bool COrdinalLinkList::Search(US key, CTokenElement** element_dp)
{
    CNote
* note_p;
    
bool result = false;
    
    
if (NULL != rootnote_p){
        note_p 
= rootnote_p;
        
while(note_p != NULL){
            
if (key == note_p->key){
                
*element_dp = note_p->element_p;
                result 
= true;
                
break;
            }

            note_p 
= note_p->tail_p;
        }

    }


    
return result;
}


bool COrdinalLinkList::GetHeader(CNote** node_dp)
{
    
bool result = false;

    
if (NULL != rootnote_p){
        
*node_dp = rootnote_p;
        result 
= true;
    }

    
return result;
}


void COrdinalLinkList::Free(void)
{
    CNote
* note_p;

    note_p 
= rootnote_p;
    
while (NULL != note_p){
        Remove(note_p);
        note_p 
= rootnote_p;
    }

}

 

 

#ifndef _ORDINALLINKLIST_H
#define _ORDINALLINKLIST_H

#pragma once
#include 
".LinkList.h"

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

    
void Insert(US, CTokenElement*);

    
void Remove(CNote*);
    
void Remove(US);
    
    
bool Search(US, CNote**);
    
bool Search(US, CTokenElement**);
    
    
bool GetHeader(CNote**);
    
    
void Free(void);
}
;


#endif/* _ORDINALLINKLIST_H */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进击的横打

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值