Summary of dynamic arrays of type int

Source: Internet
Author: User

A-int dynamic array of simple data structures

This seemingly no reference can be borrowed, if any one is fortunate to see, please give me a say ha! Thank you again!

The information I want to close is from a training facility, and I wrote it in reference to a dynamic array.

Want to close the introduction article C Language Chinese network: http://c.biancheng.net/cpp/html/2790.html

The array has a fixed length and cannot add new elements beyond its length, but dynamic arrays can dynamically increase the length of the array and can insert data indefinitely.

A dynamic array is a nonexistent data type, so you need to define a struct to describe the type of the dynamic array, the array needs space to store the data, because the initial capacity is unknown, so you need to define a pointer (int*):

Arrays have their own capacity, when capacity is not enough, need to dynamically increase capacity, since there is capacity, then you need a variable to store the actual length of the array

typedef struct _INT_DYNAMICARRAY

{

int *array;

int Arraylen;

int capacity;

}intdynamicarray;

Array structure, after the definition, you also need to define the operation function to close, and delete and change the basic data structure of the four functions, as well as initialization, printing, release, the size of the array, capacity, data sorting,

Initialization of a dynamic array:

The main function is to allocate memory space to the structure of dynamic Data.

int Init_intdynamicarray (Intdynamicarray **array, int capacity)

{

To determine the parameters passed in, if empty, then terminate the following execution, no space can not be executed

if (Array = = NULL)

{

Exit (-1);

}

Opens up space for dynamic arrays of structural bodies

*array = (Intdynamicarray *) malloc (sizeof (Intdynamicarray));

if (*array = = NULL)

{

Exit (-2);

}

for (*array)->array open a space of length capacity to store the data of an array

(*array)->array = (int *) malloc (sizeof (int) *capacity);

To open up the space must be judged, here is not used to return the reason is to open up space failure, the operation of the following error may be

if ((*array)->array = = NULL)

Exit (-2);

Assigning an initial value to the open space

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

(*array)->array[i] = 0;

To initialize a keyword in a struct

(*array)->arraylen = 0;

(*array)->capacity = capacity;

return 0;

}

Insertion of elements

int Insert_intdynamicarray (intdynamicarray *array, int data,int Pos)

{

Check for incoming parameters

if (Array = = NULL)

{

return-1;

}

Rational handling of incoming parameters

if (Pos < 0)

{

Pos = 0;

}

if (Pos > Array->arraylen)

{

Pos = array->arraylen;

}

If there is not enough space, then open up a space to store the new data

if (Array->arraylen = = array->capacity)

{

Open up twice times the space

int *array = (int *) malloc (sizeof (int) * 2 * array->capacity);

if (array = = NULL)

{

Return-2;

}

Copy the elements of the original data, and here you can also use loops.

memcpy (array, array->array, sizeof (int) *array->arraylen);

Release the original space

Free (Array->array);

Array->array = array;

Array->capacity *= 2;

Initialize the newly opened space

for (int i = array->arraylen; i < array->capacity; ++i)

{

Array[i] = 0;

}

}

Make room for inserting elements

for (int i = array->arraylen-1; i >= Pos; i.)

{

ARRAY->ARRAY[I+1] = array->array[i];

}

Insert new Element

Array->array[pos] = Data;

+ + array->arraylen;

return 0;

}

Deletion of elements

int Erasebypos_intdynamicarray (intdynamicarray *array, int Pos)

{

if (Array = = NULL)

{

return-1;

}

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

{

Return-2;

}

Delete directly by overwriting

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

{

Array->array[i] = array->array[i + 1];

}

--array->arraylen;

return 0;

}

Modify

int Modif_intdynamicarray (intdynamicarray *array, int Pos, int Data)

{

if (Array = = NULL)

{

return-1;

}

if (pos<0 | | POS>ARRAY->ARRAYLEN-1)

{

Return-2;

}

Array->array[pos] = Data;

return 0;

}

Find by Location

int Seekbypos_intdynamicarray (intdynamicarray *array, int Pos)

{

if (Array = = NULL)

{

return 0x00;

}

if (pos<0| | POS>ARRAY->ARRAYLEN-1)

{

return 0x01;

}

Return array->array[pos];

}

Find the location where a value first appears

int Seekbyval_intdynamicarray (intdynamicarray *array, int Val)

{

if (Array = = NULL)

{

return 0x00;

}

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

{

if (array->array[i] = = Val)

return i;

}

return-1;

}

Print a dynamic array

void Print_intdynamicarray (Intdynamicarray *array)

{

if (Array = = NULL)

{

Return

}

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

{

printf ("%d", array->array[i]);

}

printf ("\ n");

}

Array length

int Size_intdynamicarray (Intdynamicarray *array)

{

if (Array = = NULL)

{

return-1;

}

Return array->arraylen;

}

Array capacity

int Capacity_intdynamicarray (Intdynamicarray *array)

{

if (Array = = NULL)

{

return-1;

}

Return array->capacity;

}

Destroyed

int Destroy_intdynamicarray (Intdynamicarray *array)

{

if (Array = = NULL)

{

return-1;

}

if (array->array! = NULL)

{

Free (Array->array);

}

Free (Array);

return 0;

}

Oh, it's over!

Summary of dynamic arrays of type int

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.