Linear Array and linear array of Data Structure
# Include <stdio. h>
# Include <malloc. h>
# Include <stdlib. h>
/*
Defines a struct, including
Saves the pointer field base pointing to the dynamic array pointer,
The valid length of the dynamic array cent,
Length of the Dynamic Array
*/
Struct Student
{
Int * base;
Int cent;
Int length;
};
// Function declaration
Void init_list (struct Student * str, int len );
Bool append_list (struct Student * str, int val );
Bool insert_list (struct Student * str, int pos, int val );
Bool delete_list (struct Student * str, int pos, int * val );
Void show_list (struct Student * str );
Int main (){
Struct Student str; // define the struct variable str
Int val;
Init_list (& str, 6); // call the init_list function and assign a value to the variable in str.
Append_list (& str, 10); // Add an element to a dynamic array
Append_list (& str, 20 );
Append_list (& str, 30 );
Append_list (& str, 40 );
Append_list (& str, 50 );
// Append_list (& str, 60 );
// Append_list (& str, 70 );
Printf ("linear table :");
Show_list (& str); // traverses a linear table
Putchar ('\ n ');
If (insert_list (& str,) // determines whether the inserted element is successful. if the inserted element is successful, the linear table is output. Otherwise, the cause of insertion failure is output.
{
Printf ("linear table after inserting elements :");
Show_list (& str );
}
Putchar ('\ n ');
If (delete_list (& str, 2, & val) // checks whether the deletion element is successful. if the deletion is successful, the deletion element and the linear table are output. Otherwise, the cause of deletion failure is output.
{
Printf ("deleted elements: % d \ n", val );
Printf ("linear table after the element is deleted :");
Show_list (& str );
}
Putchar ('\ n ');
Return 0;
}
/*
Input the address of the struct variable and the length of the dynamic array,
Initialize the struct variable str,
The valid length of str is 0, and the total length of str is len,
The base of str points to the dynamically allocated array of malloc.
*/
Void init_list (struct Student * str, int len)
{
Str-> base = (int *) malloc (sizeof (int) * len); // dynamically allocates an array
If (str-> base = NULL) // checks whether the memory allocation is successful. if it fails, it exits.
{
Printf ("memory allocation failed! \ N ");
Exit (-1 );
}
Str-> cent = 0;
Str-> length = len;
}
/*
Input the address and append value of the struct variable,
Assign val to the position marked as cent in the array,
Then cent ++
*/
Bool append_list (struct Student * str, int val)
{
If (str-> cent> = str-> length) // determines whether the valid length in the dynamic array is equal to the total length. if it is equal, false is returned.
{
Printf ("dynamic array space is insufficient! \ N ");
Return false;
}
Str-> base [str-> cent] = val; // append val to the array
Str-> cent ++; // cent + 1 in the struct
Return true;
}
/*
Input the address of the struct variable,
Use for loop to traverse Dynamic Arrays
*/
Void show_list (struct Student * str)
{
If (str-> cent <= 0) // checks whether the array is empty. if it is empty, you do not need to execute the for loop and directly return
{
Printf ("the dynamic array is empty! \ N ");
Return;
}
For (int I = 0; I <str-> cent; I ++) // uses the for loop to traverse dynamic arrays.
{
Printf ("% d \ t", str-> base [I]);
}
Putchar ('\ n ');
Return;
}
/*
Input the address of the struct variable, the insertion location, and the inserted value,
First, locate the position pos to be inserted and move all elements after pos back once,
Assign val to the position where the array subscript is the pos-1
Then cent ++
*/
Bool insert_list (struct Student * str, int pos, int val)
{
If (str-> cent> = str-> length) // checks whether the array is full. if it is full, false is returned.
{
Printf ("dynamic array is full! \ N ");
Return false;
}
If (pos <1 | pos> str-> cent) // determines whether the inserted position is appropriate. if not, false is returned.
{
Printf ("the insertion location is not suitable! \ N ");
Return false;
}
For (int I = str-> cent-1; I> = pos-1; I --) // use the for loop to move all elements after the following table is pos-1 one location
{
Str-> base [I + 1] = str-> base [I];
}
Str-> base [pos-1] = val; // assign the value of val to the position where the subscript is the pos-1
Str-> cent ++;
Return true;
}
/*
Input the address of the struct variable, the location to be deleted, and the val address,
First find the position pos to be deleted, assign the subscript to the value of the pos-1 * val,
Then, move all the elements after the pos one time,
Last cent --
*/
Bool delete_list (struct Student * str, int pos, int * val)
{
If (str-> cent <= 0) // checks whether the array is null. if it is null, false is returned directly.
{
Printf ("the dynamic array is empty! \ N ");
Return false;
}
If (pos <1 | pos> str-> cent) // determines whether the deleted location is appropriate. if not, false is returned.
{
Printf ("the location of the deleted element does not exist! \ N ");
Return false;
}
* Val = str-> base [pos-1]; // store the value of subscript as pos-1 in * val
For (int I = pos; I <str-> cent; I ++) // use the for loop to forward all elements after the subscript is pos-1
{
Str-> base [I-1] = str-> base [I];
}
Str-> cent --;
Return true;
}