1、什么叫数组
元素类型相同,大小相等
2、数组的优缺点:
优点:存取速度很快
缺点:事先必须知道数组的长度
插入删除元素很慢
需要大块的内存块,空间通常有限制
#include <stdio.h>
#include <malloc.h> //包含了malloc函数
#include <stdlib.h> //exit()的头文件
//定义了一个数组类型,该类型的名字叫做struct Arr,该数据类型含有三个成员,分别是pBase,len ,cnt
struct Arr
{
int * pBase; //存储的是数组第一个元素的地址
int len; //数组所能容纳的最大元素的个数
int cnt; //当前数组有效元素的个数
// int increment; //自动增长因子
};
void init_arr(struct Arr * pArr,int length);
bool append_arr(struct Arr * pArr,int val); //追加
bool insert_arr(struct Arr * pArr,int pos, int val); //
bool delete_arr(struct Arr * pArr, int pos,int * val); //接受删除的值
int get(struct Arr * pArr, int pos);
bool is_empty(struct Arr * pArr);
bool is_full(struct Arr * pArr);
void sort_arr(struct Arr * pArr);
void show_arr(struct Arr * pArr);
void inversion_arr(struct Arr * pArr);
int main(void)
{
struct Arr arr;
int val;
init_arr(&arr,10); //结构体不能加减乘除,但是可以互相赋值
show_arr(&arr); //只传首地址
append_arr(&arr, 5);
append_arr(&arr, 7);
append_arr(&arr, 4);
append_arr(&arr, 1);
val =get(&arr, 3);
printf_s("%d \n", val);
sort_arr(&arr);
/* if (delete_arr(&arr, 5, &val))
{
printf_s("删除成功!\n");
printf_s("您删除的元素是%d\n", val);
}
else
{
printf_s("删除失败");
}
*/
// append_arr(&arr, 5);
// insert_arr(&arr, 6, 99);
show_arr(&arr); //只传首地址
getchar();
return 0;
}
void init_arr(struct Arr * pArr,int length)
{
pArr->pBase = (int *)malloc(sizeof(int) * length);
if (NULL == pArr->pBase) //检测内存是否满,分配失败返回NULL
{
printf_s("动态内存分配失败!\n");
exit(-1); //表示异常退出
}
else
{
pArr->len = length;
pArr->cnt = 0;
}
return; //表示函数已经终止了
}
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))
printf_s("数组为空!\n");
else
{
for (int i = 0; i < pArr->cnt; ++i)
printf_s("%d ", pArr->pBase[i]);
printf_s("\n");
}
}
bool append_arr(struct Arr * pArr, int val)
{
//满是返回false
if (is_full(pArr))
return false;
//不满时追加
else
{
pArr->pBase[pArr->cnt] = val;
(pArr->cnt)++;
return true;
}
}
bool insert_arr(struct Arr * pArr, int pos, int val)
{
if (is_full(pArr))
{
printf_s("数组已满!");
return false;
}
if (pos <1 || pos>pArr->cnt +1)
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++;
return true;
}
bool delete_arr(struct Arr * pArr, int pos, int * val)
{
if (is_empty(pArr))
return false;
if (pos < 1 || pos >pArr->cnt)
return false;
*val = pArr->pBase[pos - 1];
for (int i = pos; i < pArr->cnt; ++i)
{
pArr->pBase[i - 1] = pArr->pBase[i];
}
pArr->cnt--;
}
void inversion_arr(struct Arr * pArr)
{
int i = 0;
int j = pArr->cnt - 1;
int t;
if (is_empty(pArr))
{
printf_s("数组为空");
}
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;
int t;
for (i = 0;i < pArr->cnt; ++i)
{
for (j = 0; j < pArr->cnt-1; ++j)
{
if (pArr->pBase[j] > pArr->pBase[j + 1])
{
t = pArr->pBase[j];
pArr->pBase[j] = pArr->pBase[j + 1];
pArr->pBase[j + 1] = t;
}
}
}
return;
}
int get(struct Arr * pArr, int pos)
{
if (pos >= pArr->cnt)
{
printf_s("您查找的数不存在");
}
else
return pArr->pBase[pos - 1];
}