Arr

# include<stdio.h>
# include <malloc.h>
# include <stdlib.h> // Contains the exit function

//The data type name is defined as struct Arr
struct Arr
{
int * pBase; //The address of the first element of the stored array
int len; //The number of valid elements that the array can hold
int cnt; //The current array Number of valid elements
} ;

void init_arr(struct Arr*pArr, int length);
bool append_arr(struct Arr*pArr,int val); // The result of appending boolean type at the end is 0 or 1
bool insert_arr(struct Arr*pArr,int pos, int val); // The value of pos starts from 1 to represent the position, if pos is 2, it means to insert val before the second position
bool delete_arr(struct Arr*pArr,int pos, int *pVal);
int get() ;
bool is_empty(struct Arr * pArr);
bool is_full(struct Arr*);
void sort_arr(struct Arr * pArr);
void show_arr(struct Arr * pArr);
void inversion_arr(struct Arr*pArr); //Invert

int main (void)
{
struct Arr arr; //arr is the data variable before the init array is not generated but memory is allocated
int val;

init_arr(&arr,8);
show_arr(&arr);
append_arr(&arr, 98);
append_arr(&arr, 2);
append_arr (&arr, 3);
append_arr(&arr,57);
append_arr(&arr,5);
append_arr(&arr,6);
insert_arr(&arr, 2, 99);
//sort_arr(&arr);
show_arr(&arr);

if( delete_arr(&arr,1,&val))
{
printf("Deletion succeeded\n");
printf("The element you deleted is: %d\n",val);
}
else
{
printf("Deletion failed");
}
show_arr(&arr);
inversion_arr(&arr);

printf("The content of the array after inversion is:\n");
show_arr(&arr);
sort_arr(&arr);







//printf("%d\n",arr.len); //So arr=99



return 0;
}
void init_arr( struct Arr *pArr,int length) //Assign the value of arr to *pArr
{
pArr- >pBase = (int *)malloc(sizeof(int) *length); //equivalent to pARR, the pointer variable points to the pBase member in the structure variable, and then the allocated memory has 24 bytes 4*6
if( NULL ==pArr->pBase)
{
printf("dynamic memory allocation failed\n");
exit(-1);//terminate the entire program
}
else
{
pArr->len =length;
pArr->cnt =0;
}
return;
//(*pArr).len =99; //So &pArr= arr why don't you write arr directly because you need to add & to directly transfer data, so you set a new variable by yourself
}

bool is_empty(struct Arr * pArr)
{
if(0 == pArr->cnt)
return true;

else
return false;
}

bool is_full(struct Arr* pArr)
{
if(pArr->cnt == pArr->len)
return true;
else
return false;
}

void show_arr(struct Arr * pArr)
{
if(is_empty(pArr)) // pArr needs to be No need to add &
{
printf("The array is empty\n");
}
else
{
for(int i=0; i<pArr->cnt;++i)
printf("%d ",pArr->pBase[i ]); //
}
}

bool append_arr(struct Arr* pArr,int val) //(struct Arr* pArr here must bring pArr. You can omit
{
if(is_full(pArr))
return false;

//Append
pArr->pBase[pArr->cnt] =val;
(pArr->cnt)++;
return true;

}

bool insert_arr(struct Arr*pArr,int pos, int val)
{
int i;


if(is_full(pArr)) {
printf("full!");
return false;
}


if(pos<1 || pos>pArr->cnt+1)
return false;

for(i =pArr->cnt-1; i>=pos-1; --i)
{
pArr->pBase[i+1] =pArr->pBase[i];
}
pArr->pBase[pos-1] = val;
(pArr->cnt)++;
return true;

/*
if (is_full(pArr)) {
// pArr->pBase = (int *)realloc(pArr->pBase, sizeof(int) * pArr->len+1); //动态扩大内存
printf("insert failed\n");
return false;
}
if (pos<1 || pos>pArr->cnt+1) {
printf("Invalid argument\n");
return false;
}
for (int i = pArr->cnt-1; i >= pos-1;--i) {
pArr->pBase[i+1] = pArr->pBase[i];
}
pArr->pBase[pos-1] = val;
(pArr->cnt)++;
printf("Insert successfully\n");
return true;*/
}

bool delete_arr(struct Arr*pArr,int pos, int *pVal)
{
int i;
if(is_empty(pArr))
return false;
if(pos<1 || pos>pArr->cnt)
return false;

*pVal = pArr->pBase[pos-1];
for(i=pos; i<pArr->cnt; ++i)
{
pArr->pBase[i-1]= pArr->pBase[i];
}
pArr->cnt--;
return true;
}
void inversion_arr(struct Arr*pArr)
{
int i=0;
int j=pArr->cnt-1;
int t;

while(i < j)
{
t =pArr->pBase[i];
pArr->pBase[i]=pArr->pBase[j];
pArr->pBase[j]=t;
++i;
--j;

}
return;
}

void sort_arr(struct Arr * pArr)
{
int i,j,t;
for(i =0; i<pArr->cnt;++i)
{
for(j=i+1; j<pArr->cnt;++j)
{
if(pArr->pBase[i]>pArr->pBase[j])
{

t =pArr->pBase[i];
pArr->pBase[i]=pArr->pBase[j];
pArr->pBase[j]=t;
}

}
}
}
//插入和排序没出来23333 2018.4.14.16点01分

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326032647&siteId=291194637
Arr