欢迎来到山村网

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

2019-03-02 09:56:36浏览:419 来源:山村网   
核心摘要:C++有许多已经封装好的数据结构,但是当数据不是标准数据时,存在很大麻烦,如内存块时。直接进入话题吧:如题:结构头文件#incl

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

直接进入话题吧:
如题:
结构头文件

#include <stdio.h>#include <stdlib.h>#define  uint unsigned inttypedef 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);        void Entity_print(pdatabuf Node);    void PrintList(pdatabuf phead);        int GetLength(pdatabuf phead);    pdatabuf Before_Null_Node(pdatabuf phead);        pdatabuf CreateNode(pdatabuf previous,char *Addr,uint Len);    pdatabuf CreateNode_Head();        void AddNode(pdatabuf pNode,pdatabuf pNode2,char *Addr ,uint Len);        bool DeleteNode(pdatabuf pNode);private:protected:};

结构CPP文件

Netdata::NetData():Lock(0){}Netdata::~NetData(){}void Netdata::Lockdata(){printf("Lockedn");this->Lock = 1 ;}void Netdata::UnLockdata(){printf("UnLockedn");this->Lock = 0 ;}void Netdata::WaitUnLock(){while(this->Lock==1){usleep(200000);}printf("UnLockedn");}void Netdata::Entity_entity(pdatabuf Node,char *Addr,uint Len){Node->Addr = Addr ;Node->Len = Len ;}pdatabuf Netdata::CreateNode_Head(){    pdatabuf pNode = (pdatabuf)malloc(sizeof(databuf));    assert(pNode!=NULL);    pNode->next = NULL ;    pNode->previous = pNode;    return pNode ;}void Netdata::Entity_print(pdatabuf Node){}void Netdata::PrintList(pdatabuf phead){    pdatabuf p = phead ;    while(p!=NULL)    {        Entity_print(p);        p = p->next ;    }}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 ;}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 ;}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 ;}bool Netdata::DeleteNode(pdatabuf pNode){    pdatabuf pDel = pNode->next ;    if(pDel==NULL)    {        printf(" No Node to Delete ");        return 0 ;    }    pNode->next = pDel->next ;    pDel->next->previous = pNode ;    pDel->previous = NULL ;    pDel->next = NULL ;    free(pDel->Addr);    free(pDel);    return 1 ;}
(责任编辑:豆豆)
下一篇:

ShellExecute指定IE浏览器打开网页

上一篇:

C#中获取路径的几种方法

  • 信息二维码

    手机看新闻

  • 分享到
打赏
免责声明
• 
本文仅代表作者个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,作者需自行承担相应责任。涉及到版权或其他问题,请及时联系我们 xfptx@outlook.com