The most basic structure in data structure is linear structure, and the linear structure is divided into continuous storage structure and discrete storage structure. The so-called continuous storage structure is actually an array.
The essence of array is also a kind of storage of data, since there is data storage, it will involve how to address the problem of data. First, let's start by talking about how the data in the array is stored, in memory, where the data in the array exists in memory as a contiguous set of data sets. 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 this knowledge, we can design arrays (we can design our own arrays for others to use, haha).
Having learned the above knowledge, the first question comes, how can I find the address of the data in memory? This problem is actually very simple, because the array in memory is a set of contiguous sets of data, so we just know the first address of the array, and then through the corresponding byte length of the addition and subtraction can find the corresponding byte number of data, with these can be defined by our array, but as a reasonable array, There should also be array-length flags for Len and arrays of valid elements for the flag CNT. This gives the definition of an array (in this case, a struct, a friend who is not familiar with the structure) can check it out.
1 struct ARR 2 {3 int // The address of the first element of the array is stored 4 int // The maximum number of elements the array can hold 5 int // the number of valid elements in the array 6 7 };
The code above defines a struct of arr, which is an array of members that store the first address of an array element, and have a member that stores the length of the array and the number of valid elements in the group.
With the definition of the struct, it should be related to the basic operation of the array, including the initialization of the arrays, judging whether the array is empty, displaying the array, judging if the arrays are full, appending an element to the last one, and inserting the elements into the group. Among them, the main algorithm is the insertion of an array of elements, the core of the insertion algorithm should first be inserted and inserted after the position of the element after the move, and then the empty position is inserted into the element we want to insert. Give the implementation of the C language:
1 /*2 Array initialization function3 initialization simply gives an array of a certain length, but there are no valid values in the array4 */5 voidInit_arr (structARR * PARR,intlen)6 {7Parr->pbase= (int*)malloc(sizeof(int)*len);8 if(null==parr->pbase) {9printf"dynamic memory allocation failure");TenExit (-1);//terminate the entire program One } A Else{ -parr->len=Len; -Parr->cnt=0; the } - } - - /* + A function that determines whether an array is empty - */ + intIs_empty (structARR *PARR) { A if(parr->cnt==0){ at return 0;//0 represents True - } - Else{ - return 1;//1 indicates false - } - } in - /* to array output display function + when you make an array output, you should first determine if the array is empty - */ the voidShow_arr (structARR *PARR) { * if(Is_empty (PARR) = =0){ $printf"the current array is empty! ");Panax Notoginseng } - Else{ the inti; + for(i=0; i<parr->cnt; ++i) { Aprintf"%d",parr->pbase[i]); the } +printf"\ n"); - } $ } $ - /* - A function that determines whether an array is full the */ - intIs_full (structARR *PARR) {Wuyi if(parr->cnt==parr->Len) { the return 0;//0 means true to indicate full - } Wu Else{ - return 1;//1 for false, indicating not full About } $ } - - /* - appends an element to the end of the array A determines whether the current array is full before appending the array elements, and does not allow new elements to be appended when full + */ the intAppend_arr (structARR *parr,intval) { - if(Is_full (PARR) = =0){ $ return 0; the } the Else{ theparr->pbase[parr->cnt]=Val; theparr->cnt++; - return 1; in } the } the About /* the inserts an element at the specified position in the array the Insert algorithm: First move the element that is inserted into the position, and then insert the empty position. the based on the algorithm principle, you should check that the array is full when you insert it. + when the above two cases are reasonable, the data is inserted, when inserting, if inserting a third position, the data is actually assigned to ARR[POS-1] - Note: When the element after the insertion position is moved back, it should move forward from behind. Otherwise, the value that will cause the "moved" location to be overwritten the */Bayi intInsert_arr (structARR *parr,intPosintval) { the if(Is_full (PARR) = =0){ the return 0;//0 indicates that the current array is full and cannot be inserted again - } - //in the case where an array can be inserted, you should check that the POS position value entered by the user is reasonable the if(pos<0|| Pos> (parr->len)) { the return 1;//1 indicates that the current user's insertion position is illegal the } the //Move Position - inti; the for(I=parr->cnt-1; i>=pos-1;--i) { theparr->pbase[i+1]=parr->Pbase[i]; the } 94 //open Position Insert element theparr->pbase[pos-1]=Val; the return 2;//2 Indicates the current insert succeeded the}
Some of the learning process, take out a record, hoping to see the friends have helped, there are mistakes in the place to welcome criticism point.
Array definition and basic operation of data structure