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 same
#include <stdio.h> #include <malloc.h> #include <stdbool.h>struct arr{int len;//array The maximum number of elements that can be accessed int CNU The current number of elements in the array int * pbase;//store pointer to array};/*** 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 allocated memory failed \ n"); Exit (-1);} else{parray->len=len;parray->cnu=0;} return;} /*** determines whether the array is empty, the address saves 4 bytes of memory, the transfer structure variable needs to be copied, 12 bytes */bool isempty (struct ARR * parray) {if (0==PARRAY->CNU) {return true;} Else{return false;}} /**** determines whether the array is full */bool isfull (struct ARR * parray) {if (PARRAY->LEN==PARRAY->CNU) {return true; }else {return false; }}/*** Displays the 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");}} /**** appends 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 the array, the POS is the first position in the group, and the pos=3 is the insert element to a[2] */bool insert (struct ARR * Parray,int pos,int val) {if (pos<1| | POS>PARRAY->LEN+1)//The insertion position cannot be less than 1, and cannot be more than the last element of the sophomore {printf ("an illegal entry in the inserted position \ n"); return false; } if (Isfull (Parray)) {printf ("The array is full, insert failed! \ n "); return false; } int i; The loop moves the POS position to the beginning of the array after the for (i=parray->cnu;i>=pos;i--)//Move range is from the first POS to the end CNU {PARRAY->PBASE[I]=PARRAY->PB ASE[I-1]; /** If I represents the position of the element to move, from the beginning. The right side 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;} /**** deletes the first POS element in the array, 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 Inverted */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 as an argument, in order to modify the structure of the value, if the structure of the variable is passed, then the copy will not change 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 (1)--Array C language implementation