Summary of a general-purpose dynamic array

Source: Internet
Author: User

Basic data Structures-general-purpose dynamic arrays

The application of dynamic array is mainly for the unknown length array, first open up a space to store the data, when the space is not enough, open up twice times the space to store the data

The difference with normal arrays is that we don't have to worry about the length of the array, the only thing to focus on is whether the type of the data is a custom data type or a basic data type, but both the basic data type and the custom data type require a custom two function. These two functions traverse (print) and compare functions because, in passing an address, there is no way to judge what type it is, but to give it to the user to define the function it wants to close.

First say the basic structure:

To accommodate more data types, we store only the address of the data (void *), but we are more concerned with the first address of the data store, so we use void * * to store the first address of the data storage area;

To better manipulate the array, we need to maintain two variables, one is capacity, one is length, and when the length and capacity of the array are the same, we need to re-open a space to store the current data and the data that will be put into the array.

Directly on the code:

typedef struct _DYNAMICARRAY

{

Data storage

void **array;

The capacity of the array

int capacity;

The length of an array of existing data

int Arraylen;

}dynamicarray;

Custom data type, we must define our own initialization function

int Init_dynamicarray (void **darray)

{

if (Darray = = NULL)

{

Exit (-1);

}

Dynamicarray *dynamicarray = (Dynamicarray *) malloc (sizeof (Dynamicarray));

Dynamicarray->array = (void *) malloc (sizeof (void *) *maxcapacity);

if (Dynamicarray->array = = NULL)

{

Exit (-2);

}

for (int i = 0; i < maxcapacity; ++i)

{

will open void * pointer to null

Dynamicarray->array[i] = NULL;

* ((Dynamicarray->array) + i) = NULL;

}

Writes the changes to the array to close the data

Dynamicarray->arraylen = 0;

dynamicarray->capacity = maxcapacity;

*darray = Dynamicarray;

return 0;

}

Arrays have sequential insertions, but you can also insert them randomly within an array, but insertions are generally not allowed because the data you insert may not be accessible to arrays, so while the random insert function is provided, it does not change the length and capacity of the data because I am not sure that you are inserting the data with intent, If you insert the data in exactly the same position as the array can allow the range class, then you modify the current value, if you insert is not within the allowable range, then the original data of the array is not affected by E

Inserting data at the end of an array

int Pushback_dynamicarray (void *darray, void *data)

{

if (Darray = = NULL)

{

return-1;

}

if (Data = = NULL)

{

Return-2;

}

Dynamicarray *dynamicarray = (Dynamicarray *) Darray;

if (Dynamicarray->arraylen = = dynamicarray->capacity)

{

void * * Tempcapaticy = (void * *) malloc (sizeof (void *) *dynamicarray->capacity * 2);

if (Tempcapaticy = = NULL)

{

return-3;

}

memcpy (Tempcapaticy, Dynamicarray->array, sizeof (void *) *dynamicarray->capacity);

Free (Dynamicarray->array);

Dynamicarray->array = Tempcapaticy;

for (int i = dynamicarray->capacity; i < dynamicarray->capacity * 2; ++i)

{

Dynamicarray->array[i] = NULL;

}

Dynamicarray->capacity *= 2;

}

Dynamicarray->array[dynamicarray->arraylen] = Data;

++dynamicarray->arraylen;

return 0;

}

Specifies the position of the insertion function, if the array allows the scope of the element to modify the role of the position, if in the Disallowed range class, may cause the process to crash, of course, you can also modify the source code to insert a legitimate

Specify location Insertion

void Insertbypos (void *darray, int Pos, void *val)

{

if (Darray = = NULL)

{

Return

}

Dynamicarray *dynamicarray = (Dynamicarray *) Darray;

Dynamicarray->array[pos] = Val;

}

Delete data from a location, which may be somewhat different from the native array, because the native data does not have the ability to delete individual data, and I do the following, and when I delete a certain data, I move the back data forward one position, overwriting the original data

Specify location Delete

int Erasebypos_dynamicarray (void *darray, int Pos)

{

if (Darray = = NULL)

{

return-1;

}

Dynamicarray *dynamicarray = (Dynamicarray *) Darray;

if (Pos < 0 | | Pos >= Dynamicarray->arraylen)

{

Return-2;

}

for (int i = Pos; i < dynamicarray->arraylen-1; ++i)

{

Dynamicarray->array[i] = dynamicarray->array[i + 1];

}

Dynamicarray->array[dynamicarray->arraylen-1] = NULL;

--dynamicarray->arraylen;

return 0;

}

Header Delete (an array of this basic data type is also not, but as the data type of its own definition, for the convenience of operation, self-added)

int Erasefront_dynamicarray (void *darray)

{

int ret = Erasebypos_dynamicarray (darray, 0);

return ret;

}

End-Deletion (same-head deletion)

int Eraseback_dynamicarray (void *darray)

{

if (Darray = = NULL)

{

return-1;

}

Dynamicarray *dynamicarray = (Dynamicarray *) Darray;

int ret = Erasebypos_dynamicarray (Darray, dynamicarray->arraylen-1);

return ret;

}

View values for a location

void * Getbypos_dynamicarray (void *darray, int Pos)

{

The reason why there is no security check here is because in the actual array, we can crash the program when we access a nonexistent value.

Dynamicarray *dynamicarray = (Dynamicarray *) Darray;

Return dynamicarray->array[pos];

}

To see if a data exists in the current array

int Getbyval_dynamicarray (void *darray, void *val, COMPARE COMPARE)

{

if (Darray = = NULL)

{

return-1;

}

Dynamicarray *dynamicarray = (Dynamicarray *) Darray;

for (int i = 0; i < dynamicarray->arraylen; ++i)

{

if (!compare (Dynamicarray->array[i], Val))

{

return i;

}

}

return-1;

}

Traverse (Prints the current array)

void Traversal_dynamicarray (void *darray, traversal traversal)

{

if (Darray = = NULL)

{

Return

}

Dynamicarray *dynamicarray = (Dynamicarray *) Darray;

printf ("<............................................................>\n");

for (int i = 0; i < dynamicarray->arraylen; ++i)

{

Traversal (Dynamicarray->array[i]);

}

}

The release of the current array

int Destroy__dynamicarray (void *darray)

{

if (Darray = = NULL)

{

return-1;

}

Dynamicarray *dynamicarray = (Dynamicarray *) Darray;

if (dynamicarray->array! = NULL)

{

Free (Dynamicarray->array);

Dynamicarray->array = NULL;

}

Free (Dynamicarray);

Dynamicarray = NULL;

return 0;

}

Some other features:

Get the length of the array

int Size_dynamicarray (void *darray)

{

if (Darray = = NULL)

{

return 0;

}

Dynamicarray *dynamicarray = (Dynamicarray *) Darray;

Return dynamicarray->arraylen;

}

Get the capacity of the array

int Capacity_dynamicarray (void *darray)

{

if (Darray = = NULL)

{

return 0;

}

Dynamicarray *dynamicarray = (Dynamicarray *) Darray;

Return dynamicarray->capacity;

}

Inverse of an array

int Inverted_dynamicarray (void *darray)

{

if (Darray = = NULL)

{

return-1;

}

Dynamicarray *dynamicarray = (Dynamicarray *) Darray;

for (int i = 0; i < dynamicarray->arraylen/2; ++i)

{

void *temp = dynamicarray->array[i];

Dynamicarray->array[i] = dynamicarray->array[dynamicarray->arraylen-1-i];

Dynamicarray->array[dynamicarray->arraylen-1-I] = temp;

}

return 0;

}

Sorting of arrays

int Sort__dynamicarray (void *darray, COMPARE COMPARE)

{

if (Darray = = NULL)

{

return-1;

}

Dynamicarray *dynamicarray = (Dynamicarray *) Darray;

if (Dynamicarray->arraylen = = 1)

{

return 0;

}

Quick_sort (Dynamicarray->array, 0, dynamicarray->arraylen-1, compare);

return 0;

}

This does not give Quick_sort (Dynamicarray->array, 0, dynamicarray->arraylen-1, Compare) functions, mainly because this value modification comes from the quick row, to believe that they are also possible.

The source code may be placed on GitHub. Recently in training time learn how to use GitHub, if you have a video tutorial that wants to turn off the operation, you are welcome to share to me!

Thank you!!

Summary of a general-purpose dynamic array

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.