#include <stdio.h>
#include <malloc.h> //malloch函数
#include <stdlib.h>//exit函数
#include <stdbool.h>
struct Arr
{
int *pBase;
int len;
int cnt;
};
bool append_arr(struct Arr*pArr,int val);
bool insert_arr(struct Arr*pArr,int pos,int val);
void init_arr(struct Arr*pArr,int length);
bool delet_arr(struct Arr*pArr,int pos,int * pval);
bool get_arr();
bool is_full(struct Arr*pArr);
bool is_empty(struct Arr*pArr);
void sotr_arr(struct Arr*pArr);
void show_arr(struct Arr*pArr);
void innversion_arr(struct Arr*pArr);
int main (void)
{
struct Arr arr;
int len=6;
int val;
int posi=2;
init_arr(&arr,len);
append_arr(&arr,1);
append_arr(&arr,3);
append_arr(&arr,6);
append_arr(&arr,98);
append_arr(&arr,90);
insert_arr(&arr,2,10);
show_arr(&arr);
insert_arr(&arr,1,100);
delet_arr(&arr,2,&posi);
sotr_arr(&arr);
show_arr(&arr);
innversion_arr(&arr);
show_arr(&arr);
return 0;
}
void init_arr(struct Arr*pArr,int length)
{
pArr->pBase = (int*)malloc(sizeof(int)*length) ;
if(NULL==pArr->pBase)
{
printf("动态内存分配失败!\n");
exit(-1);
}
else
{
printf("动态数组分配成功!\n");
pArr->len=length;
pArr->cnt=0;
}
return;
}
bool is_empty(struct Arr*pArr)
{
if(0==pArr->cnt)
return true;
else
return false;
}
void show_arr(struct Arr*pArr)
{
if(is_empty(pArr))
{
printf("数组为空\n");
}
else
{
for(int i=0;i<pArr->cnt;++i)
{
printf("%d",pArr->pBase[i]);
printf("\n");
}
}
printf("\n");
}
bool is_full(struct Arr*pArr)
{
if(pArr->cnt==pArr->len)
return true;
else
return false;
}
bool append_arr(struct Arr*pArr,int val)
{
if(is_full(pArr))
return false;
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))
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;
}
bool delet_arr(struct Arr * pArr,int pos,int * pval)
{
int i;
if(is_full(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 innversion_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 sotr_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;
}
}
}
}