CPTRRAY class based on two-way linked list with C ++

xiaoxiao2021-03-06  70

People who have used VC may have used all kinds of Array classes, which are often used, which makes me write a lot more convenient. It may also know that it is a way to do this, and this is unconfigured. Do not manage what software is to configure, huh, huh, let's joke, until today I don't really find this one. A clear explanation of the word. I have always understood it as "I don't need programmers to modify programs, debug people or users can complete new system monitoring." Even using DOS5.0 as a product of the operating system or if we do not have a product, we must configure it. I have developed an embedded configuration system in this time, using DOS5.0, the development environment is BorlandC 3.1. In order to implement my configuration function on such a system, it costs I have a lot of mind, one is to solve the display of the picture. The configuration system requires the user to design a picture, then we will design a modified monitoring screen. When the VC development background, I use the Array class to save the monitoring screen, but in BC31, how many years are not used After it, I don't know if there is an Array class. If there is anything, this class should use, find it again, it is better to write one, look like it, I am a standard code man, no matter what Dongdong I always want to write myself. But fortunately, there is no white study in the school's data structure in the school. The problem is soon solved. I used a two-way linked list to solve this problem, and it is too convenient to use it to achieve node's addition and deletion. Below is the source code of this class, posted here, I hope to be like me, love, love, and batch of friends who design.

The first is the definition of my data type. I am used to the VC. When I press VC's format, of course, I know that I am learning UCOS, huh, huh, this point I admitted that the great thinker Lu Xun education We: Take the essence

/ *

*********************************************************** ******

*

* (C) CopyRight 2003, Hu Wenjin

* All Right Reserved

*

*

* File: DATADEF.H

* Auther: hu wenjin

* DATA: 11.4.2003

*********************************************************** *******

* /

/ *

*********************************************************** *******

* Data Types Define

*********************************************************** *******

* /

#if! defined (_DataDef_h)

#define _DataDef_h

typedef signed long INT; typedef unsigned long DWORD; typedef INT BOOL; typedef unsigned char bool; typedef unsigned char BYTE; typedef unsigned short WORD; typedef float FLOAT; typedef unsigned long UINT; typedef unsigned long DWORD; typedef DWORD COLORREF; typedef char CHAR ; typedef short SHORT; typedef long LONG; typedef unsigned char INT8U; typedef signed char INT8S; typedef unsigned int INT16U; typedef signed int INT16S; typedef unsigned long INT32U; typedef signed long INT32S; typedef char * LPCTSTR; #define TRUE 1 # define False 0 # Define True 1 # define false 0typedef struct {long left; long.com;} Rect;

#ENDIF

Next, of course, it is .h file #if! Defined (_ptrarray_h) #define _ptrarray_h # include "datadef.h" / **************************** *********************************************************** ***

File: PTRARRAY.H Auther: huwenjin data: 9.27,2004

Discrption: Modification: 1.

*********************************************************** *********************** /

TypedEf struct {void * m_pobj; // Node included data pointer void * m_pparent; // Previous node void * m_pchild; // The next node int m_nindex; // This node is in the queue position BOOL M_BDELETED; / / I have been deleted} ST_OBJ;

Class CjinPtraray {public: cjinptrarray (); Virtual ~ cjinptraray ();

Public: void * setat (int NINDEX, VOID * POBJ); / / Modify the target pointer of the specified point to the new pointer int getCount (); // Get the total number int append (void * pobj); // Add one, from delete queue Add, if you don't re-generate int DeleTeat (INT NINDEX); // Delete Specified, no memory is released, keep the next use void * getat (int NINDEX); // Get the specified object int RemoveAll (); // Clear all the node data, the node does not release memory, keep the next use

protected: st_obj * m_pdata; // Object Pointer INT M_NCOUNT; // Total number of objects

Protected: int matfree (); // Remove all data void * sort from memory (int NINDEX = 0); // Delete the object after sorting, reset the serial number in the column};

#ENDIF is the final is the CPP file / **************************************************** ***************************************

File: PTRARRAY.CPP Auther: HUWENJIN DATA: 9.27, 2004

Discrption:

Modification: 1.

*********************************************************** ******************************** / # include "incrudes.h" #include "ptrarray.h"

//// CjinPtrarray

CjinPtrarray :: cjinptrarray () {m_pdata = null; m_ncount = 0;

CjinPtrarray :: ~ cjinptrarray () {memfree ();

INT CJINPTRARRAY :: Memfree () {st_obj * pobj; ST_OBJ * POBJNEXT;

Pobj = m_pdata; while (1) {if (! pobj) Break; pobjnext = (st_obj *) pobj-> m_pchild; delete pobj; pobj = pobjnext;} return 0;}

INT CJINPTRARRAY :: getCount () {return m_ncount;}

INT CJINPTRARRAY:: Removeall () {st_obj * pobj = m_pdata;

While (1) {if (! pobj) Break; pobj-> m_bdeleted = true; pobj-> m_nindex = -1; pobj = (st_obj *) pobj-> m_pchild;} m_ncount = 0; Return 0;}

INT CJINPTRARRAY :: Append (void * pobjadd) {st_obj * pobj = m_pdata;

If (! pobjadd) return -1; // finds if there is a free memory area, if there is any case (1) {if (! POBJ) Break;

IF (pobj-> m_bdeleted == true) {pobj-> m_pobj = pobjadd; pobj-> m_bdeleted = false; pobj-> m_nindex = m_ncount; m_ncount ; return (m_ncount-1);} Pobj = (ST_OBJ *) POBJ-> m_pchild;

// There is no free memory area, then apply for 1 data area to join POBJ = NULL; POBJ = new st_obj; if (! Pobj) return -1; pobj-> m_bdeleted = false; pobj-> m_pobj = Pobjadd; POBJ-> M_NINDEX = m_ncount; m_ncount ; pobj-> m_pparent = null; pobj-> m_pchild = m_pdata; if (m_pdata) m_pdata-> m_pparent = Pobj;

/ / Save the current pointer to the root pointer m_pdata = pobj;

Return pobj-> m_nindex;} int cjinptrarray :: deleteat (int NINDEX) {st_obj * pobj = m_pdata;

While (1) {if (! pobj) Break;

IF (pobj-> m_nindex == nindex) {pobj-> m_bdeleted = true; pobj-> m_nindex = -1; m_ncount -; // Delete the object after re-sorting the set number sort (nindex); return m_ncount; } POBJ = (ST_OBJ *) POBJ-> m_pchild;} return -1;}

Void * cjinptrarray :: getat (int NINDEX) {st_obj * pobj = m_pdata;

While (1) {if (! pobj) Break; if (pobj-> m_nindex == nindex) Return Pobj-> m_pobj;

POBJ = (st_obj *) pobj-> m_pchild;} return null;}

Void * cjinptrarray :: sort (int nindex) {st_obj * pobj = m_pdata;

While (1) {if (! pobj) Break;

IF (pobj-> m_nindex m_nindex> nindex) pobj-> m_nindex -; next_time: pobj = (st_obj *) pobj-> m_pChild;} Return m_pdata;}

Void * cjinptrarray :: setat (int NINDEX, VOID * PTAGOBJ) {st_obj * pobj = m_pdata; void * Poldtagobj = null; while (1) {if (! pobj) Break; if (pobj-> m_nindex == nindex) { Poldtagobj = pobj-> m_pobj; pobj-> m_pobj = ptagobj; return Poldtagobj;} POBJ = (ST_OBJ *) POBJ-> m_pchild;} Return NULL;}

Ok, everyone can see the entire program is not very long, should still be very well understood.

In memory, if the user assigns 20 spaces, then delete, the program does not immediately release the memory immediately, but modify its identity because the user may immediately use more space.

All spaces are released during object destructors.

At last -----

Welcome everyone to come to my blog:

Http://blog.9cbs.net/saimen

My personal website:

Http://www.5701.com

转载请注明原文地址:https://www.9cbs.com/read-92479.html

New Post(0)