Data structure Basics (1)--array C language implementation--dynamic memory allocation
Basic idea: An array is the most commonly used data structure that is stored continuously in memory, can be statically initialized (int a[2]={1,2}), and can be dynamically initialized with malloc ().
The difficulty is that when the array is deleted or inserted, the coordinates of the element are not determined. Law:
1. If you want to insert an element in the array pos position (should be moved from the back)
for (i=cnu;i>=pos;i--)
PBASE[I]=PBASE[I-1];
2. Delete the element at POS position of the array
for (i=pos+1;i<=cnu;i--)
PBASE[I-2]=PBASE[I-1];
Dynamically allocating memory using malloc and assigning the return value to the shaping pointer
int *pbase= (int *) malloc (sizeof (int) *len);//Allocate 4*len byte-length memory
This is pbase can point to the first element in the array and can be used as an array variable name.
Advantages and disadvantages of arrays:
Advantages:
Fast access speed O (1) The memory location can be found directly according to the subscript
Disadvantages:
The length of the array must be known beforehand
Insert Delete element is slow
Space is usually limited.
Chunks of contiguous blocks of memory are required
Insertion of deleted elements is inefficient
[CPP]View Plain Copy
- #include <stdio.h>
- #include <malloc.h>
- #include <stdbool.h>
- struct arr{
- The maximum number of elements that an int len;//array can access
- int cnu;//The current number of elements in the array
- int * pbase;//stores pointers to arrays
- };
- /**
- * Initialize Array
- */
- void Init_array (struct ARR * Parray,int len) {
- parray->pbase= (int *) malloc (sizeof (int) *len);//Allocate 4*len byte-length memory
- if (null== parray->pbase)//Determine if memory allocation failed
- {
- printf ("Dynamically allocating memory failed \ n");
- Exit (-1);
- }else{
- parray->len=len;
- parray->cnu=0;
- }
- return;
- }
- /**
- * Determine if the array is empty, address the memory 4 bytes, transfer structure variables need to be copied, 12 bytes
- */
- BOOL IsEmpty (struct ARR * parray) {
- if (0==PARRAY->CNU)
- {
- return true;
- }else{
- return false;
- }
- }
- /**
- * * Determine if the array is full
- */
- BOOL Isfull (struct ARR * parray)
- {
- if (PARRAY->LEN==PARRAY->CNU)
- {
- return true;
- }else {
- return false;
- }
- }
- /**
- * Display array Contents
- */
- void Show_array (struct ARR * parray) {
- if (IsEmpty (Parray))
- printf ("The array is empty! \ n ");
- else{
- int i;
- for (i=0; i<parray->cnu;i++)
- {
- printf ("%d \ n", Parray->pbase[i]);
- }
- printf ("------------------------------------\ n");
- }
- }
- /**
- * * Append an element to an array
- */
- BOOL Append (struct ARR * Parray,int val) {
- if (Isfull (Parray))
- {
- printf ("The array is full!" \ n ");
- return false;
- }else{
- parray->pbase[parray->cnu]=val;
- parray->cnu++;
- }
- }
- /**
- * * Inserts an element into an array, POS is the number of positions in a group, pos=3 is inserting an element into the a[2]
- */
- BOOL Insert (struct ARR * parray,int pos,int val)
- {
- if (pos<1| | POS>PARRAY->LEN+1)//inserted position cannot be less than 1, and cannot be more than the last element sophomore
- {
- printf ("An illegal entry in the inserted position \ n");
- return false;
- }
- if (Isfull (Parray))
- {
- printf ("The array is full, the insertion failed!") \ n ");
- return false;
- }
- int i;
- Loop moves the array that starts the POS position back
- for (i=parray->cnu;i>=pos;i--)
- The moving range is from the first POS to the end of CNU
- {
- parray->pbase[i]=parray->pbase[i-1];
- /**
- If I represents the position of the element to move, from the beginning. Right is i-1, if left, left is i-2, right shift, left is I
- */
- }
- parray->pbase[pos-1]=val;
- parray->cnu++;
- parray->len++;
- return true;
- }
- /**
- * * Delete the first POS element in the array while returning the value of the deleted element
- */
- BOOL Delete (struct ARR * parray,int Pos,int * val)
- {
- if (pos<1| | POS>PARRAY->CNU)
- {
- printf ("Delete failed, location not valid \ n");
- return false;
- }
- int i;
- *val=parray->pbase[pos-1];
- for (i=pos+1;i<=parray->cnu;i++)
- {
- Moving units are from pos+1 to CNU
- parray->pbase[i-2]=parray->pbase[i-1];
- }
- parray->cnu--;
- return true;
- }
- /**
- * * Array Inversion
- */
- bool Inverse (struct ARR * parray)
- {
- if (IsEmpty (Parray))
- {
- printf ("Inversion fails, factor group is empty");
- return false;
- }
- int i=0;
- int j=parray->cnu-1;
- int temp;
- while (I<J)
- {
- temp=parray->pbase[i];
- parray->pbase[i]= parray->pbase[j];
- parray->pbase[j]=temp;
- i++;
- j--;
- }
- return true;
- }
- int main ()
- {
- struct ARR arr;
- Init_array (&arr,6);//The address of the struct is used as an argument in order to modify the value in the struct, and if a struct variable is passed, it will be copied without changing the value
- Append (&arr,1);
- Append (&arr,2);
- Append (&arr,3);
- Append (&arr,4);
- Show_array (&arr);
- Insert (&arr,2,88);
- Show_array (&arr);
- int Val;
- Delete (&arr,1,&val);
- Show_array (&arr);
- printf ("Removed%d\n", Val);
- Inverse (&arr);
- Show_array (&arr);
- return 0;
- }
Data structure Basics (1)--array C language implementation--dynamic memory allocation