Two ways to modify the sequence table of the data structure learning series

  • Method 1:
  • 数据元素的位置Modify according to the sequence table , the code is as follows:
  • Sample code:
int modify_seq_list_1(list_t *seq_list,int pos, int data){
    
    


    if(NULL == seq_list){
    
    

        printf("入参为NULL\n");

        return -1;

    }

    if( pos < 0 || pos >= seq_list->count){
    
    

        printf("修改位置不合理,修改失败\n");

        return -1;

    }

    seq_list->a[pos].num = data;


    return 0;
}
  • Precautions:
  • 1. After the formal parameters are passed to the function with the function of modifying data elements, it needs to be done 入参合理性检查;
  • 2. To determine the data element to be modified 位置在顺序表中是否合理, the content of the if conditional statement is consistent with the deletion function at any position;
  • Method 2:
  • 数据元素的值Modify according to the sequence table , the code is as follows:
  • Sample code:
int modify_seq_list_2(list_t *seq_list,int data1, int data2){
    
    

    if(NULL == seq_list){
    
    

        printf("入参为NULL\n");

        return -1;

    }

    int i = 0;
    int count = 0;

    while(i < seq_list->count){
    
    

        if(seq_list->a[i].num == data1){
    
    

            seq_list->a[i].num = data2;

            count++;

        }

        i++;
    }

    if(0 == count){
    
    


        printf("顺序表中无所要修改的值\n");

    }

    return 0;
}
  • Precautions:
  • 1. Same as method 1, after the formal parameter is passed to the function with the function of modifying the data element, it needs to be done 入参合理性检查;
  • 2. while循环或者for循环After finding the value of the data element to be modified by the method that can be used, 新值赋值于旧值just change it;
  • 3. In this function 定义的count变量, it is used 记录in the sequence table 有无所要修改的值;

Guess you like

Origin blog.csdn.net/qq_41878292/article/details/132645769