#include <stdio.h>
#include <malloc.h> //malloch函数
#include <stdlib.h>//exit函数
#include <stdbool.h>
typedef struct Arr
{
int* pBase;
int cnt;
int len;
}ARR;
bool append_arr();
void init_arr( );
bool delete_arr();
bool get_arr();
bool is_full();
bool is_empty();
void sotr_arr();
void show_arr();
void innversion_arr();
bool inser_arr();
int main (void)
{
ARR arr;
int val;
init_arr(&arr,5);
append_arr(&arr,1);
append_arr(&arr,2);
append_arr(&arr,3);
append_arr(&arr,4);
show_arr(&arr);
inser_arr(&arr,1,3);
sotr_arr(&arr);
show_arr(&arr);
delete_arr(&arr,5,&val);
sotr_arr(&arr);
show_arr(&arr);
}
void init_arr(ARR* pArr,int lengt)
{
pArr->pBase = (int*)malloc(sizeof(int)*lengt);
if(NULL==pArr->pBase)
{
printf("内存分配失败,程序终止!\n");
exit(-1);
}
else
{
printf("内存分配成功!\n");
pArr->cnt = 0;
pArr->len = lengt;
}
return;
}
bool is_empty(ARR* pArr)
{
if(NULL==pArr->pBase)
return true;
else
return false;
}
void show_arr(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(ARR* pArr)
{
if(pArr->len==pArr->cnt)
return true;
else
return false;
}
bool append_arr(ARR* pArr,int val)
{
if(is_full(pArr))
{
printf("数组中已满,追加失败!\n");
return false;
}
else
{
pArr->pBase[pArr->cnt]=val;
(pArr->cnt)++;
return true;
}
}
bool inser_arr(ARR* pArr, int pos,int val)
{
int i;
if(is_full(pArr))
{
printf("数组已满,插入失败!\n");
return false;
}
if( 1 > pos || pArr->cnt+1 < pos)
{
printf("插入的位置有误!\n");
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 delete_arr(ARR* pArr,int pos,int* pval)
{
int i;
if(is_empty(pArr))
{
printf("数组为空,删除失败!\n");
return false;
}
if(1 > pos || pos > pArr->cnt)
{
printf("数组中不存在该号元素!\n");
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(ARR* pArr)
{
int i = 0;
int j = pArr->cnt-1;
int k;
while(i < j)
{
k = pArr->pBase[i];
pArr->pBase[i] = pArr->pBase[j];
pArr->pBase[j] = k;
i++;
j--;
}
return;
}
void sotr_arr(ARR* pArr)
{
int i,j,t;
for(i=0;i<pArr->cnt-1;++i)
{
for(j=i+1;j<pArr->cnt;++j)
{
if(pArr->pBase[i]>pArr->pBase[j])
{
t=pArr->pBase[j];
pArr->pBase[j]=pArr->pBase[i];
pArr->pBase[i]=t;
}
}
}
}