An algorithm for continuously storing arrays of C data structures

  • 2020-05-12 02:54:45
  • OfStack

Data structure array definition and basic operations

The most basic data structure is the linear structure, and the linear structure is divided into the continuous storage structure and the discrete storage structure. A continuous storage structure is an array.

Arrays are essentially a way of storing data, and since you have the storage of data, it's a question of how to address the data. First, let's talk about how data is stored in an array. In memory, the data in an array is stored in memory as a continuous set of data. When we access an array that exists in memory, we should find its address in memory, and when we find the address of the data, we can find the corresponding data. With that knowledge, we can design arrays (we can design our own arrays for others to use, haha).

With this knowledge in mind, the first question is how to find the address of the data in memory. This question is very simple, because an array in memory is a set of continuous data collection, so we need to know first address array, and then through the corresponding bytes and subtract can find corresponding to the number of bytes, with these can define our array, however, as a reasonable array, there should be the mark of the array length len and array elements of marking cnt effectively. This gives the definition of the array (in this case, the structure is used, so those who don't know anything about the structure can look it up 1).


struct Arr
{
  int *pBase; // It stores the first part of the array 1 The address of the element 
  int len; // The maximum number of elements an array can hold 
  int cnt; // The number of valid elements in an array   

};

The code above defines an struct Arr struct, which is an array containing the member that stores the first address of an array element, the member that stores the length of the array, and the number of valid elements in the array.

Once you have a definition of a structure, you need to perform basic operations on the array, including initializing the array, determining whether the array is empty, displaying the array, determining whether the array is full, appending the last element of the array, and inserting the array elements. Among them, the main algorithm is the insertion of array elements. The core of the insertion algorithm is that the elements after the insertion and insertion position should be moved back first, and then the empty position should be inserted into the elements we want to insert. 1. The implementation of c language is given below:


/*
 Array initialization function  
 Initialization is just giving 1 With the 1 A fixed-length array, but there are no valid values in the array  
*/
void init_arr(struct Arr * pArr,int len)
{
  pArr->pBase=(int *)malloc(sizeof(int)*len);
  if(NULL==pArr->pBase){
    printf(" Dynamic memory allocation failed ");
    exit(-1); // Terminate the entire program  
  }
  else{
    pArr->len=len;
    pArr->cnt=0;
  }
}

/*
 A function that determines whether an array is empty  
*/ 
int is_empty(struct Arr * pArr){
  if(pArr->cnt==0){
    return 0;  //0 On behalf of true 
  }
  else{
    return 1;  //1 On behalf of false 
  }
}

/*
 Array output display function  
 When you output an array, you should first determine if the array is empty  
*/
void show_arr(struct Arr * pArr){  
  if(is_empty(pArr)==0){
    printf(" Current array is empty! ");
  }
  else{
    int i;
    for(i=0; i<pArr->cnt; ++i){
      printf("%d  ",pArr->pBase[i]);
    }
    printf("\n");
  }
}

/*
 A function that determines whether an array is full  
*/
int is_full(struct Arr * pArr){
  if(pArr->cnt==pArr->len){
    return 0; //0 On behalf of true , it is full  
  }
  else{
    return 1; //1 On behalf of false , meaning less than  
  }
}

/*
 Append to the end of the array 1 An element  
 Determine if the current array is full before appending an array element. No new element is allowed when the array is full  
*/
int append_arr(struct Arr *pArr,int val){
  if(is_full(pArr)==0){
    return 0;
  }
  else{
    pArr->pBase[pArr->cnt]=val;
    pArr->cnt++;
    return 1;
  }
}

/*
 Inserts an element at the specified location in the array  
 Insertion algorithm: first, all the elements in the inserted position are moved backward, and then the empty position is inserted. 
 According to the algorithm principle, therefore, you should check whether the array is full when inserting.  
 When both of the above situations are reasonable, the data is inserted; when the data is inserted, the data is inserted 3 You're actually assigning data to arr[pos-1] 
 Note: when you move back the inserted element, you should move forward from back. Otherwise, the value of the "moved" location will be overwritten  
*/
int insert_arr(struct Arr *pArr,int pos,int val){
  if(is_full(pArr)==0){
    return 0; //0 Indicates that the current array is full and cannot be inserted  
  }  
  // In the case of an array that is pluggable, the user input should be checked pos Whether the position value is reasonable 
  if(pos<0||pos>(pArr->len)){
    return 1; //1 Indicates that the current user insertion location is not valid  
  } 
  // Mobile location  
  int i;
  for(i=pArr->cnt  -1;i>=pos-1;--i){
    pArr->pBase[i+1]=pArr->pBase[i];
  } 
  // Insert the element in the open position 
  pArr->pBase[pos-1]=val;
  return 2; //2 Indicates that the current insert was successful  
}

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: