C + + encapsulated two-way loop list for storing memory blocks

Source: Internet
Author: User
Tags assert bool data structures printf

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-&gt;lock = 1;
	} void Netdata::unlockdata () {printf ("Unlockedn");
This-&gt;lock = 0;
	} void Netdata::waitunlock () {while (this-&gt;lock==1) {usleep (200000);
printf ("Unlockedn");
	} void Netdata::entity_entity (Pdatabuf node,char *addr,uint Len) {node-&gt;addr = Addr;
Node-&gt;len = Len;
    } pdatabuf Netdata::createnode_head () {pdatabuf Pnode = (pdatabuf) malloc (sizeof (DATABUF));


    ASSERT (Pnode!=null);
    Pnode-&gt;next = NULL;
    pnode-&gt;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-&gt;next;
    }/* Length 1 No data only head */int netdata::getlength (Pdatabuf phead) {pdatabuf p = phead; int length=0; while (p!=null) {Length ++ ;
    p = p-&gt;next;
return Length;
    } pdatabuf Netdata::before_null_node (Pdatabuf phead) {pdatabuf p = phead;
    while (p-&gt;next!=null) {p=p-&gt;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-&gt;next = NULL;
    Pnode-&gt;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-&gt;next = pnew;
    Pnew-&gt;next = PNode2;
pnew-&gt;previous = Pnode;
    }/* Delete next Node */bool Netdata::D eletenode (Pdatabuf pnode) {pdatabuf Pdel = Pnode-&gt;next;
        if (pdel==null) {printf ("No Node to Delete");
    return 0;
    } pnode-&gt;next = Pdel-&gt;next; Pdel-&gt;next-&gt;previous = Pnode;
    pdel-&gt;previous = NULL;
    Pdel-&gt;next = NULL;
    Free (PDEL-&GT;ADDR);
    Free (Pdel);
return 1; }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.