To be continued: D
<Pre name = "code" class = "c">
/*
Implementation of linear structure Arrays
*/
# Include & lt; stdio. h & gt;
# Include & lt; malloc. h & gt; // contains the malloc Function
# Include & lt; stdlib. h & gt; // contains the exit function
// First define the struct type that describes the array information
Struct Arr
{
Int * pBase; // the pointer variable that stores the first address of the array.
Int len; // array Length
Int cnt; // number of elements in the array
};
// Define the function declaration for the basic operation of the array
Void init_arr (struct Arr * pArr, int length); // array Initialization
Bool append_arr (struct Arr * pArr, int val); // append Element
Bool insert_arr (struct Arr * pArr, int index, int val); // insert an element
Bool delete_arr (); // deletes an element.
Int get (); // get the element
Bool is_empty (struct Arr * pArr); // determines whether it is null.
Bool is_full (); // determines whether it is full.
Void sort_arr (); // sort
Void show_arr (struct Arr * pArr); // traverses the Array
Void inversion_arr (); // array Inversion
Int main (void)
{
Struct Arr arr;
Init_arr (& amp; arr, 6); // test the initialization function.
// Show_arr (& amp; arr );
Append_arr (& amp; arr, 3 );
Append_arr (& amp; arr, 2 );
Append_arr (& amp; arr, 9 );
Insert_arr (& amp; arr, 2, 7 );
Show_arr (& amp; arr );
Return 0;
}
// Initialize the Array Function. pArr is the pointer of the struct variable arr.
Void init_arr (struct Arr * pArr, int length)
{
PArr-& gt; pBase = (int *) malloc (sizeof (int) * length); // malloc () function header file Declaration
If (NULL = pArr-& gt; pBase)
{
Printf ("dynamic memory allocation failed! \ N ");
Exit (-1); // declare it in the header file
}
Else
{
PArr-& gt; len = length;
PArr-& gt; cnt = 0;
}
}
// Array traversal function implementation
Void show_arr (struct Arr * pArr)
{
If (is_empty (pArr ))
{
Printf ("the array is empty \ n ");
}
Else
{
For (int I = 0; I & lt; pArr-& gt; cnt; I ++)
{
Printf ("% d", pArr-& gt; pBase [I]);
}
}
}
// Determine whether the array is empty
Bool is_empty (struct Arr * pArr)
{
If (pArr-& gt; cnt = 0)
Return true;
Else
Return false;
}
// Append an array element
Bool append_arr (struct Arr * pArr, int val)
{
If (pArr-& gt; cnt & lt; pArr-& gt; len)
{
PArr-& gt; pBase [pArr-& gt; cnt] = val;
(PArr-& gt; cnt) ++;
Return true;
}
Else
Printf ("array full \ n ");
Return false;
}
// Insert element
Bool insert_arr (struct Arr * pArr, int index, int val)
{
If (pArr-& gt; cnt & lt; pArr-& gt; len & amp; index & lt; = pArr-& gt; cnt)
{
For (int I = pArr-& gt; cnt-1; I & gt; = index-1; I --)
{
PArr-& gt; pBase [I + 1] = pArr-& gt; pBase [I];
}
PArr-& gt; pBase [index-1] = val;
(PArr-& gt; cnt) ++;
Return true;
}
Else
{
Printf ("insertion failed \ n ");
Return false;
}
} </Pre>