C++封裝的用于存放內(nèi)存塊的雙向循環(huán)列表
來源:易賢網(wǎng) 閱讀:810 次 日期:2014-09-05 17:13:32
溫馨提示:易賢網(wǎng)小編為您整理了“C++封裝的用于存放內(nèi)存塊的雙向循環(huán)列表”,方便廣大網(wǎng)友查閱!

C++有許多已經(jīng)封裝好的數(shù)據(jù)結(jié)構(gòu),但是當(dāng)數(shù)據(jù)不是標(biāo)準(zhǔn)數(shù)據(jù)時(shí),存在很大麻煩,如內(nèi)存塊時(shí)。

直接進(jìn)入話題吧:

如題:

結(jié)構(gòu)頭文件

#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:

};

結(jié)構(gòu)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 ;

}

更多信息請(qǐng)查看IT技術(shù)專欄

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:C++封裝的用于存放內(nèi)存塊的雙向循環(huán)列表
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)