C++封装的用于存放内存块的双向循环列表

C++有许多已经封装好的数据结构,但是当数据不是标准数据时,存在很大麻烦,如内存块时。

直接进入话题吧:

如题:

结构头文件

#include <stdio.h>
#include <stdlib.h>

#define  uint unsigned int

typedef struct databuf
{
	char *Addr ;
	unsigned int Len ;

	databuf *next;
	databuf *previous;
}databuf,*pdatabuf ;


class NetData
{
public:

	pdatabuf Data ;
	bool Lock ;

	NetData();

	~NetData();

	void Lockdata();
	void UnLockdata();
	void WaitUnLock() ;
    void Entity_entity(pdatabuf Node,char *Addr,uint Len);
    /* first is messy print */
    void Entity_print(pdatabuf Node);
    void PrintList(pdatabuf phead);
    /* Length 1 no data only head */
    int GetLength(pdatabuf phead);
    pdatabuf Before_Null_Node(pdatabuf phead);
    /* Create,return Node add */
    pdatabuf CreateNode(pdatabuf previous,char *Addr,uint Len);
    pdatabuf CreateNode_Head();
    /* Add Node between */
    void AddNode(pdatabuf pNode,pdatabuf pNode2,char *Addr ,uint Len);
    /* Delete next Node */
    bool DeleteNode(pdatabuf pNode);

private:

protected:

};

结构CPP文件



NetData::NetData()
:Lock(0)
{
	this->Data = CreateNode_Head() ;
}
NetData::~NetData()
{

}

void NetData::Lockdata()
{
    printf("Locked\n");
    this->Lock = 1 ;
}
void NetData::UnLockdata()
{
    printf("UnLocked\n");
    this->Lock = 0 ;
}
void NetData::WaitUnLock()
{
    while(this->Lock==1)
    {
        usleep(200000);
    }
    printf("UnLocked\n");
}
void NetData::Entity_entity(pdatabuf Node,char *Addr,uint Len)
{
    Node->Len = Len ;
    Node->Addr = (char*)malloc(sizeof(char)*Node->Len);
    memset(Node->Addr,0,Node->Len);
    memcpy(Node->Addr,Addr,Node->Len);
}
pdatabuf NetData::CreateNode_Head()
{
    pdatabuf pNode = (pdatabuf)malloc(sizeof(databuf));
    assert(pNode!=NULL);


    pNode->next = NULL ;
    pNode->previous = pNode;
    return pNode ;
}
/* first is messy print */
void NetData::Entity_print(pdatabuf Node)
{

}
void NetData::PrintList(pdatabuf phead)
{
    pdatabuf p = phead ;
    while(p!=NULL)
    {
        Entity_print(p);
        p = p->next ;
    }
}
/* Length 1 no data only head */
int NetData::GetLength(pdatabuf phead)
{
    pdatabuf p = phead ; int Length=0 ;
    while(p!=NULL)
    {
        Length ++ ;
        p = p->next ;
    }
    return Length ;
}
pdatabuf NetData::Before_Null_Node(pdatabuf phead)
{
    pdatabuf p = phead ;
    while(p->next!=NULL)
    {
        p=p->next ;
    }
    return p ;
}
/* Create,return Node add */
pdatabuf NetData::CreateNode(pdatabuf previous,char *Addr ,uint Len)
{
    pdatabuf pNode = (pdatabuf)malloc(sizeof(databuf));
    assert(pNode!=NULL);
    pNode->next = NULL ;
    pNode->previous = previous ;
    Entity_entity(pNode,Addr,Len);

    return pNode ;
}
/* Add Node between */
void NetData::AddNode(pdatabuf pNode,pdatabuf pNode2,char *Addr,uint Len)
{
    pdatabuf pNew = CreateNode(pNode,Addr,Len);
    pNode->next = pNew ;
    pNew->next = pNode2 ;
    //pNew->previous = pNode ;
}
/* Delete next Node */
bool NetData::DeleteNode(pdatabuf pNode)
{
    pdatabuf pDel = pNode->next ;
    if(pDel==NULL)
    {
        printf(" No Node to Delete ");
        return 0 ;
    }
    pNode->next = pDel->next ;
    if(pDel->next!=NULL)
    {
    	pDel->next->previous = pNode ;
    }

    pDel->previous = NULL ;
    pDel->next = NULL ;
    //free(pDel->Addr);
    free(pDel);
    return 1 ;
}


使用此封装好的结构,改变类的名字,或者重新申请即可。




  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值