C++封裝的用於存放記憶體塊的雙向迴圈列表

來源:互聯網
上載者:User

  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);    /* 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){}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 ;}/* 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 ;    pDel->next->previous = pNode ;    pDel->previous = NULL ;    pDel->next = NULL ;    free(pDel->Addr);    free(pDel);    return 1 ;}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.