C + + has a number of already encapsulated data structures, but when the data is not standard data, there is a lot of trouble, such as memory block.
Go straight to the topic:
Title:
Structure header File
#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);
/* 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:
};
Structure CPP file
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;
}/* Messy print/void Netdata::entity_print (Pdatabuf Node) {} void Netdata::P rintlist (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 = C
Reatenode (Pnode,addr,len);
Pnode->next = pnew;
Pnew->next = PNode2;
pnew->previous = Pnode;
}/* Delete next Node */bool Netdata::D eletenode (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; }