Step-by-step writing algorithm (the processing of linear structures)

Source: Internet
Author: User

Original: Step-by-step writing algorithm (the processing of linear structures)

"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "


We know that the space in memory is continuous. In other words, the address below 0x00000001 must be 0x00000002. Therefore, there is no change of address in space. What type of data structure is continuous internal space, is actually an array, of course, it can be a heap. Arrays have many advantages, and they can hold the same type of data within a contiguous space and manage that data. So in this sense, mastering the array can explain how you get started with data structures.

So, what should we pay attention to in the actual development of the linear structure? My personal point of view:

(1) The resources of the array are limited and the scope of the resources must be determined

(2) The application and release of resources in the array must be one by one corresponding, otherwise it is easy to cause the phenomenon of resource leakage

(3) The considerations in the array are also applied to the contiguous memory resource space of the heap allocation

Below is the design of an int assigned to the small program, you can try together:

a) design the data form of the memory node

typedef struct _DATA_NODE{INT* pdata;char* pflag;int num;} Data_node; #define STATUS int#define TRUE 1#define FALSE 0
b) Create memory node

data_node* Malloc_node (int number) {data_node* Pdatanode = null;if (0 = = number) return Null;pdatanode = (data_node*) malloc (sizeof (Data_node)); assert (NULL! = Pdatanode); memset (pdatanode, 0, sizeof (data_node));pD Atanode->pdata = (int*) malloc (sizeof (int) * number), if (null = = Pdatanode->pdata) {free (pdatanode); return NULL;} Pdatanode->pflag = (char*) malloc ((number + 7) >> 3); if (NULL = = Pdatanode->pflag) {free (pdatanode->pdata) ; free (pdatanode); return NULL;} memset (pdatanode->pdata, 0, sizeof (int) * number), memset (Pdatanode->pflag, 0, (number + 7) >> 3);pD atanode-& Gt;num = Number;return Pdatanode;}
c) Delete memory node

STATUS free_node (const data_node* pdatanode) {if (null = = Pdatanode) return False;assert (null! = Pdatanode->pdata); ASSERT (NULL! = pdatanode-> PFlag), assert (0! = Pdatanode), free (Pdatanode->pflag), free (pdatanode->pdata); Free ((void*) pdatanode); return TRUE;}
d) Determine if there is currently memory available to allocate

int check_if_data_exist (const data_node* pdatanode) {int number = pdatanode->num;char* PFlag = pdatanode->pflag; unsigned char flag = 0;int loop = 1;while (Loop <= number) {flag = pflag[(loop + 7) >> 3-1] & (0x1 << ( (Loop + 7)% 8); if (0! = flag) {return loop;} loop + +;} return-1;}
e) allocating memory space

int* alloca_data (const data_node* pdatanode) {int* pData = Null;int pos;if (NULL = = Pdatanode) return null;if ( -1 = = (pos = ch Eck_if_data_exist (Pdatanode))) return null;pdatanode->pflag[(pos + 7) >> 3-1] |= 0x1 << ((pos + 7)% 8); ret Urn Pdatanode->pdata + (pos-1);}
f) Reclaim memory space

STATUS free_data (const data_node* pdatanode, const int* pData) {int pos = 0;if (NULL = = Pdatanode | | NULL = = PData) return false;if (PData < Pdatanode->pdata | | PData > (pdatanode->pdata + pdatanode->num)) Retu RN False;pos = (pdata-pdatanode->pdata) >> 3;pdatanode->pflag[(pos + 7)-1]  &= ~ (0x1 << (pos + 7)% 8)); return TRUE;}
g) Statistics How many DWORD spaces are currently allocated
int count_free_space (const data_node* pdatanode) {int count = 0;int loop = 1;char flag = 0;if (NULL = = Pdatanode) return 0;FO  R (; loop <= pdatanode->num; loop++) {flag = pdatanode->pflag[(loop + 7) >> 3-1] & (0x1 << ((Loop + 7)% 8); if (0 = = Flag) {count + +;}} return count;}

The code above is just a demonstration and can be improved on this basis, for example:

(1) modified to be free to allocate a lot of memory, note the need to change the flag structure type

(2) Change to first-come-first-served memory allocation type

(3) Memory allocation type modified to the most suitable space

(4) Modify the mode of memory allocation to debug type, each time the allocation and release to check whether the memory is out of bounds, not run in pairs, attention needs to add the corresponding judgment function


"Trailer: The following blog introduces the queue linear table"


Step-by-step writing algorithm (the processing of linear structures)

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.